-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3919.cpp
114 lines (103 loc) · 1.79 KB
/
P3919.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdio>
#include <cstring>
using namespace std;
int read()
{
int x=0, t=1;
char c = getchar();
while(c<'0' || c>'9')
{
if(c == '-')
t = -1;
c = getchar();
}
while(c>='0' && c<='9')
{
x = x*10 + c-'0';
c = getchar();
}
return x*t;
}
constexpr int MAXN = 1e6+5;
struct Node
{
int ls, rs;
int val;
}Tree[MAXN*40];
int rt[MAXN];
int cnt;
int N,M;
int buildTree(int l, int r)
{
int root = ++cnt;
if(l == r)
{
Tree[root].val = read();
}
else
{
int mid = (l+r)>>1;
Tree[root].ls = buildTree(l, mid);
Tree[root].rs = buildTree(mid+1, r);
}
return root;
}
int modify(int l, int r, int pre, int pos, int val)
{
int root = ++cnt;
Tree[root] = Tree[pre];
if(l == r)
{
Tree[root].val = val;
}
else
{
int mid = (l+r)>>1;
if(pos <= mid)
Tree[root].ls = modify(l, mid, Tree[pre].ls, pos, val);
else
Tree[root].rs = modify(mid+1, r, Tree[pre].rs, pos, val);
}
return root;
}
int query(int l, int r, int root, int pos)
{
if(l == r)
{
return Tree[root].val;
}
int mid = (l+r)>>1;
if(pos<=mid)
return query(l, mid, Tree[root].ls, pos);
else
return query(mid+1, r, Tree[root].rs, pos);
}
void init()
{
N = read();
M = read();
rt[0] = buildTree(1, N);
}
void work()
{
int v,op,loc,val;
for(int i=1; i<=M; ++i)
{
v = read();op = read();loc = read();
if(op == 1)
{
val = read();
rt[i] = modify(1, N, rt[v], loc, val);
}
else
{
rt[i] = rt[v];
printf("%d\n", query(1, N, rt[i], loc));
}
}
}
int main()
{
init();
work();
}