社区讨论
蒟蒻求助莫队20pts
P2464[SDOI2008] 郁闷的小 J参与者 2已保存回复 2
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 2 条
- 当前快照
- 1 份
- 快照标识符
- @lo7ma64a
- 此快照首次捕获于
- 2023/10/27 04:08 2 年前
- 此快照最后确认于
- 2023/11/25 11:19 2 年前
CPP
#include <iostream>
#include <algorithm>
#include <cmath>
const int sz = 1e5 + 10;
int arr[sz], xarr[sz << 1], cxarr[sz << 1], cnt[sz << 1], npp, qpp, tpp, ans[sz];
struct query {
int l, r, id, bl, br, t, num;
bool operator<(const query &a) const {
if (bl != a.bl) return l < a.l;
if (br != a.br) {
if (bl & 1) return r < a.r;
return r > a.r;
}
if (br & 1) return t < a.t;
return t > a.t;
}
} que[sz];
struct modify {
int pos, val;
} change[sz];
void Change(int t, int ln, int rn) {
int pos = change[t].pos, &val = change[t].val;
if (pos >= ln && pos <= rn) cnt[arr[pos]]--, cnt[val]++;
std::swap(arr[pos], val);
}
int main() {
std::ios::sync_with_stdio(false);
int n, q;
std::cin >> n >> q;
int lim = std::pow(n, 2. / 3.);
for (int i = 1; i <= n; i++) std::cin >> arr[i], xarr[++npp] = arr[i];
for (int i = 1; i <= n; i++) {
char op;
int x, y, val;
std::cin >> op >> x >> y;
if (op == 'Q')
std::cin >> val, que[++qpp] = query{x, y, qpp, (x - 1) / lim + 1, (y - 1) / lim + 1, tpp, val}, xarr[++npp] = val;
else change[++tpp] = modify{x, y}, xarr[++npp] = y;
}
std::sort(que + 1, que + qpp + 1);
std::copy(xarr + 1, xarr + npp + 1, cxarr + 1);
int f = std::unique(cxarr + 1, cxarr + npp + 1) - cxarr;
for (int i = 1; i <= n; i++)
arr[i] = std::lower_bound(cxarr + 1, cxarr + f, arr[i]) - cxarr;
for (int i = 1; i <= qpp; i++)
que[i].num = std::lower_bound(cxarr + 1, cxarr + f, que[i].num) - cxarr;
for (int i = 1; i <= tpp; i++)
change[i].val = std::lower_bound(cxarr + 1, cxarr + f, change[i].val) - cxarr;
auto add = [](int x) { cnt[arr[x]]++; };
auto del = [](int x) { cnt[arr[x]]--; };
int l = 1, r = 0, t = 0;
for (int i = 1; i <= qpp; i++) {
int le = que[i].l, re = que[i].r, te = que[i].t;
while (l > le) add(--l);
while (r < re) add(++r);
while (l < le) del(l++);
while (r > re) del(r--);
while (t < te) Change(++t, le, re);
while (t > te) Change(t--, le, re);
ans[que[i].id] = cnt[que[i].num];
}
for (int i = 1; i <= qpp; i++)
std::cout << ans[i] << "\n";
return 0;
}
回复
共 2 条回复,欢迎继续交流。
正在加载回复...