Under construction

Still being built. Things may change

LCM of GCDs

Time limit10sMemory limit512 MB

Summary
With point updates to a sequence, each query asks for the LCM of the GCDs obtained by removing k (at most 2) items from a range.
Level

Medium7 of 10

Topics
Segment tree, Number theory
Solved
No attempts yet

Problem

You are given a sequence of positive integers, followed by a number of instructions that update the sequence and query it. Updates and queries come in an arbitrary order.

Each update replaces a single item in the sequence with a given value. Updates accumulate: every later instruction runs on the sequence after that replacement.

Each query specifies a subsequence of the (possibly updated) sequence and the number of items to exclude from it. Excluding different items gives different sets of integers. Each set has a greatest common divisor (GCD). The answer to the query is the least common multiple (LCM) of these GCDs.

Figure H.1. Answering the last query "Q 2 5 2" of Sample Input 1

Input

The input consists of a single test case in the following format.

n m
a1 . . . an
c1
.
.
.
cm

The first line has two integers, nn and mm. nn (1≤n≤1051 \le n \le 10^5) is the length of the sequence, and mm (1≤m≤1051 \le m \le 10^5) is the number of instructions. The second line gives the original sequence a1,…,ana_1, \ldots, a_n, where 1≤ai≤1061 \le a_i \le 10^6 for all ii.

Each of the following mm lines is either an update starting with U or a query starting with Q.

An update has the following format.

U j x

It replaces the jj-th item of the sequence with xx, where 1≤j≤n1 \le j \le n and 1≤x≤1061 \le x \le 10^6. Updates accumulate: all later instructions run on the sequence after every earlier update has been applied.

A query has the following format.

Q l r k

Here ll and rr give the start and end positions of a subsequence, and kk is the number of items to exclude from bl,…,brb_l, \ldots, b_r. The sequence b1,…,bnb_1, \ldots, b_n is the sequence after all updates that come before the query. The constraints are 1≤l1 \le l, 0≤k≤20 \le k \le 2, and l+k≤r≤nl + k \le r \le n.

Output

Update instructions produce no output. For each query, output one line containing the LCM of the GCDs of all the sets formed by excluding kk items from bl,…,brb_l, \ldots, b_r.

Hint

For the first query, "Q 1 3 1", the subsequence is 12 10 16. Excluding one item gives the sets {12, 10}, {12, 16}, and {10, 16}. Their GCDs are 2, 4, and 2, so the answer is their LCM, 4.

The fifth instruction, "U 3 21", changes the answer to the sixth query, "Q 1 3 1". After the update, the subsequence is 12 10 21. The sets after excluding one item are {12, 10}, {12, 21}, and {10, 21}. Their GCDs are 2, 3, and 1, so the answer is 6.

Examples1

  1. Example 1

    Input
    5 10
    12 10 16 28 15
    Q 1 3 1
    Q 3 4 0
    Q 2 2 0
    Q 2 5 2
    U 3 21
    Q 1 3 1
    Q 2 4 1
    Q 3 5 1
    Q 4 4 0
    Q 2 5 2
    
    Expected output
    4
    4
    10
    20
    6
    14
    21
    28
    210