JOI Spring Training Camp 2014/15
Original Statement / Submission Site
One of the most important features of a text editor is copy and paste. JOI Corporation is developing a text editor that performs copy-and-paste operations extremely quickly. As an excellent programmer working for JOI Corporation, you have been assigned to test the core copy-and-paste functionality. The future of the company depends on this project, so you want to write an accurate and efficient program.
The detailed specification is as follows. Initially, the contents of the file are the string S. Then, N copy-and-paste operations are performed.
In the i-th operation, the substring between positions Ai and Bi is copied and inserted at position Ci of the original string.
A position x is the location immediately after the first x characters of the string. Thus, position 0 is the beginning of the string. For example, in the string copypaste, position 6 is between the characters a and s, while position 9 is after the final character e, that is, at the end of the string.
If the length of the string exceeds M after an operation, characters are deleted one by one from the right end until the length becomes exactly M.
Your task is to determine in advance the first K characters of the string obtained after all N operations, so that the editor can be tested.
Given the integer K, the maximum string length M, the initial string S, the number N of operations, and the instructions for those operations, determine the first K characters of the resulting string.
The input is given from standard input in the following format:
K M S N A1 B1 C1 A2 B2 C2 ... AN BN CN
K and M. Here, K is the number of characters to output, and M is the maximum allowed length of the string.S, representing the initial contents of the file.N, the number of copy-and-paste operations.N lines contains three integers Ai, Bi, and Ci. In the i-th operation, the substring from position Ai to position Bi is copied and inserted at position Ci.Print one line containing the first K characters of the string obtained after all N operations.
1 ≤ K ≤ 2001 ≤ M ≤ 1000000000S is a lowercase English letter.K ≤ |S| ≤ min(M, 200000)1 ≤ N ≤ 200000Li be the length of the string immediately before the i-th operation. Then:
0 ≤ Ai < Bi ≤ Li0 ≤ Ci ≤ LiM ≤ 2000N ≤ 2000There are no additional constraints.
2 18 copypaste 4 3 6 8 1 5 2 4 12 1 17 18 0
ac
In this example, the N = 4 copy-and-paste operations are performed as follows:
copypaste.ypa from position 3 to position 6 is copied and inserted at position 8, producing copypastypae.opyp from position 1 to position 5 is copied and inserted at position 2, producing coopyppypastypae.yppypast from position 4 to position 12 is copied and inserted at position 1, producing cyppypastoopyppypastypae. Since its length exceeds M = 18, characters are deleted from the right end, leaving cyppypastoopyppypa.a from position 17 to position 18 is copied and inserted at position 0, producing acyppypastoopyppypa. Since its length exceeds M = 18, characters are deleted from the right end, leaving acyppypastoopyppyp.Therefore, the first K = 2 characters of the final string acyppypastoopyppyp are ac, which should be output.
6 100 jjooii 3 5 6 2 4 6 1 1 2 3
joioji