1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include <bits/stdc++.h> using namespace std; #define LL long long #define ll long long #define ULL unsigned long long #define Pair pair<LL,LL> #define f1 first #define f2 second #define ls rt<<1 #define rs rt<<1|1 #define Pi acos(-1.0) #define eps 1e-6 #define DBINF 1e100 #define mod 998244353 #define MAXN 1e18 #define MS 1000009
LL n,m; LL a[MS]; struct node{ int l,r,id; }ask[MS]; LL size,bknum; LL bkl[MS],bkr[MS]; LL belong[MS];
LL ANS; LL cnp[MS]; LL cnt[MS];
LL ac[MS];
void init_ANS(){ for(int i=1;i<=n;i++){ if(a[i] > n+1) continue; cnt[a[i]]++; } while(cnt[ANS]) ANS++; }
void init_bk(){ size = sqrt(n); bknum = n/size; for(int i=1;i<=bknum;i++){ bkl[i] = (i-1)*size+1; bkr[i] = i*size; } if(bkr[bknum] < n){ bknum++; bkl[bknum] = bkr[bknum-1]+1; bkr[bknum] = n; } for(int i=1;i<=bknum;i++){ for(int j=bkl[i];j<=bkr[i];j++){ belong[j] = i; } } }
bool cmp(node t1,node t2){ if(belong[t1.l] != belong[t2.l]) return t1.l < t2.l; return t1.r > t2.r; }
void add(int pos){ if(a[pos] > n+1) return; cnt[a[pos]]++; }
void remove(int pos,LL &ans){ if(a[pos] > n+1) return; cnt[a[pos]]--; if(cnt[a[pos]] == 0) ans = min(ans,a[pos]); }
int main() { ios::sync_with_stdio(false); cin >> n >> m; for(int i=1;i<=n;i++){ cin >> a[i]; } init_ANS(); for(int i=1;i<=m;i++){ int l,r; cin >> l >> r; ask[i] = {l,r,i}; } init_bk(); sort(ask+1,ask+m+1,cmp); int L = 1 ,R = n; int lastbk = 0; LL ans = ANS; for(int i=1;i<=m;i++){ if(belong[ask[i].l] == belong[ask[i].r]){ for(int j=ask[i].l;j<=ask[i].r;j++){ if(a[j] > n+1) continue; cnp[a[j]]++; } LL tmp = 0; while(cnp[tmp]) tmp++; ac[ask[i].id] = tmp; for(int j=ask[i].l;j<=ask[i].r;j++){ if(a[j] > n+1) continue; cnp[a[j]]--; } continue; } if(belong[ask[i].l] != lastbk){ while(R < n) add(++R); while(L < bkl[belong[ask[i].l]]) remove(L++,ANS); lastbk = belong[ask[i].l]; ans = ANS; } while(R > ask[i].r){ remove(R--,ans); } LL tmp = ans; while(L < ask[i].l){ remove(L++,tmp); } ac[ask[i].id] = tmp; while(L > bkl[belong[ask[i].l]]) add(--L); } for(int i=1;i<=m;i++){ cout << ac[i] << "\n"; } return 0; }
|