Files
python/TangDou/AcWing/SegmentTree/P3919.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

99 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1000010;
struct Node {
int l, r, v;
} tr[N << 5];
int root[N], idx;
int n, m, a[N];
//快读
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
// build的原因,是因为有初始值,版本0不是空的
void build(int &u, int l, int r) {
u = ++idx;
if (l == r) {
tr[u].v = a[l];
return;
}
int mid = (l + r) >> 1;
build(tr[u].l, l, mid);
build(tr[u].r, mid + 1, r);
}
void update(int &u, int l, int r, int x, int v) {
tr[++idx] = tr[u];
u = idx;
if (l == r) {
tr[idx].v = v;
return;
}
int mid = (l + r) >> 1;
if (mid >= x)
update(tr[u].l, l, mid, x, v);
else
update(tr[u].r, mid + 1, r, x, v);
}
int query(int u, int l, int r, int x) {
if (l == r) return tr[u].v;
int mid = (l + r) >> 1;
if (mid >= x)
return query(tr[u].l, l, mid, x);
else
return query(tr[u].r, mid + 1, r, x);
}
/*
答案:
59
87
41
87
88
46
*/
int main() {
//文件输入输出
#ifndef ONLINE_JUDGE
freopen("P3919.in", "r", stdin);
#endif
n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
//构建主席树,因为有初始化值a[i]
build(root[0], 1, n);
for (int i = 1; i <= m; i++) {
int x = read(), op = read(), y = read();
if (op == 1) {
// op=1:修改 x这个版本,管辖范围[1,n],将a[y]的值,即y这个位置,修改为v
int v = read();
root[i] = root[x];
update(root[i], 1, n, y, v);
} else {
// op=2:访问版本x中,y这个位置上的值
root[i] = root[x];
printf("%d\n", query(root[i], 1, n, y));
}
}
return 0;
}