Under construction

Still being built. Things may change

Version-Controlled IDE

Time limit1sMemory limit128 MB

Summary
Maintain a versioned text buffer under insert and delete operations, answering substring queries against any past version, with all commands encoded by a running counter.
Level

Hard9 of 10

Topics
Tree, Implementation, String, Brute force
Solved
No attempts yet

Problem

Many programmers use a version control system to manage the files in a project. Such systems have a drawback, however: a version is recorded only when the user explicitly saves.

Here you will implement an IDE that automatically saves a new version every time a string is inserted or deleted.

Positions in the buffer are numbered from left to right starting at 1. Initially the buffer is empty and the version number is 0.

Let L[v]L[v] be the length of the buffer at version vv, and let vnowv_{now} be the version number just before a command is executed. The IDE supports three commands:

  • 1 p s: insert the string ss right after position pp (0≤p≤L[vnow]0 \le p \le L[v_{now}]). If p=0p = 0, insert at the very front of the buffer. The length of ss is between 1 and 100.
  • 2 p c: delete cc characters starting at position pp (p≥1p \ge 1, p+c≤L[vnow]+1p + c \le L[v_{now}] + 1).
  • 3 v p c: output cc characters starting at position pp in version vv (p≥1p \ge 1, p+c≤L[v]+1p + c \le L[v] + 1).

The first command is always a type-1 command. Executing a type-1 or type-2 command increases the version by 1; a type-3 command does not change the version.

Input

The first line contains the number of commands TT (1≤T≤50 0001 \le T \le 50\,000). Each of the next TT lines contains one command. The total length of all inserted strings does not exceed 1 000 0001\,000\,000.

To prevent preprocessing the input, each command is encoded. Let dd be the number of lowercase c characters printed so far (initially d=0d = 0).

  • A type-1 command is given as 1 p+d s,
  • a type-2 command is given as 2 p+d c+d,
  • a type-3 command is given as 3 v+d p+d c+d.

So after reading each command, subtract dd from every numeric argument to recover the real values pp, cc, and vv.

Output

Every time a type-3 command is executed, print its resulting string on its own line. The total length of the printed strings does not exceed 200 000200\,000.

Examples1

  1. Example 1

    Input
    6
    1 0 abcdefgh
    2 4 3
    3 1 2 5
    3 3 3 4
    1 4 xy
    3 5 4 6
    
    Expected output
    bcdef
    bcg
    bxyc