Advertisement
yicongli

LG4137

Mar 27th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     x=0;T k=1;char gc;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
  14. }
  15.  
  16. const int N=2e5+7;
  17. const int S=450;
  18. int n,m;
  19. int a[N];
  20. int be[N];
  21. int cnt[N];
  22. int Ans[N];
  23. int sum[N];
  24.  
  25. inline void work(int pos,int d){
  26.     if(a[pos]>n)return ;
  27.     if(d==1){
  28.         cnt[a[pos]]++;
  29.         if(cnt[a[pos]]==1)sum[a[pos]/S]++;
  30.     }
  31.     else {
  32.         cnt[a[pos]]--;
  33.         if(cnt[a[pos]]==0)sum[a[pos]/S]--;
  34.     }
  35. }
  36.  
  37. inline int calc(){
  38.     for(int i=0;i<S;i++){
  39.         if(sum[i]<S){
  40.             for(int j=0;j<S;++j){
  41.                 if(!cnt[i*S+j])return i*S+j;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. struct Query{
  48.     int l,r,id;
  49.     inline bool operator < (const Query &a)const{
  50.         return be[l]==be[a.l]?r<a.r:l<a.l;
  51.     }
  52. }Q[N];
  53.  
  54. int main(){
  55.     // freopen("forest.in","r",stdin);
  56.     // freopen("forest.out","w",stdout);
  57.     r(n),r(m);
  58.     int size=sqrt(n);
  59.     for(int i=1;i<=n;++i){
  60.         be[i]=(i-1)/size+1;
  61.         r(a[i]);
  62.     }
  63.     for(int i=1;i<=m;++i){
  64.         r(Q[i].l),r(Q[i].r);
  65.         Q[i].id=i;
  66.     }
  67.     sort(Q+1,Q+m+1);
  68.     int l=1,r=0;
  69.     for(int i=1;i<=m;++i){
  70.         Query &x=Q[i];
  71.         while(r>Q[i].r)work(r--,-1);
  72.         while(r<Q[i].r)work(++r,1);
  73.         while(l>Q[i].l)work(--l,1);
  74.         while(l<Q[i].l)work(l++,-1);
  75.         Ans[x.id]=calc();
  76.     }
  77.     for(int i=1;i<=m;++i){
  78.         printf("%d\n",Ans[i]);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement