From 0247fe4aec42007cb63fd846e742dfb948599cd7 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sun, 20 Nov 2022 11:43:02 +0800 Subject: [PATCH] =?UTF-8?q?P3919=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=8F=AF=E6=8C=81=E4=B9=85=E5=8C=96=E7=BA=BF=E6=AE=B5=E6=A0=91?= =?UTF-8?q?=201=EF=BC=88=E5=8F=AF=E6=8C=81=E4=B9=85=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E7=BB=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/94757324 --- Luogu/P3919/P3919.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Luogu/P3919/P3919.cpp diff --git a/Luogu/P3919/P3919.cpp b/Luogu/P3919/P3919.cpp new file mode 100644 index 00000000..eff7b0f5 --- /dev/null +++ b/Luogu/P3919/P3919.cpp @@ -0,0 +1,100 @@ +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n, m, a[N], root[N], cnt; + +struct node { + int l, r, v; +} tr[N << 6]; + +int build(int l, int r) { + int p = ++cnt; + + if (l == r) { + tr[p].v = a[l]; + + return p; + } + + int mid = l + r >> 1; + + tr[p].l = build(l, mid); + tr[p].r = build(mid + 1, r); + + return p; +} + +int modify(int u, int l, int r, int p, int v) { + int q = ++cnt; + + tr[q] = tr[u]; + + if (l == r) { + a[p] = v; + tr[q].v = v; + + return q; + } + + int mid = l + r >> 1; + + if (p <= mid) { + tr[q].l = modify(tr[u].l, l, mid, p, v); + } else { + tr[q].r = modify(tr[u].r, mid + 1, r, p, v); + } + + return q; +} + +int query(int u, int l, int r, int p) { + if (l == r) return tr[u].v; + + int mid = l + r >> 1; + + if (p <= mid) { + return query(tr[u].l, l, mid, p); + } + + return query(tr[u].r, mid + 1, r, p); +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + root[0] = build(1, n); + + for (int i = 1, ver, op; i <= m; i++) { + cin >> ver >> op; + + if (op == 1) { + int p, v; + + cin >> p >> v; + + root[i] = modify(root[ver], 1, n, p, v); + } else { // op == 2 + int p; + + cin >> p; + + cout << query(root[ver], 1, n, p) << endl; + + root[i] = root[ver]; + } + } + + return 0; +}