#P10891. 【烂题杯 Round 1】消灭劳嗝

【烂题杯 Round 1】消灭劳嗝

Problem Description

You need to eliminate Lao Ge.

Given a permutation A=a1,a2,,anA=a_1,a_2,\cdots,a_n of length nn, define Si={xximaxikxakax}S_i=\{x|x\ge i\land \max_{i\le k\le x}a_k\le a_x\}. You may understand it as the set of indices of prefix maximums in the suffix starting at ii. For example, for A={3,5,2,1,4}A=\{3,5,2,1,4\}, S1={1,2}S_1=\{1,2\} and S3={3,5}S_3=\{3,5\}.

There are qq queries. Each query gives l,rl,r, and you need to compute:

$$\left(\left(\sum_{l\le x\le y\le r} |S_x\cup S_y|-\sum_{\substack{{1\le x<l}\\{r<y\le n}}} |S_x\cup S_y|\right)\bmod P+P\right)\bmod P$$

where P=998244353P=998244353.

Input Format

Since the input file is too large to upload, we will read the data in a weird way.

The first line contains two non-negative integers nn and XX, representing the permutation length and the random seed.

Initially let ai=ia_i=i. Next, an integer cc is given in one line, representing the number of operations on the permutation.

Next, we will perform cc operations on the permutation AA. For the ii-th operation (1-indexed), let l=(X×(Xi))modn+1l=(X\times (X\oplus i))\bmod n+1 and r=(X(i×i))modn+1r=(X\oplus(i\times i))\bmod n+1. You need to swap ala_l and ara_r. Here \oplus denotes bitwise XOR.

For C++, the code is as follows:

l = (1ll * X * (X ^ i)) % n + 1;
r = (X ^ (1ll * i * i)) % n + 1;

Next, an integer qq is given in one line, representing the number of queries.

For the ii-th query (1-indexed), let $l=\min((X\times i+(X\oplus (X\times i)))\bmod n,(X-i+(X\oplus (X+i)))\bmod n)+1$ and $r=\max((X\times i+(X\oplus (X\times i)))\bmod n,(X-i+(X\oplus (X+i)))\bmod n)+1$. These are the query parameters.

For C++, the code is as follows:

l = min((1ll * X * i + (X ^ (1ll * X * i))) % n, (X - i + (X ^ (X + i))) % n) + 1;
r = max((1ll * X * i + (X ^ (1ll * X * i))) % n, (X - i + (X ^ (X + i))) % n) + 1;

Output Format

Since the output file is too large to upload, we will output the data in a weird way.

Output one line with one integer, representing the XOR of all answers over the qq queries.

5 3
4
5
998244304
10 114514
191981
3
998244191

Hint

Sample 1 Explanation:

After the operations, A={1,5,4,2,3}A=\{1,5,4,2,3\}.

After decrypting the queries, the real queries are as follows:

4 5
2 3
1 5
3 4
3 5

After decrypting the output, the real output is as follows:

5
998244350
33
1
11

For the first query, S4={4,5}S_4=\{4,5\} and S5={5}S_5=\{5\}. Then S4S4+S4S5+S5S5=5|S_4\cup S_4|+|S_4\cup S_5|+|S_5\cup S_5|=5.

For the second last query, do not forget the terms with 1x<l,r<yn1\le x<l,r<y\le n.

Constraints:

For 20%20\% of the testdata, 1n1001\le n\le 100 and 1q1001\le q\le 100.

For 40%40\% of the testdata, 1n1001\le n\le 100 and 1q1051\le q\le 10^5.

For 60%60\% of the testdata, 1n1051\le n\le 10^5 and 1q1051\le q\le 10^5.

For 80%80\% of the testdata, 1n3×1061\le n\le 3\times10^6 and 1q3×1061\le q\le 3\times10^6.

For 100%100\% of the testdata, 1n1071\le n\le 10^7, 1q1071\le q\le 10^7, 0c1070\le c\le 10^7, 0X1090\le X\le 10^9, and all aia_i are distinct.

All contestants should pay attention to the impact of constant factors.

Translated by ChatGPT 5