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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #pragma GCC optimize ("O2") #include <cstdio> #include <cmath> #include <vector> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long LL; const int N = 25e4 + 10, M = 510; LL A[N], s[M], f[N]; int n, q, m, C[N]; int p[N]; int B, tot, id[N], L[N], R[N]; short c[M][N]; vector <int> op[M]; struct OP { int op, l, r, a, b; } o[N]; void rebuild (int x) { for (int i = L[x]; i <= R[x]; ++i) c[x][C[i]] = 0; reverse(op[x].begin(), op[x].end()); for (int i : op[x]) if (o[i].op == 1) p[o[i].a] = p[o[i].b], f[o[i].a] = f[o[i].b], c[x][o[i].b] = 0; else if (o[i].op == 2) f[o[i].a] += o[i].b; for (int i = L[x]; i <= R[x]; ++i) A[i] += f[C[i]], ++c[x][C[i] = p[C[i]]]; for (int i : op[x]) p[o[i].a] = o[i].a, f[o[i].a] = 0; op[x].clear(); } void modify (int l, int r, int x, int y, int t) { if (id[l] == id[r]) { rebuild(id[l]); for (int i = l; i <= r; ++i) if (C[i] == x) --c[id[l]][x], ++c[id[l]][C[i] = y]; return; } rebuild(id[l]), rebuild(id[r]); for (int i = l; i <= R[id[l]]; ++i) if (C[i] == x) --c[id[l]][x], ++c[id[l]][C[i] = y]; for (int i = id[l] + 1; i < id[r]; ++i) { c[i][y] += c[i][x], c[i][x] = 0; op[i].push_back(t); } for (int i = L[id[r]]; i <= r; ++i) if (C[i] == x) --c[id[r]][x], ++c[id[r]][C[i] = y]; } void add (int l, int r, int x, int v, int t) { if (id[l] == id[r]) { rebuild(id[l]); for (int i = l; i <= r; ++i) if (C[i] == x) A[i] += v, s[id[l]] += v; return; } rebuild(id[l]), rebuild(id[r]); for (int i = l; i <= R[id[l]]; ++i) if (C[i] == x) A[i] += v, s[id[l]] += v; for (int i = id[l] + 1; i < id[r]; ++i) { s[i] += (LL)v * c[i][x]; op[i].push_back(t); } for (int i = L[id[r]]; i <= r; ++i) if (C[i] == x) A[i] += v, s[id[r]] += v; } LL query (int l, int r) { LL res = 0; if (id[l] == id[r]) { rebuild(id[l]); for (int i = l; i <= r; ++i) res += A[i]; return res; } rebuild(id[l]), rebuild(id[r]); for (int i = l; i <= R[id[l]]; ++i) res += A[i]; for (int i = id[l] + 1; i < id[r]; ++i) res += s[i]; for (int i = L[id[r]]; i <= r; ++i) res += A[i]; return res; } int main () { read(n, q, m); for (int i = 1; i <= n; ++i) read(A[i]); for (int i = 1; i <= n; ++i) read(C[i]); B = sqrt(n); for (; R[tot] < n; ++tot) L[tot + 1] = R[tot] + 1, R[tot + 1] = R[tot] + B; R[tot] = n; for (int i = 1; i <= tot; ++i) for (int j = L[i]; j <= R[i]; id[j++] = i) s[i] += A[j], ++c[i][C[j]]; for (int i = 1; i <= m; ++i) p[i] = i; for (int i = 1; i <= q; ++i) { read(o[i].op, o[i].l, o[i].r); if (o[i].op == 1) { read(o[i].a, o[i].b); modify(o[i].l, o[i].r, o[i].a, o[i].b, i); } else if (o[i].op == 2) { read(o[i].a, o[i].b); add(o[i].l, o[i].r, o[i].a, o[i].b, i); } else if (o[i].op == 3) write(query(o[i].l, o[i].r)), puts(""); } return 0; }
|