UVA

题意显然:给出初始序列,单点修改,区间查询元素的种类。

由于时限过宽,暴力可过。

比较优秀的解法应该是莫队。

带修改的莫队题解可以看https://www.luogu.org/blog/user12668/solution-p1903

证明和解释比较详细。

但是……为什么我的莫队也要跑5~6秒,

  1 /*
  2 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
  3 ¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
  4 ¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
  5 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
  6 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó
  7 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó
  8 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó
  9 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó
 10 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó
 11 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ó¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ô¡ó¡ó¡ó¡ó¡ó
 12 ¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ô¡ô¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ô¡ô¡ô¡ó¡ô¡ô¡ô¡ó¡ó¡ó¡ó
 13 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
 14 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
 15 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
 16 ¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó¡ó
 17 */
 18 #include<iostream>
 19 #include<cstdio>
 20 #include<cstring>
 21 #include<ctime>
 22 #include<cstdlib>
 23 #include<algorithm>
 24 #include<cmath>
 25 #include<string>
 26 #include<queue>
 27 #include<vector>
 28 #include<map>
 29 #include<set>
 30 using namespace std;
 31 int read(){
 32     int xx=0,ff=1;char ch=getchar();
 33     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
 34     while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
 35     return xx*ff;
 36 }
 37 const int maxn=50010;
 38 int N,M,a[maxn],b[maxn],arg[maxn][3];
 39 int belong[maxn],block;
 40 struct query{
 41     int x,y,t,id;
 42     bool friend operator<(const query&A,const query&B){
 43         if(belong[A.x]!=belong[A.y])
 44             return A.x<B.x;
 45         if(belong[A.y]!=belong[B.y])
 46             return A.y<B.y;
 47         return A.t<B.t;
 48     }
 49 }Q[maxn];
 50 struct change{
 51     int x,y,p;
 52 }C[maxn];
 53 int totc,totq;
 54 int cnt[1000010],now,ans[maxn];
 55 int X,Y,T;
 56 inline void add(int x){
 57     if(!cnt[x])
 58         now++;
 59     cnt[x]++;
 60 }
 61 inline void del(int x){
 62     cnt[x]--;
 63     if(!cnt[x])
 64         now--;
 65 }
 66 inline void change_add(int i){
 67     if(X<=C[i].p&&C[i].p<=Y){
 68         del(b[C[i].p]);
 69         add(C[i].y);
 70     }
 71     b[C[i].p]=C[i].y;
 72 }
 73 inline void change_del(int i){
 74     if(X<=C[i].p&&C[i].p<=Y){
 75         del(b[C[i].p]);
 76         add(C[i].x);
 77     }
 78     b[C[i].p]=C[i].x;
 79 }
 80 int main(){
 81     //freopen("in.txt","r",stdin);
 82     N=read(),M=read();
 83     block=(int)pow(M,2.0/3)+1;
 84     for(int i=1;i<=N;i++)
 85         belong[i]=(i-1)/block;
 86     for(int i=1;i<=N;i++)
 87         a[i]=b[i]=read();
 88     for(int i=1;i<=M;i++){
 89         char opt=getchar();
 90         while(opt==' '||opt=='
')
 91             opt=getchar();
 92         arg[i][0]=(opt=='Q');
 93         arg[i][1]=read();
 94         arg[i][2]=read();
 95         if(arg[i][0]==0){
 96             totc++;
 97             C[totc].x=b[arg[i][1]+1];
 98             C[totc].y=arg[i][2];
 99             C[totc].p=arg[i][1]+1;
100             b[arg[i][1]+1]=arg[i][2];
101         }
102         else{
103             totq++;
104             Q[totq].x=arg[i][1]+1;
105             Q[totq].y=arg[i][2];
106             Q[totq].id=totq;
107             Q[totq].t=totc;
108         }
109     }
110     for(int i=1;i<=N;i++)
111         b[i]=a[i];
112     X=1,Y=1,T=0;
113     cnt[a[1]]=1,now=1;
114     for(int i=1;i<=totq;i++){
115         for(;X<Q[i].x;X++)
116             del(b[X]);
117         for(;X>Q[i].x;X--)
118             add(b[X-1]);
119         for(;Y<Q[i].y;Y++)
120             add(b[Y+1]);
121         for(;Y>Q[i].y;Y--)
122             del(b[Y]);
123         for(;T<Q[i].t;T++)
124             change_add(T+1);
125         for(;T>Q[i].t;T--)
126             change_del(T);
127         ans[Q[i].id]=now;
128     }
129     for(int i=1;i<=totq;i++)
130         printf("%d
",ans[i]);
131     return 0;
132 }
View Code

 人丑自带大常数

原文地址:https://www.cnblogs.com/lzhAFO/p/9142648.html