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
| void solve(int l, int r) { LL totval = 0; std::vector<int> all(C); for (int i = 0; i < C; ++ i) all[i] = i + 1; auto pushdown = [&]() { for (int i = l; i <= r; ++ i) col[i] = id[find(i)], a[i] = val[i];
}; auto rebuild = [&]() { tot = n, totval = 0; for (int c : all) rt[c] = 0; all.clear(); for (int i = l; i <= r; ++ i) { if (!rt[col[i]]) id[rt[col[i]] = ++ tot] = col[i], f[tot] = tot, val[tot] = sz[tot] = 0; f[i] = rt[col[i]], val[i] = a[i], sz[rt[col[i]]] ++, totval += a[i]; all.push_back(col[i]); } }; rebuild(); for (int i = 1; i <= Q; ++ i) { if (eve[i].l > r || eve[i].r < l) continue; if (eve[i].op == 1) { if (eve[i].l <= l && eve[i].r >= r) { int x = eve[i].x, y = eve[i].y; if (!rt[x]) continue; all.push_back(x), all.push_back(y); x = find(rt[x]); ++ tot, f[tot] = tot, val[tot] = sz[tot] = 0, id[tot] = y; if (rt[y]) f[rt[y]] = tot, sz[tot] += sz[rt[y]]; f[x] = rt[y] = tot, sz[tot] += sz[x], rt[id[x]] = 0; } else { pushdown(); for (int j = std::max(eve[i].l, l), ed = std::min(eve[i].r, r); j <= ed; ++ j) if (col[j] == eve[i].x) col[j] = eve[i].y; rebuild(); } } else if (eve[i].op == 2) { if (eve[i].l <= l && eve[i].r >= r) { if (!rt[eve[i].x]) continue; all.push_back(eve[i].x); int x = find(rt[eve[i].x]); ++ tot, f[tot] = tot, val[tot] = sz[tot] = 0; val[x] += eve[i].y, totval += (LL) sz[x] * eve[i].y, rt[eve[i].x] = tot; f[x] = tot, sz[tot] = sz[x], id[tot] = eve[i].x; } else { pushdown(); for (int j = std::max(eve[i].l, l), ed = std::min(eve[i].r, r); j <= ed; ++ j) if (col[j] == eve[i].x) a[j] += eve[i].y; rebuild(); } } else { if (eve[i].l <= l && eve[i].r >= r) eve[i].res += totval; else { for (int j = std::max(eve[i].l, l), ed = std::min(eve[i].r, r); j <= ed; ++ j) find(j), eve[i].res += val[j]; } } } }
int main() { read(n, Q, C); for (int i = 1; i <= n; ++ i) read(a[i]); for (int i = 1; i <= n; ++ i) read(col[i]); for (int i = 1; i <= Q; ++ i) { read(eve[i].op, eve[i].l, eve[i].r); if (eve[i].op != 3) read(eve[i].x, eve[i].y); } int B = std::sqrt(n) + 1; for (int l = 1, r; l <= n; l = r + 1) solve(l, r = std::min(l + B - 1, n)); for (int i = 1; i <= Q; ++ i) if (eve[i].op == 3) printf("%lld\n", eve[i].res); return 0; }
|