#P3688. [ZJOI2017] 树状数组

    ID: 2674 远端评测题 4000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017线段树各省省选树状数组浙江

[ZJOI2017] 树状数组

Description

On a dark night, Kurousagi Kotori tossed and turned in bed. Unable to sleep, she remembered a tragic OI contest experience years ago. It was a basic Binary Indexed Tree problem.

Given an array AA of length nn, initially all 00, perform mm operations of two types:

  • 1 x1\ x: set AxA_x to (Ax+1)mod2(A_x + 1) \bmod 2.
  • 2 l r2\ l\ r: ask for (i=lrAi)mod2(\sum_{i=l}^r A_i) \bmod 2.

Although Kotori was quite simple back then, she still noticed that this problem could be solved using a Binary Indexed Tree. When she was very young, she wrote the following algorithm:

Here lowbit(x)\mathrm{lowbit}(x) denotes the lowest nonzero binary bit of number xx, e.g., lowbit(5)=1,lowbit(12)=4\text{lowbit}(5) = 1, \text{lowbit}(12) = 4. For the first type of operation she calls Add(x)\mathrm{Add}(x), and for the second type the answer is Query(l,r)\mathrm{Query}(l, r).

If you are familiar with Binary Indexed Trees, it is not hard to see that Kotori wrote it incorrectly: the direction in which xx changes in Add\text{Add} and Find\text{Find} is reversed. Therefore, this program spectacularly got a score of 00 in the final test.

Strangely, at that time, this program passed the large sample test provided by the problem setter—this is also why Kotori did not conduct stress testing.

Now, Kotori wants to compute the probability that this program answers each query correctly, so she can once again feel how “fei” she was. However, many years have passed, and even Kotori cannot fully remember the large sample. Fortunately, she recalls most of it; the only thing she forgot is the value of xx for each type 1 operation, so she assumes that xx is chosen uniformly at random from the range [li,ri][l_i, r_i] for that operation.

Specifically, Kotori gives an array AA of length nn, initially 00, and then performs mm operations:

  • 1 l r1\ l\ r: uniformly at random pick an xx in the interval [l,r][l, r] and execute Add(x)\text{Add}(x).
  • 2 l r2\ l\ r: ask for the probability that executing Query(l,r)\text{Query}(l, r) yields the correct result.

Input Format

The first line contains two integers n,mn, m.

Each of the next mm lines describes one operation in the format specified above.

Output Format

For each query, output one integer representing the answer. If the simplified fraction of the answer is xy\frac x y, you only need to output the value of x×y1mod998244353x \times y^{-1} \bmod 998244353 (that is, output the answer modulo 998244353998244353).

5 5
1 3 3
2 3 5
2 4 5
1 1 3
2 2 5
1
0
665496236

Hint

  • Sample Explanation:

    After performing Add(3)\mathrm{Add}(3), the array AA becomes [0,1,1,0,0][0, 1, 1, 0, 0]. Therefore, the program’s answers for the first two queries are both 11. Hence, the first query is certainly correct, and the second query is certainly incorrect.

  • Constraints:

    • Test point 1: n5n \le 5, m10m \le 10, no additional constraints.
    • Test point 2: n50n \le 50, m50m \le 50, no additional constraints.
    • Test point 3: n50n \le 50, m50m \le 50, no additional constraints.
    • Test point 4: n3×103n \le 3 \times 10^3, m3×103m \le 3 \times 10^3, no additional constraints.
    • Test point 5: n3×103n \le 3 \times 10^3, m3×103m \le 3 \times 10^3, no additional constraints.
    • Test point 6: n=105n = 10^5, m=105m = 10^5, all queries occur after updates.
    • Test point 7: n=105n = 10^5, m=105m = 10^5, all queries occur after updates.
    • Test point 8: n=105n = 10^5, m=105m = 10^5, no additional constraints.
    • Test point 9: n=105n = 10^5, m=105m = 10^5, no additional constraints.
    • Test point 10: n=105n = 10^5, m=105m = 10^5, no additional constraints.

    For 100%100\% of the testdata, it is guaranteed that 1lrn1 \le l \le r \le n.

    Update: 2018/05/13 @larryzhong provided 5 stronger testdata sets.

Translated by ChatGPT 5