Greatest common divisor
Practice
3 (9 votes)
Advanced data structures
Data structures
Gcd
Implementation
Number theory
Segment trees
Problem
91% Success 2110 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given array A. There are 4 types of operations associated with it:

  • 1 l r x, for each i ∈ [l, r] replace ai with the value of x.
  • 2 l r x, for each i ∈ [l, r] replace ai with the value of the gcd(ai, x) function.
  • 3 l r, print the value of max ai, l ≤ i ≤ r.
  • 4 l r, print the value of al + al+1 + ... + ar.

A greatest common divisor (gcd(a, b)) of two positive integers a and b is equal to the biggest integer d such that both integers a and b are divisible by d.

Input format

  • The first line contains two integers n, m (1 ≤ n, m ≤ 105) - the number of array elements and the number of queries.
  • The second line contains n positive integers a1, a2, ..., an - initial state of the array.(1 ≤ max A≤ 109, 1 ≤ i ≤ n)
  • Next m lines contain the description of the queries, one per line. Queries are formatted the same way as in the problem statement above.
  • It is guaranteed that 1 ≤ l ≤ r ≤ n and 1 ≤ x ≤ 109.

Output format

For each third and fourth query type, print the answer for this query in a separate line.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
754 votes
Tags:
ApprovedFenwick TreeMathMediumNumber TheorySegment Trees
Points:30
330 votes
Tags:
AlgorithmsApprovedFenwick TreeMediumOpen
Points:30
14 votes
Tags:
ApprovedData StructuresMathMediumRecruitSegment Trees