Under construction

Still being built. Things may change

Greedy

Time limit2sMemory limit1024 MB

Summary
Split a digit string into the fewest substrings, each forming a number at most x, and report the minimum after each single-digit update.
Level

Medium7 of 10

Topics
Greedy, Segment tree
Solved
No attempts yet

Statement

James Bond has always been saved by new gadgets designed by a whole team at MI-6. Few people wonder how the geniuses of that department spend their time.

Q is one of the people who supply Bond with new weapons and keep the MI-6 computer network secure. When Q gets bored, he solves programming olympiad problems. His method is somewhat unusual.

Q knows that in real-life problems, all input data is fairly random and uniformly distributed over the values it can take. He also knows that real problems often have a solution that is some kind of greedy algorithm.

When Q gets a new olympiad problem, he first writes a solution right after reading the statement. It gives correct answers for small parameters but does not fit the time limit. Then he generates many tests completely at random, gets their answers from that solution, and builds a greedy algorithm that passes these tests and runs in the required time. He submits the new solution to the server and gets Accepted.

Yesterday Petya solved the following problem.

You are given a string of digits from 1 to 9 and an integer x>9x > 9. Split this string into the smallest possible number of substrings such that each substring, read as a decimal number yy, satisfies y≤xy \le x.

Q wrote a greedy that gave correct answers on his tests and ran in O(n)O(n), where nn is the length of the string. When he finished reading the statement, he found an extra requirement. The problem also asks to process queries that change one digit of the string, and after each query the new answer must be printed. Q's greedy is too slow for these limits. He thought for five more minutes, wrote a faster greedy, and got Accepted.

Solve this same problem.

Input

The first line contains a string of digits from 1 to 9 of length n≤105n \le 10^5.

The second line contains two integers mm and xx (0≤m≤1050 \le m \le 10^5, 10≤x≤10910 \le x \le 10^9), where mm is the number of single-digit change queries and xx is the bound that every substring must not exceed.

Each of the next mm lines contains two integers aia_i and bib_i (1≤ai≤n1 \le a_i \le n, 1≤bi≤91 \le b_i \le 9). The digit at position aia_i is changed to bib_i.

Output

Print the minimum number of substrings for the original string on the first line.

Then print the answer after each of the mm queries is applied, one answer per line.

Examples1

  1. Example 1

    Input
    123123123123
    3 123
    3 1
    1 2
    3 9
    
    Expected output
    4
    4
    5
    5