Cogs 1901. [国家集训队2011]数颜色 bzoj2120(分块 有修改的分块)

来源:互联网 发布:淘宝购买充气娃娃图 编辑:程序博客网 时间:2024/05/16 06:58

题目链接:bzoj2120

题目大意:墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

思路;需要修改的莫队

对于每个询问记录他已经被修改了几次

对于每个修改操作记录修改前的数值,方便之后的复原

排序的话 按照块从小到大  右端点从小到大  按照改的次数从小到大

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 100050struct nodeQ{    int l,r,t,id;    //t为已经改的次数}a[maxn];struct nodeR{    int pos,x,h;    //h为记录改之前的值}b[maxn];bool if_[maxn];int bel[maxn],ai[maxn],tot,num,ans[maxn],ti[maxn*10];//ti数组记录相应种类的个数//bel记录在哪个块int n,m,size=440,now;int cmp(nodeQ x,nodeQ y){    if(bel[x.l]==bel[y.l]) {        if(bel[x.r]==bel[y.r])return x.t<y.t;        return x.r<y.r;    }    return x.l<y.l;}//按照块从小到大  右端点从小到大  按照改的次数从小到大 inline void change(int x){    int pos=b[x].pos;    if(if_[pos]){//判断这个点是否在区间内        ti[ai[pos]]--;//ai[pos]对应种类减一        if(!ti[ai[pos]])now--;//如果种类减为0  区间的种类数减一    }    b[x].h=ai[pos];//记录之前的值    ai[pos]=b[x].x;    if(if_[pos]){        if(!ti[ai[pos]])now++;//如果新增了一个种类        ti[ai[pos]]++;//对应种类的个数加一    }}inline void unchange(int x){    int pos=b[x].pos;    if(if_[pos]){//是否在区间内        ti[ai[pos]]--;        if(!ti[ai[pos]])now--;    }    ai[pos]=b[x].h;    if(if_[pos]){        if(!ti[ai[pos]])now++;        ti[ai[pos]]++;    }}inline void update(int to,int x){    int pre=ti[ai[to]];    ti[ai[to]]+=x;    if(ti[ai[to]]==0&&pre==1)now--;    if(ti[ai[to]]==1&&pre==0)now++;    if(x==1)if_[to]=true;    else if_[to]=false;}int main(){    scanf("%d%d",&n,&m);    char ch[4];int l,r,t;    for(int i=1;i<=n;i++)scanf("%d",&ai[i]),bel[i]=(i-1)/size+1;    for(int i=1;i<=m;i++){        scanf("%s%d%d",ch,&l,&r);        if(ch[0]=='Q')a[++tot].l=l,a[tot].r=r,a[tot].t=num,a[tot].id=tot;        elseb[++num].pos=l,b[num].x=r;    }    sort(a+1,a+tot+1,cmp);    l=1,r=0,t=0,now=0;    for(int i=1;i<=tot;i++){        while(t<a[i].t)t++,change(t);        while(t>a[i].t)unchange(t),t--;        while(r<a[i].r)r++,update(r,1);        while(r>a[i].r)update(r,-1),r--;        while(l<a[i].l)update(l,-1),l++;        while(l>a[i].l)l--,update(l,1);        ans[a[i].id]=now;    }    for(int i=1;i<=tot;i++)printf("%d\n",ans[i]);    return 0;}


原创粉丝点击