#P11657. 「FAOI-R5」datealive

    ID: 11769 远端评测题 5000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树2025洛谷原创O2优化洛谷比赛

「FAOI-R5」datealive

Background

Problem Description

Given a sequence c1,c2,,cnc_1,c_2,\cdots,c_n of length nn, where ci{0,1}c_i\in\{0,1\}. Each element corresponds to a parenthesis: if ci=0c_i=0, the parenthesis at this position is a left parenthesis; if ci=1c_i=1, it is a right parenthesis.

Let f(l,r)f(l,r) denote whether the parenthesis sequence in the interval [l,r][l,r] is valid. If the sequence formed by cl,cl+1,,crc_l,c_{l+1},\cdots,c_r is a valid parenthesis sequence, then f(l,r)=1f(l,r)=1; otherwise, f(l,r)=0f(l,r)=0.

Definition of a valid parenthesis sequence:

  • The empty string is a valid parenthesis sequence.
  • If A is a valid parenthesis sequence, then (A) is a valid parenthesis sequence.
  • If A and B are valid parenthesis sequences, then AB is a valid parenthesis sequence.

You need to perform qq operations online, of two types:

  • 1 l r: Query $\max\limits_{[l',r']\in[l,r]}\{(r'-l'+1)\cdot f(l',r')\}$, i.e., query the length of the longest valid parenthesis substring within [l,r][l,r].
  • 2 l r: For all i[l,r]i\in[l,r], modify cic_i to (1ci)(1-c_i), i.e., flip every parenthesis in [l,r][l,r] one by one.

Input Format

The first line contains two positive integers n,qn,q, representing the sequence length and the number of operations.

The next line contains nn non-negative integers c1,c2,cnc_1,c_2\cdots,c_n, representing the initial sequence.

The next qq lines each contain three positive integers op,l,rop,l',r', representing the operation type and the encrypted l,rl,r.

To enforce online processing, the data is encrypted and must be decrypted as follows: let pp be the answer to the previous query operation, or 00 if there is none. Compute l=((l+p)modn)+1l=((l'+p)\bmod n)+1 and r=((r+p)modn)+1r=((r'+p)\bmod n)+1. If l>rl>r, swap l,rl,r.

Here, op,l,rop,l,r are the three parameters of the operation. If op=1op=1, it means the 1 l r operation described above; if op=2op=2, it means the 2 l r operation described above.

Output Format

For each query, output one line with a non-negative integer representing the answer.

10 10
0 1 1 0 0 1 0 0 1 1 
2 7 8
2 10 5
2 5 1
2 7 6
2 7 1
2 4 5
2 3 4
1 5 2
1 4 1
1 7 7
4
2
0
20 20
0 0 0 1 1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 
2 16 7
2 12 10
1 4 5
1 14 7
1 16 7
2 15 12
1 12 2
1 3 4
1 1 10
2 14 3
2 19 1
1 20 9
2 14 16
1 10 13
1 6 8
2 1 2
1 8 16
1 9 15
1 17 12
2 15 14
0
2
8
2
0
2
6
4
0
2
0
0

Hint

Sample 1 Explanation

The decrypted result is as follows:

10 10
0 1 1 0 0 1 0 0 1 1 
2 8 9
2 1 6
2 2 6
2 7 8
2 2 8
2 5 6
2 4 5
1 3 6
1 6 9
1 10 10

After 77 modifications, the parenthesis string becomes )((())()())((())()().

  • For the first query, take the substring [3,6][3,6] to get (())(()). The whole substring is a valid parenthesis sequence, so the answer is 63+1=46-3+1=4.
  • For the second query, take the substring [6,9][6,9] to get )()()()(. The substring [7,8][7,8] is the longest valid parenthesis substring within it, so the answer is 87+1=28-7+1=2.
  • For the third query, take the substring [10,10][10,10] to get )). The valid parenthesis substring within this substring is the empty string, so the answer is 00.

Constraints and Notes

This problem uses bundled testdata.

Subtask ID nn \le qq \le Score Special Property
11 100100 1010 ×
22 2×1042 \times 10^4
33 4×1064 \times 10^6 10510^5 2020 \checkmark
44 10510^5 3030 ×
55 4×1064 \times 10^6

Special property: it is guaranteed that there are no modification operations.

For all testdata, 1n4×1061 \le n\le 4 \times 10^6, 1q1051\le q \le 10^{5}, 1l,rn1 \le l',r' \le n, op{1,2}op\in\{1,2\}, and ci{0,1}c_i\in\{0,1\}.

For this problem, except for Subtask #3, it is guaranteed that the operation type is generated randomly, i.e., each time a random number from {1,2}\{1,2\} is chosen (with probability 50%50\% for both 11 and 22) as opop.

A large sample is provided in the problem attachments.

Translated by ChatGPT 5