社区讨论
求助,40pts
P1903【模板】带修莫队 / [国家集训队] 数颜色 / 维护队列参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lsyrkqm4
- 此快照首次捕获于
- 2024/02/23 22:45 2 年前
- 此快照最后确认于
- 2024/02/23 22:56 2 年前
CPP
#include<bits/stdc++.h>
using namespace std;
int n,m,a[200005],len,in[200005],pos1,pos2,nw,sum[1000005],ansc,ans[200005];
char op[2];
struct node{
int l,r,idx,pos;
bool operator<(const node t)const{
if(in[l]!=in[t.l])
return l<t.l;
return r<t.r;
}
}qus[200005],upd[200005];
int main(){
scanf("%d%d",&n,&m);
len=pow(n,2.0/3);
for(int i=1;i<=n;i++){
scanf("%d",a+i);
in[i]=(i-1)/len+1;
}
for(int i=1,l,r;i<=m;i++){
scanf("%s%d%d",op,&l,&r);
if(op[0]=='Q')
qus[++pos1]={l,r,pos1,pos2};
else
upd[++pos2]={l,r,0,0};
}
sort(qus+1,qus+pos1+1);
int l=1,r=0;
nw=0;
for(int i=1;i<=pos1;i++){
while(l<qus[i].l){
sum[a[l]]--;
if(sum[a[l]]==0)
ansc--;
l++;
}
while(l>qus[i].l){
l--;
sum[a[l]]++;
if(sum[a[l]]==1)
ansc++;
}
while(r<qus[i].r){
r++;
sum[a[r]]++;
if(sum[a[r]]==1)
ansc++;
}
while(r>qus[i].r){
sum[a[r]]--;
if(sum[a[r]]==0)
ansc--;
r--;
}
while(nw>qus[i].pos){
if(upd[nw].l>=qus[i].l&&upd[nw].l<=qus[i].r){
sum[a[upd[nw].l]]--;
if(sum[a[upd[nw].l]]==0)
ansc--;
sum[upd[nw].r]++;
if(sum[upd[nw].r]==1)
ansc++;
swap(upd[nw].r,a[upd[nw].l]);
}
nw--;
}
while(nw<qus[i].pos){
nw++;
if(upd[nw].l>=qus[i].l&&upd[nw].l<=qus[i].r){
sum[a[upd[nw].l]]--;
if(sum[a[upd[nw].l]]==0)
ansc--;
sum[upd[nw].r]++;
if(sum[upd[nw].r]==1)
ansc++;
swap(upd[nw].r,a[upd[nw].l]);
}
}
ans[qus[i].idx]=ansc;
}
for(int i=1;i<=pos1;i++)
printf("%d\n",ans[i]);
return 0;
}
1-6 WA,7-10 TLE
回复
共 0 条回复,欢迎继续交流。
正在加载回复...