#P5310. [Ynoi2011] 遥远的过去

[Ynoi2011] 遥远的过去

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