codeforce 940F

题链

这道题的数据规模·,居然是带修改莫队,神奇。

我们维护每个数出现了多少次,我们发现答案可以枚举,因为答案最大是O(N^0.5)的。

哈希会tle,要离散化。

#include<bits/stdc++.h>
using namespace std;
#define N 1000007
#define bel(x) ((x-1)/B+1)
#define sight(x) ('0'<=x&&x<='9')
int B,a[N],anw[N],ans,opt,pl,pr,Now_ti,n,m;
int ot,cnt,l,r;
int usd[N],ch;int b[N];
//struct Hash{
//    #define p 10050007
//    int head[p],net[N],fall[N],cost[N],tot;
//    void add(int x,int dla){
//        for (int i=head[x%p];i;i=net[i])
//         if (fall[i]==x) {cost[i]+=dla; return;}
//        fall[++tot]=x; net[tot]=head[x%p]; head[x%p]=tot; cost[tot]=dla;
//    }
//    int que(int x){
//        for (int i=head[x%p];i;i=net[i]) 
//        if (fall[i]==x) return cost[i]; return 0;
//    }
//    #undef p
//}ha;
template <class T>
inline void read(T &x){
  static char c;static int b;
  for (b=1,c=getchar();!sight(c);c=getchar()) if (c=='-') b=-1;
  for (x=0;sight(c);c=getchar()) x=x*10+c-48;
  x*=b;
} 
struct Node {
    int l,r,id,tim;
    inline bool operator <(const Node&A) const {
        if (bel(l)^bel(A.l)) return l<A.l;
        if (bel(r)^bel(A.r)) return r<A.r;
        return id<A.id; 
    }
}g[N];
struct cha{
    int p,col,fre;
}p[N];
int pp,c[N],gg,usdd[N];
inline void add(int x){
      pp=usdd[a[x]]++;  
      if (pp<N) usd[pp]--;
      if (pp+1<N) usd[pp+1]++;
}
inline void del(int x){
//      pp=ha.que(a[x]); ha.add(a[x],-1); 
      pp=usdd[a[x]]--; 
      if (pp<N) usd[pp]--;
      if (pp-1<N) usd[pp-1]++;
}
inline void change_add(int Tim) {
    opt=p[Tim].p;
    if (opt<=pr&&pl<=opt) {
        del(opt); a[opt]=p[Tim].col; add(opt);
    }
    a[opt]=p[Tim].col;
}
inline void change_del(int Tim) {
    opt=p[Tim].p;
    if (opt<=pr&&pl<=opt) {
        del(opt); a[opt]=p[Tim].fre; add(opt);
    }
    a[opt]=p[Tim].fre;
}
inline void change(int Tim) {
    while (Now_ti<Tim) change_add(++Now_ti);
    while (Now_ti>Tim) change_del(Now_ti--);
}
int dos() {
    for (int i=1;;i++) if (!usd[i]) return i;
}
int main () {
    read(n); read(m); B=(int)pow(n,0.667);
    for (int i=1;i<=n;i++) read(a[i]),b[i]=a[i],c[i]=a[i]; gg=n;
    for (int i=1;i<=m;i++) {
        read(ch);
        if (ch&1) {
            read(g[++ot].l); read(g[ot].r); 
            g[ot].id=ot;g[ot].tim=cnt;
        } else {
            read(p[++cnt].p); read(p[cnt].col);
            c[++gg]=p[cnt].col;
            p[cnt].fre=b[p[cnt].p]; b[p[cnt].p]=p[cnt].col;
        }
    }
    sort(c+1,c+gg+1);
    for (int i=1;i<=n;i++) a[i]=lower_bound(c+1,c+gg,a[i])-c;
    for (int i=1;i<=cnt;i++) 
    p[i].fre=lower_bound(c+1,c+gg,p[i].fre)-c,
    p[i].col=lower_bound(c+1,c+gg,p[i].col)-c;
    sort(g+1,g+ot+1);
    for (int i=1;i<=ot;i++) {
        change(g[i].tim);
        while (pl>g[i].l) add(--pl);
        while (pr<g[i].r) add(++pr);
        while (pl<g[i].l) del(pl++);
        while (pr>g[i].r) del(pr--);
        anw[g[i].id]=dos();
    }
    for (int i=1;i<=ot;i++)  printf("%d\n",anw[i]);
    return 0;
}

 

posted @ 2018-03-02 21:12  泪寒之雪  阅读(241)  评论(0编辑  收藏  举报