[bzoj2453]维护队列

题目大意

需要兹瓷修改,区间询问不同种类数。

带修改莫队

裸上呀!

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int maxn=10000+10,maxd=1000000+10,c=25;
int belong[maxn],a[maxn],b[maxn][3],ans[maxn],cnt[maxd];
struct dong{
    int l,r,x,id;
} ask[maxn];
int i,j,k,t,n,m,l,r,x,top,tot,now;
bool cmp(dong a,dong b){
    return belong[a.l]<belong[b.l]||belong[a.l]==belong[b.l]&&belong[a.r]<belong[b.r]||belong[a.l]==belong[b.l]&&belong[a.r]==belong[b.r]&&a.x<b.x;
}
void in(int x){
    cnt[x]++;
    if (cnt[x]==1) now++;
}
void out(int x){
    cnt[x]--;
    if (cnt[x]==0) now--;
}
void change(int j,int x,int y){
    a[j]=y;
    if (l<=j&&j<=r){
        out(x);
        in(y);
    }
}
char get(){
    char ch=getchar();
    while (ch!='Q'&&ch!='R') ch=getchar();
    return ch;
}
int main(){
    scanf("%d%d",&n,&m);
    fo(i,1,n) scanf("%d",&a[i]),belong[i]=(i-1)/c+1;
    fo(i,1,m)
        if (get()=='Q'){
            ++top;
            scanf("%d%d",&ask[top].l,&ask[top].r);
            ask[top].x=tot;
            ask[top].id=top;
        }
        else{
            scanf("%d%d",&j,&k);
            b[++tot][0]=j;
            b[tot][1]=a[j];
            a[j]=b[tot][2]=k;
        }
    sort(ask+1,ask+top+1,cmp);
    l=1;r=0;x=tot;
    fo(i,1,top){
        while (x<ask[i].x){
            x++;
            change(b[x][0],b[x][1],b[x][2]);
        }
        while (x>ask[i].x){
            change(b[x][0],b[x][2],b[x][1]);
            x--;
        }
        while (l<ask[i].l){
            out(a[l]);
            l++;
        }
        while (l>ask[i].l){
            l--;
            in(a[l]);
        }
        while (r<ask[i].r){
            r++;
            in(a[r]);
        }
        while (r>ask[i].r){
            out(a[r]);
            r--;
        }
        ans[ask[i].id]=now;
    }
    fo(i,1,top) printf("%d\n",ans[i]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值