#P10796. 『SpOI - R1』我看到了,谢谢你们

    ID: 11678 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>贪心线段树O2优化树链剖分KMP 算法

『SpOI - R1』我看到了,谢谢你们

Problem Description

This problem contains multiple test cases.

Special note: in this problem, the definition of border is different. For strings s,ts,t, if there exists a prefix and a suffix of ss (they may be empty, and may also be ss itself) that are equal to tt, then tt is a border of ss.

There is a string SS of length nn. We use the information on this string to elect the president.

Let pip_i denote the prefix of SS of length ii. In particular, p0p_0 denotes the empty prefix (including the 00-th position). Now there are n+1n+1 candidates standing on these n+1n+1 prefixes, indexed by [0,n][0,n]. Candidate ii corresponds to prefix ii. Each person has a number of votes aia_i and a cost wiw_i.

A person whose number of votes is strictly more than half of the total votes can be elected president.

Initially, everyone is in the uncontrolled state. At each moment, any person ii who is uncontrolled and has been waiting all the time before can make one of the following three choices:

  1. Perform one vote for vv operation: cast their own aia_i votes to person vv at a cost of wiw_i.
  2. Perform one canvass votes for vv operation:
    • Spend wiw_i to select person vv, and it is required that pip_i is a border of pvp_v.
    • For all j[0,n]j\in[0,n], if pvp_v is a border of pjp_j, and jj is uncontrolled at this moment, then jj becomes controlled at the next moment, and all of their aja_j votes are cast to ii at a cost of wjw_j.
  3. Wait for the next moment.

Each candidate hopes that no one else will become the president, and everyone is extremely smart. In particular, when operations overlap and cause one person's votes to need to be cast to multiple people, the overlapped person's votes can be cast independently to each of them and are all valid (you may think of their votes as splitting). Therefore, there may be multiple presidents.

You can intervene in this process. Specifically, at time 00 you may operate on a candidate xx, making xx perform one specified choice, and you may also fix all variables involved in that choice. After that, xx can no longer make any choices, and the remaining people must start choosing from time 11. The cost of your intervention is the total cost of this choice made by xx.

Votes aa and costs ww will change qq times.

Each change modifies one entry of aa or one entry of ww. A vote count aa may become any positive integer, and a cost ww will only decrease or stay the same.

After each change, you need to find a person xx such that there exists a way for you to intervene on them so that they are guaranteed to become president, and the intervention cost is minimized. You only need to output this minimum cost.

It can be proven that such a person always exists.

This problem is forced online.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case:

One line contains three integers n,q,typen,q,type, denoting the string length, the number of modifications, and whether it is forced online.

The next line contains a string SS of length nn. It is guaranteed that SS contains only lowercase English letters.

The next n+1n+1 lines each contain two integers ai,wia_i,w_i, in order describing the initial votes and costs of candidates 00 to nn.

The next qq lines each contain three integers o,p,xo',p',x'. Let lstlst be the absolute value of the previous output answer. Then $o\gets o'\oplus(type\times lst),p\gets p'\oplus(type\times lst),x\gets (|x'|\oplus(type\times lst))\times (-1)^{[x'\leq 0]}$ (where [x0][x'\leq 0] is the Iverson bracket), and then:

  1. When o=1o=1, modify apa_p to xx.
  2. When o=2o=2, modify wpw_p to wp=xw_p'=x. It is guaranteed that wpwpw_p\geq w_p'.

Output Format

Output q+1q+1 lines in total.

The first line outputs the minimum intervention cost in the initial state.

After that, output the minimum intervention cost after each modification, for a total of qq lines.

3
2 1 0
aa
6 1
2 -1
3 2
1 0 2
19 0 0
happythbirthdayshun
1000000000 8
1000000000 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 1
1 0
1 0
5 6 1
acbac
1 3
2 4
1 -5
3 6
2 -3
3 1
11 10 12
9 13 108
8 12 8
10 9 0
6 1 4
4 7 1
1
0
17
9
8
-9
8
-4
-5
-5

Hint

Sample #1 Explanation

For the first test case:

Consider the state before the first modification. There are 1111 votes in total, so being elected president requires >5.5>5.5 votes.

Intervene on candidate 00, and choose the first option. After performing one vote for 00 operation with cost w0=1w_0=1, candidate 00 gets 66 votes and directly meets the requirement for president. It can be proven that this is the minimum-cost answer.

After the first modification, there are 77 votes in total, so being elected president requires >3.5>3.5 votes.

Intervene on candidate 11, and choose the second option. After performing one canvass votes for 11 operation, candidate 11 will get 55 votes, and the total cost is 1+(1)+2=0-1+(-1)+2=0. They directly meet the requirement for president. It can be proven that this is the minimum-cost answer.

For the third test case, after removing the forced-online encoding, the modification operations are:

  • o=2,p=3,x=5o=2,p=3,x=5;
  • o=1,p=5,x=100o=1,p=5,x=100;
  • o=1,p=5,x=1o=1,p=5,x=1;
  • o=2,p=1,x=8o=2,p=1,x=-8;
  • o=2,p=5,x=0o=2,p=5,x=0;
  • o=1,p=2,x=4o=1,p=2,x=4.

Constraints

Please note the impact of constant factors on program efficiency.

This problem enables subtask bundling and subtask dependencies.

For 100%100\% of the data, 1T20001\leq T\leq 2000, 1n1051\leq n\leq 10^5, 0q1050\leq q\leq 10^5, 0type10\leq type\leq 1, and it is guaranteed at any time that 1ai2×1091\leq a_i\leq 2\times 10^9, wi2×109|w_i|\leq 2\times 10^9.

It is guaranteed that the string contains only lowercase letters.

For any single modification, it is guaranteed that oo is 11 or 22, and 0pn0\leq p\leq n. When o=1o=1, 1x2×1091\leq x\leq 2\times 10^9; when o=2o=2, 0x2×1090\leq |x|\leq 2\times 10^9.

In particular, each entry in wiw_i is monotone non-increasing during the operations.

Subtask TT\leq n,qn,q\leq ai,wia_i,\lvert w_i\rvert \leq Special Property Score Subtask Dependencies
1 20002000 2020 10510^5 None 55 None
2 200200 1010 1
3 33 10510^5 2×1092\times 10^9 AA 1515 None
4 BB 55
5 CC 1515
6 DD 2020
7 None 3030 1,2,3,4,5,6

Special property AA: it is guaranteed that o2o\neq 2.

Special property BB: each character in the string is independently and uniformly random among the 2626 lowercase letters.

Special property CC: the string contains only a\texttt{a}.

Special property DD: it is guaranteed that type=0type=0.

Translated by ChatGPT 5