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
| #include <cstdio> using namespace std; const int N = 1e6 + 10, M = 1e6 + 10; struct Node { int l, r, v; } tr[4 * N + 20 * M]; int idx; int n, m, w[N], rt[N]; void build(int &x, int l, int r) { x = ++idx; if (l == r) { tr[x].v = w[l]; return; } int mid = l + r >> 1; build(tr[x].l, l, mid); build(tr[x].r, mid + 1, r); } void modify(int &x, int l, int r, int t, int k) { tr[++idx] = tr[x]; x = idx; if (l == r) { tr[x].v = k; return; } int mid = l + r >> 1; if (t <= mid) modify(tr[x].l, l, mid, t, k); else modify(tr[x].r, mid + 1, r, t, k); } int query(int x, int l, int r, int t) { if (l == r) return tr[x].v; int mid = l + r >> 1; if (t <= mid) return query(tr[x].l, l, mid, t); return query(tr[x].r, mid + 1, r, t); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); build(rt[0], 1, n); for (int i = 1, v, op, t; i <= m; i++) { scanf("%d%d%d", &v, &op, &t); rt[i] = rt[v]; if (op == 1) { int k; scanf("%d", &k); modify(rt[i], 1, n, t, k); } else if (op == 2) printf("%d\n", query(rt[i], 1, n, t)); } return 0; }
|