fork download
  1. // ROOT : DRAGON3012009
  2. #include <bits/stdc++.h>
  3. #define ll long long
  4. #define ld long double
  5. #define el "\n"
  6. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7. #define __ROOT__ int main()
  8. #pragma GCC optimize("O2")
  9. //#pragma GCC optimize("unroll-loops")
  10. //#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  11. #define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
  12. #define FORD(i,r,l) for(int i = r ; i >= l ; i --)
  13. #define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
  14. #define fi first
  15. #define se second
  16. #define M 1000000007
  17. #define MAXN 1000001
  18. #define INF (1ll<<30)
  19. #define BLOCK_SIZE 425
  20. #define MAX_NODE 1001001
  21. #define LOG 19
  22. #define ALPHA_SIZE 26
  23. #define BASE 256
  24. #define NAME "file"
  25. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
  26. using namespace std;
  27. using namespace chrono ;
  28. const ll MOD[] = {(ll)1e9 + 2277, (ll)1e9 + 5277, (ll)1e9 + 8277, (ll)1e9 + 9277, (ll) 1e9 + 7 };
  29. const ll NMOD = 1;
  30. const int dx[] = {-1, 0, 1,0};
  31. const int dy[] = {0, 1, 0, -1};
  32. //**Variable**//
  33. ll n, q, nb ;
  34. ll arr[MAXN];
  35. ll cnt[MAXN] ;
  36. ll block[MAXN] ;
  37. ll res[MAXN] ;
  38. //**Struct**//
  39.  
  40. struct Que {
  41. ll l, r, id ;
  42. bool operator < (const Que & other ) const {
  43. ll blockA = l / BLOCK_SIZE, blockB = other.l / BLOCK_SIZE ;
  44. return blockA == blockB ? (blockA % 2 == 0 ? r < other.r : r > other.r) : blockA < blockB ;
  45. }
  46. };
  47. vector<Que> que ;
  48. //**Function**//
  49. template<class X, class Y >
  50. bool minimize(X & x, const Y &y ) {
  51. return x > y ? x = y, 1:0 ;
  52. }
  53. template<class X, class Y >
  54. bool maximize(X &x, const Y &y ) {
  55. return x < y ? x = y, 1:0 ;
  56. }
  57.  
  58. void init() {
  59. cin>>n >> q ;
  60. FOR(i,1, n) cin >> arr[i] ;
  61. FOR(i, 1, q ) {
  62. ll l, r ;
  63. cin >> l >> r ;
  64. que.push_back({l, r, i }) ;
  65. }
  66. nb = n / BLOCK_SIZE ;
  67. }
  68. void add(ll value ) {
  69. cnt[value ] ++ ;
  70. if(cnt[value] == 1 ) block[value / BLOCK_SIZE] ++ ;
  71. }
  72. void del(ll value ) {
  73. cnt[value ] -- ;
  74. if(cnt[value] == 0 ) block[value / BLOCK_SIZE] -- ;
  75. }
  76. ll get() {
  77. FOR(i, 0, nb ) {
  78. if(block[i] != BLOCK_SIZE ) {
  79. ll start = i * BLOCK_SIZE ;
  80. FOR(j, start, n ) {
  81. if(cnt[j] == 0 ) return j ;
  82. }
  83. }
  84. }
  85. }
  86. void Mo() {
  87. cnt[0] = 1;
  88. block[0] = 1 ;
  89. sort(que.begin(), que.end() ) ;
  90. ll l = 1, r = 0 ;
  91. for(auto [l1, r1, id ] : que) {
  92. while(l < l1 ) del(arr[l ++ ]) ;
  93. while(l > l1 ) add(arr[--l ]) ;
  94. while(r > r1 ) del(arr[r-- ]) ;
  95. while(r < r1 ) add(arr[++ r]) ;
  96. res[id] = get() ;
  97. }
  98. }
  99. void solve() {
  100. Mo() ;
  101. FOR(i , 1 , q ) cout << res[i] << el ;
  102. }
  103.  
  104. __ROOT__ {
  105. // freopen(NAME".inp" , "r" , stdin);
  106. // freopen(NAME".out" , "w", stdout) ;
  107. fast;
  108. int t = 1; // cin >> t ;
  109. while(t--) {
  110. init();
  111. solve();
  112. }
  113. return (0&0);
  114. }
  115.  
  116.  
  117.  
  118.  
Success #stdin #stdout 0.01s 7500KB
stdin
Standard input is empty
stdout
Standard output is empty