Bob's Fair Sharing

hard segment-tree number-theory arrays 1000 ms · 256 MB

Bob's Fair Sharing

Alice keeps n candy boxes in a row on a shelf, numbered 1 to n. Box i holds a_i candies (a box can be empty, holding 0 candies). From time to time Alice swaps the contents of one box for a different count.

Bob visits often. Each time he picks a range of boxes [l, r] and invites some friends over, wanting to split the candies in every box of that range evenly among them — each friend gets a whole number of candies from each box, with none left over. Bob wants to know the largest possible number of friends for which this works, which is exactly the greatest common divisor (GCD) of a_l, a_l+1, ..., a_r. By convention, if every box in the range is empty, report 0 (there is no largest group size that works, so Bob reports the range as "nothing to share").

Process Alice's swaps and Bob's questions in the order they occur.

Input

The first line contains two integers n and q (1 ≤ n, q ≤ 2·10^5) — the number of boxes and the number of operations.

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^18) — the initial candy counts.

Each of the next q lines describes one operation, in one of two forms:

  • 1 i v — Alice replaces the count of box i (1 ≤ i ≤ n) with v (0 ≤ v ≤ 10^18).
  • 2 l r — Bob asks about the range [l, r] (1 ≤ l ≤ r ≤ n); report the GCD of a_l, ..., a_r, or 0 if all of them are 0.

Output

For each operation of the second form, print the answer on its own line.

Notes

In the first example, after the boxes start as [4, 6, 8], Bob's first query covers the whole row: gcd(4, 6, 8) = 2, so at most 2 friends can share every box evenly. After Alice replaces box 2 with 9, the same range has gcd(4, 9, 8) = 1.

Example 1

Input

3 3
4 6 8
2 1 3
1 2 9
2 1 3

Output

2
1

Example 2

Input

5 6
0 0 5 10 0
2 1 2
2 1 3
2 3 4
1 3 0
2 1 5
2 4 4

Output

0
5
5
10
10

Hints · reveal one at a time if you're stuck

Hint 1

This is a range query combined with point updates, and the combining operation is gcd. Which data structure supports both a single-value change and a range combine in logarithmic time?

Hint 2

Store the gcd of each segment in a segment tree, and update the path to the root after each change. Treat 0 as the identity for gcd, since gcd(0, x) = x, so empty boxes need no special handling.

report an issue Prefer your own editor? sooks prep 79 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.