题意
给定一个大小为 \(n\) 的序列,每个点属于一个编号为 \(c_i\) 的环,有一个权值为 \(s_i\)。
维护这若干个环:
- 询问 \(\sum_{i = l} ^ r s_i\)。
- 将编号为 \(x\) 的环上所有点的权值逆时针旋转一次。
\(n, q \le 1.5 \times 10 ^ 5 \texttt{32MB}\)。
Sol
考虑根号分治。
对于 \(siz \le \sqrt n\) 的环,直接暴力移位修改,然后上一个分块维护区间和即可。
对于 \(siz > \sqrt n\) 的环,对于每个环维护前缀和。
询问变成求 \([l, r]\) 里每个大环的前缀和,需要使用二分查找下一个环上的点。
考虑预处理这个东西,但是发现预处理的空间是 \(n \sqrt n\) 的,炸了。
于是离线下来,先将小块的答案算好,对于每个大块先预处理 \([1, n]\) 的 \(\texttt{lower_bound}\),然后枚举所有询问与修改,直接前缀和查询即可。
时间复杂度 \(O(n \sqrt n)\),空间复杂度 \(O(n)\)。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <vector>
#define ll long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;#endif
int read() {int p = 0, flg = 1;char c = getchar();while (c < '0' || c > '9') {if (c == '-') flg = -1;c = getchar();}while (c >= '0' && c <= '9') {p = p * 10 + c - '0';c = getchar();}return p * flg;
}
void write(ll x) {if (x < 0) {x = -x;putchar('-');}if (x > 9) {write(x / 10);}putchar(x % 10 + '0');
}
bool _stmer;#define fi first
#define se secondconst int N = 3e5 + 5, bsk = 427;array <vector <int>, N> col;
array <int, N> s, h;array <ll, N> ans, bk;array <pii, N> qrl;int calc(int x) { return (x - 1) / bsk + 1; }void modify(int x, int val) { bk[calc(x)] += val * s[x]; }ll query(int l, int r) {ll res = 0;if (calc(l) == calc(r)) {for (int i = l; i <= r; i++)if ((int)col[h[i]].size() <= bsk) res += s[i];return res;}for (int i = l; i <= calc(l) * bsk; i++)if ((int)col[h[i]].size() <= bsk) res += s[i];for (int i = (calc(r) - 1) * bsk + 1; i <= r; i++)if ((int)col[h[i]].size() <= bsk) res += s[i];for (int i = calc(l) + 1; i < calc(r); i++) res += bk[i];return res;
}array <int, N> isl;
array <ll, N> pre;bool _edmer;
int main() {cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";int n = read(), m = read(), q = read();for (int i = 1; i <= n; i++)h[i] = read(), col[h[i]].push_back(i);vector <int> arc;for (int i = 1; i <= m; i++)if ((int)col[i].size() > bsk) arc.push_back(i);for (int i = 1; i <= n; i++) {s[i] = read();if ((int)col[h[i]].size() <= bsk)modify(i, 1);}for (int i = 1; i <= q; i++) {int op = read();if (op == 1) {int l = read(), r = read();qrl[i] = make_pair(l, r);ans[i] += query(l, r);}else {int x = read();qrl[i] = make_pair(-1, x);if ((int)col[x].size() <= bsk) {for (int j = (int)col[x].size() - 1; j; j--) {modify(col[x][j], -1), modify(col[x][j - 1], -1);swap(s[col[x][j]], s[col[x][j - 1]]);modify(col[x][j], 1), modify(col[x][j - 1], 1);}}}}for (auto k : arc) {int res = 0, lst = 0, len = (int)col[k].size();for (int i = 1; i <= n; i++) {while (col[k][lst] <= i && lst < len) lst++;isl[i] = lst;/* cerr << isl[i] << " "; */}/* cerr << "@" << endl; */for (int i = 0; i < 2 * len; i++)pre[i + 1] = pre[i] + s[col[k][i % len]];pre[2 * len + 1] = pre[2 * len];for (int i = 1; i <= q; i++) {if (!~qrl[i].fi)(res += qrl[i].se == k) %= len;else {ans[i] += pre[isl[qrl[i].se] + len - res]- pre[isl[qrl[i].fi - 1] + len - res];}}}for (int i = 1; i <= q; i++)if (~qrl[i].fi) write(ans[i]), puts("");return 0;
}