PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000219. Mảng lưu phiên bản (Snapshot Array)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mảng lưu phiên bản (Snapshot Array)

Snapshot Array

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An array of length nn has indices 00 through n−1n-1 and initially contains zeros. Operations may modify the current array, save its state as a snapshot, or read an entry from a saved snapshot. Later modifications never affect saved states. Snapshot identifiers start at 0 and increase by one with each save. Process the qq operations in order.

Input

The first line contains n,qn,q. Each of the next qq lines has one of these forms:

  • 1 i v: assign vv to current entry ii.
  • 2: save the current state and return its new snapshot identifier.
  • 3 i s: read entry ii from snapshot ss.

Output

For each operation of type 2 or 3, print the returned identifier or value on its own line. Type 1 produces no output.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤1001\le n,q\le100; 0≤i<n0\le i<n; 0≤v≤1090\le v\le10^9; 0≤s<c0\le s<c, where cc counts earlier type-2 operations; only the three specified operation types occur.
  • Subtask 2 (30 points): 1≤n≤101\le n\le10; 1≤q≤5⋅1041\le q\le5\cdot10^4; 0≤i<n0\le i<n; 0≤v≤1090\le v\le10^9; 0≤s<c0\le s<c, where cc counts earlier type-2 operations; only the three specified operation types occur.
  • Subtask 3 (50 points): 1≤n,q≤5⋅1041\le n,q\le5\cdot10^4; 0≤i<n0\le i<n; 0≤v≤1090\le v\le10^9; 0≤s<c0\le s<c, where cc counts earlier type-2 operations; only the three specified operation types occur.

Examples

Input

3 7
1 0 5
2
1 0 6
3 0 0
2
3 0 1
3 2 1

Output

0
5
1
6
0

Explanation

Save (5,0,0) as snapshot 0. Updating entry 0 to 6 leaves the saved value 5 unchanged. Save (6,0,0) as snapshot 1; reading entries 0 and 2 from it gives 6 and 0.