[BZOJ2120][带修改莫队算法]数颜色

待修改莫队裸题,打完后发现N^2貌似也能过….

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 100010

using namespace std;

int n,m,cnt0,cnt1,x,y,block,g,L,R,Ans;
char op;
int A[N],b[N],app[1000010],lst[N];
struct query{
  int l,r,t,iAns;
  friend bool operator <(query x,query y){
    if(b[x.l]!=b[y.l]) return b[x.l]<b[y.l];
    if(b[x.r]!=b[y.r]) return b[x.r]<b[y.r];
    return x.t<y.t;
  }
}Q[N];
struct modify{
  int a,b,t,g;
}M[N];

inline bool back(query a,query b){return a.t<b.t;}

inline void reaD(int &x){
  char c=getchar(); x=0;
  for(;!(c>='0'&&c<='9');c=getchar());
  for(;c>='0'&&c<='9';x=x*10+c-'0',c=getchar());
}

inline void change(int x,int b){
  if(x>=L&&x<=R){
    if((--app[A[x]])==0) Ans--;
    if((app[A[x]=b]++)==0) Ans++;
  }else A[x]=b;
}

inline void Add(int x){
  if((app[x]++)==0) Ans++;
}

inline void Minus(int x){
  if((--app[x])==0) Ans--;
}

int main(){
  reaD(n); reaD(m); block=(int)sqrt(n);
  for(int i=1;i<=n;i++) reaD(A[i]),lst[i]=A[i];
  for(int i=1;i<=n;i++){
    b[i]=g;
    if(i%block==0) ++g;
  }
  for(int i=1;i<=m;i++){
    while((op=getchar())!='Q'&&op!='R');
    reaD(x); reaD(y);
    if(op=='Q'){
      Q[++cnt1].l=x; Q[cnt1].r=y; Q[cnt1].t=i;
    }
    else{
      M[++cnt0].a=lst[x]; M[cnt0].b=lst[x]=y; M[cnt0].t=i;
      M[cnt0].g=x;
    }
  }
  sort(Q+1,Q+1+cnt1); int head=0; L=1,R=0;
  for(int i=1;i<=cnt1;i++){
    while(Q[i].t>M[head+1].t&&head+1<=cnt0){
      head++;
      change(M[head].g,M[head].b);
    }
    while(Q[i].t<M[head].t&&head){
      change(M[head].g,M[head].a);
      head--;
    }
    while(R>Q[i].r) Minus(A[R--]);
    while(R<Q[i].r) Add(A[++R]);
    while(L<Q[i].l) Minus(A[L++]);
    while(L>Q[i].l) Add(A[--L]);
    Q[i].iAns=Ans;
  }
  sort(Q+1,Q+1+cnt1,back);
  for(int i=1;i<=cnt1;i++) printf("%d\n",Q[i].iAns);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值