Making Number
Time limit1.5sMemory limit1024 MB
Given X and a Y whose digits change one at a time, answer queries for a digit of the smallest rearrangement of X's digits, with no leading zero, that is at least Y.
- Level
Medium7 of 10
- Topics
- Segment tree, Greedy, String
- Solved
- No attempts yet
Problem
You are given two positive integers and with the same number of digits in base 10. is the positive integer in base 10 that satisfies all of the following conditions.
- The digits of are a rearrangement of the digits of . The first digit of cannot be 0. For example, if , can be or , but cannot be , , or .
- .
- is the minimum value that satisfies the conditions above.
Process queries. Each query is one of the following two types.
- Change the -th digit of to .
- Output the -th digit of . If no such exists, output .
Digits are counted from the left.
Input
The first line contains two integers and , separated by a space.
The second line contains .
Each of the next lines is a query in one of the following forms. The first integer is the query type.
1: Change the -th digit of to .2: Output the -th digit of . If no such exists, output .
At least one query of type 2 is given.
Output
For each query of type 2, print the answer on its own line.
Constraints
and have the same number of digits. The first digits of and are not 0. For a query of type 1, the number of digits of and . If , then . For a query of type 2, the number of digits of .