#P3919. 【模板】可持久化线段树 1(可持久化数组)

    ID: 2856 远端评测题 1500ms 1024MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树平衡树递归O2优化主席树可持久化

【模板】可持久化线段树 1(可持久化数组)

Description

As described, you need to maintain an array of length N N , supporting the following two operations:

  1. Modify the value at some position on a certain historical version.
  2. Access the value at some position on a certain historical version.

In addition, each time you perform an operation, a new version is created. The version number equals the current operation index (starting from 1 1 ; version 0 0 is the initial array).

For operation 2 2 , a completely identical version is created without any changes. That is, the version generated by a query is a copy of the version that is being accessed.

Input Format

The first line contains two positive integers N,M N, M , the array length and the number of operations.

The second line contains N N integers, the initial values of the array (denoted as ai a_i , 1iN 1 \leq i \leq N ).

Each of the next M M lines contains 3 3 or 4 4 integers, representing one of the two operations (assume the current operation is the i i -th):

  1. For operation 1 1 , the format is v 1 p c, meaning: based on version v v , set ap a_p to c c .
  2. For operation 2 2 , the format is v 2 p, meaning: access the value of ap a_p in version v v . Note: the version that is duplicated must be v v .

Output Format

Output several lines, each being the result of an operation 2 2 , in order.

5 10
59 46 14 87 41
0 2 1
0 1 1 14
0 1 1 57
0 1 1 88
4 2 4
0 2 5
0 2 4
4 2 1
2 2 2
1 1 5 91
59
87
41
87
88
46

Hint

Constraints

  • For 30% 30\% of the testdata, 1N,M103 1 \leq N, M \leq 10^3 .
  • For 50% 50\% of the testdata, 1N,M104 1 \leq N, M \leq 10^4 .
  • For 70% 70\% of the testdata, 1N,M105 1 \leq N, M \leq 10^5 .
  • For 100% 100\% of the testdata:
    • 1N,M106 1 \leq N, M \leq 10^6 ;
    • 1pN 1 \leq p \leq N ;
    • Suppose this is the x x -th operation, then 0v<x 0 \leq v < x ;
    • 109ai,c109 -10^9 \leq a_i, c \leq 10^9 .

Sample Explanation

After all operations, a total of 11 11 versions are generated, numbered 010 0 \sim 10 , in order:

Version 0 0 : 59 46 14 87 41. Version 1 1 : 59 46 14 87 41. Version 2 2 : 14 46 14 87 41. Version 3 3 : 57 46 14 87 41. Version 4 4 : 88 46 14 87 41. Version 5 5 : 88 46 14 87 41. Version 6 6 : 59 46 14 87 41. Version 7 7 : 59 46 14 87 41. Version 8 8 : 88 46 14 87 41. Version 9 9 : 14 46 14 87 41. Version 10 10 : 59 46 14 87 91.

Translated by ChatGPT 5