This page is still under construction.

Parts of this page are still being built. What you see may change.

Range GCD

Time limit2sMemory limit512 MB

Summary
Support range add and range GCD queries on an array by tracking a difference array with a segment tree and one prefix sum.
Level

Hard8 of 10

Topics
Segment tree, Number theory, Math
Solved
No attempts yet

Problem

You are given a sequence of NN natural numbers A1,A2,…,ANA_1, A_2, \dots, A_N. Two kinds of operations act on this sequence.

  • Add the same value to every element of a contiguous range.
  • Compute the greatest common divisor of the elements in a contiguous range.

Process the operations in the order they are given, and print an answer for every operation of the second kind.

Input

The first line contains the number of elements NN. (1≤N≤1000001 \le N \le 100000)

The second line contains the NN elements of the sequence, separated by spaces. The ii-th number is AiA_i, and 1≤Ai≤1091 \le A_i \le 10^9.

The third line contains the number of operations QQ. (1≤Q≤1000001 \le Q \le 100000)

Each of the next QQ lines contains one operation as three integers TT, AA, BB.

  • If TT is not 0, add TT to every element from the AA-th through the BB-th.
  • If TT is 0, print the greatest common divisor of the elements from the AA-th through the BB-th.

TT satisfies 0≤T≤1090 \le T \le 10^9, and AA and BB satisfy 1≤A≤B≤N1 \le A \le B \le N. At least one operation has TT equal to 0.

Output

For every operation with TT equal to 0, print the greatest common divisor of the requested range on its own line, in the order the operations are given.

Examples8

  1. Example 1

    Input
    4
    6 3 38 49
    5
    0 1 3
    9 2 2
    0 1 2
    6 3 3
    0 3 4
    
    Expected output
    1
    6
    1
    
  2. Example 2

    Input
    1
    1000000000
    3
    0 1 1
    1000000000 1 1
    0 1 1
    
    Expected output
    1000000000
    2000000000
    
  3. Example 3

    Input
    5
    12 12 12 12 12
    5
    0 1 5
    12 1 2
    0 1 5
    0 1 2
    0 3 5
    
    Expected output
    12
    12
    24
    12
    
  4. Example 4

    Input
    8
    6 12 18 24 30 36 42 48
    6
    0 1 8
    0 2 4
    6 1 8
    0 1 8
    1000000000 4 8
    0 4 5
    
    Expected output
    6
    6
    6
    2
    
  5. Example 5

    Input
    4
    1000000000 1000000000 1000000000 999999999
    6
    0 1 4
    0 1 3
    1000000000 1 4
    0 1 3
    0 4 4
    0 1 4
    
    Expected output
    1
    1000000000
    2000000000
    1999999999
    1
    
  6. Example 6

    Input
    10
    1 2 4 8 16 32 64 128 256 512
    8
    0 1 10
    0 2 4
    0 5 10
    3 1 1
    0 1 3
    0 9 10
    512 1 10
    0 1 10
    
    Expected output
    1
    2
    16
    2
    256
    2
    
  7. Example 7

    Input
    5
    100 200 300 400 500
    6
    0 1 5
    0 1 1
    0 2 3
    0 3 5
    0 5 5
    0 2 5
    
    Expected output
    100
    100
    100
    100
    500
    100
    
  8. Example 8

    Input
    3
    5 5 5
    5
    5 3 3
    0 1 3
    0 3 3
    7 1 3
    0 1 3
    
    Expected output
    5
    10
    1