luogu#P5310. [Ynoi2011] 遥远的过去

    ID: 3444 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2011平衡树O2优化哈希 hashing洛谷月赛Ynoi

[Ynoi2011] 遥远的过去

Background

When I was in the 6th grade, I learned Java and wrote a small game. It basically flung my math olympiad teacher’s head to hit frogs. Looks like I already understood mouha and P-head back then.

Now I feel I didn’t understand a lot at the time and was just writing randomly. The program was full of if’s, yet I still finished it and even won a first prize in a science and innovation contest (it could add 5 points to the high school entrance exam, definitely not shady!).

So I’ve always felt I actually like doing creative things. This will show up later too.

In middle school, because I had learned some elementary olympiad math and also some math contest stuff, I felt the classes were plain and wasn’t very interested.

I first encountered the informatics olympiad. It felt quite interesting, but I didn’t study it seriously.

Our school had OI training, but they taught Pascal. Since I had already learned Java, I naturally thought that language was outdated and trash, so I didn’t really listen. I remember I took a selection test and there was a “chicken and rabbit in the same cage” problem. I wrote a solution using equations and got points deducted. The teacher’s official solution was something like while( a-- ) b++. Then I decided not to go anymore.

In the 8th grade I took the NOIP Junior group and only got 120. The second problem was expression evaluation. I spent a long time on it and kept failing, then that was it.

After the exam I learned that several classmates were top 10 in the province. They seemed really strong. I was still very competitive back then, so I decided to study OI well and crush them in 9th grade.

In 9th grade I got 295 and got crushed by two 8th graders at the time, but I was the highest in 9th grade.

Those two kids seem to have gone to Cha Yuan now. Truly legends.

Middle school felt quite fun. Although I had to play mind games with a toxic teacher every day, it was still interesting.

[Remember to add images. Content: a screenshot of the small game, and the NOIP score.]

Since this is Ynoi, not a place for the problem setter to write strange essays, you now need to do a data structure problem:

Problem Description

Little F decides to design a language with a huge character set — the Z language — even if extra characters are sometimes useless.

Features of this language:

  • The character set is extremely large, possibly with 2147483648 (231)2147483648\ (2^{31}) distinct characters.
  • Each word is made of a sequence of pairwise distinct characters.
  • Characters can be compared for equality and order, so we will use numbers to represent the strange characters in the Z language.
  • Two words that look completely different can still be the same word because: as long as the positions of the KK-th largest characters are the same in both words for every KK, then they are essentially the same word. For example, {1,2,3,4,5}\{1, 2, 3, 4, 5\} and {2,3,23,233,23333}\{2, 3, 23, 233, 23333\} are the same. (So you can conveniently use the Z language to encrypt information!)

Now, Little F plans to apply the Z language to a real task. For example, he opens an algorithm problem on a computer:

Given two strings AA and BB, find how many times BB is matched as a substring in AA.

Little F knows this is a basic problem solvable with KMP. However, when implementing Z-KMP for matching in the Z language, he ran into trouble. Can you help him?

To verify you really understand what Little F means, he will modify string BB many times and ask you. No slacking!

See the input and output formats for the operations your program must support.

Input Format

The first line contains two integers n,m,qn, m, q (1n,m,q1051 \leq n, m, q \leq 10^5), the lengths of strings AA and BB, and the number of operations.

The second line contains nn non-negative integers. The ii-th is the ii-th character AiA_i of string AA (0Ai2147483647=23110 \leq A_i \leq 2147483647 = 2^{31}-1).

The third line contains mm non-negative integers. The ii-th is the ii-th character BiB_i of string BB (0Bi2147483647=23110 \leq B_i \leq 2147483647 = 2^{31}-1).

Then follow qq lines. Each line contains two positive integers xi,cix_i, c_i (1ci2147483647=23111 \leq c_i \leq 2147483647 = 2^{31}-1), meaning the character at position xix_i in BB is changed from BxiB_{x_i} to cic_i.

The testdata guarantees that at any time, both AA and BB are valid Z strings that satisfy the above requirements.

Output Format

After each modification, output the number of times BB is Z-matched as a substring in AA.

5 3 2
11 7 5 3 2
3 2 1
2 5
1 6

0
3

Hint

Idea: nzhtl1477, Solution: nzhtl1477, Code: nzhtl1477, Data: nzhtl1477 (partially uploaded).

Sample 1 Explanation:

  • After the first modification, {3,5,1}\{3, 5, 1\} cannot be matched by any substring of AA.
  • After the second modification, {6,5,1}\{6, 5, 1\} can be matched by every length-33 substring of AA, because AA is strictly decreasing and BB is also strictly decreasing, so positions of characters with the same ranks coincide.

Subtasks:

  • Subtask 11 (31 pts): n,m100n, m \leq 100, q1000q \leq 1000.
  • Subtask 22 (41 pts): n,m1000n, m \leq 1000, q5×104q \leq 5 \times 10^4.
  • Subtask 33 (78 pts): n,m,q105n, m, q \leq 10^5.

Translated by ChatGPT 5