社区讨论
莫队被卡常,求调
P1903【模板】带修莫队 / [国家集训队] 数颜色 / 维护队列参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @lo2qctvl
- 此快照首次捕获于
- 2023/10/23 18:03 2 年前
- 此快照最后确认于
- 2023/10/23 18:03 2 年前
CPP
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define pre(i, a, b) for(int i = (a); i >= (b); i--)
#define Ede(i, u) for(int i = h[u]; i; i = ne[i])
#define go(i, a) for(auto i : a)
//#define int long long
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define opb pop_back
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
const int inf = 0x3f3f3f3f;
template <typename T> inline void rd(T &x){
x = 0; bool f = true; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = false; ch = getchar();}
while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}
using namespace std;
const int N = 1.4e5, M = 1e6 + 10;
int n, m;
int a[N];
int siz;
struct Qu{
int l, r;
int pre, id;
}qu[N]; int qcnt;
bool cmp(Qu a, Qu b){
if(a.l / siz == b.l / siz){
if(a.r == b.r) return a.pre < b.pre;
else return a.r < b.r;
}
else return a.l < b.l;
}
struct M{
int x, v;
}ops[N]; int mcnt;
int ans[N], res;
int cnt[M];
void Add(int x){
cnt[a[x]]++;
if(cnt[a[x]] == 1) res++;
}
void Sub(int x){
cnt[a[x]]--;
if(!cnt[a[x]]) res--;
}
void modify(int rt, int l, int r){
if(l <= ops[rt].x && ops[rt].x <= r){
cnt[a[ops[rt].x]]--;
if(!cnt[a[ops[rt].x]]) res--;
cnt[ops[rt].v]++;
if(cnt[ops[rt].v] == 1) res++;
}
swap(ops[rt].v, a[ops[rt].x]); //只将v的值交换了,没有交换x
}
int main(){
/*
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
*/
rd(n, m);
siz = (int)pow(n, 2.0 / 3);
rep(i, 1, n) rd(a[i]);
char ins[2];
rep(i, 1, m){
sf("%s", ins);
int l, r;
rd(l, r);
if(ins[0] == 'Q') ++qcnt, qu[qcnt] = {l, r, mcnt, qcnt};
else ops[++mcnt] = {l, r};
}
sort(qu + 1, qu + qcnt + 1, cmp);
for(int i = 1, l = qu[1].l, r = qu[1].l - 1, rt = 0; i <= m; i++){
while(l < qu[i].l) Sub(l++);
while(r > qu[i].r) Sub(r--);
while(l > qu[i].l) Add(--l);
while(r < qu[i].r) Add(++r);
while(rt < qu[i].pre) modify(++rt, l, r);
while(rt > qu[i].pre) modify(rt--, l, r);
ans[qu[i].id] = res;
}
rep(i, 1, qcnt) prf("%d\n", ans[i]);
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...