fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fastIO ios_base::sync_with_stdio(0); cin.tie(0);
  4. #define ll long long
  5. #define all(x) x.begin(), x.end()
  6.  
  7. const int INF = 1e9;
  8. const int N = 1e5+5;
  9.  
  10. struct query{
  11. int l, r, id;
  12. } Q[N];
  13. int n, q, a[N], BLOCK_SIZE, L=1, R=0, cnt[N], res[N];
  14. set<int> se;
  15.  
  16. void solve(int i){
  17. while (R<Q[i].r){
  18. R++;
  19. if (cnt[a[R]]==0){
  20. se.erase(a[R]);
  21. }
  22. cnt[a[R]]++;
  23. }
  24. while (L>Q[i].l){
  25. L--;
  26. if (cnt[a[L]]==0){
  27. se.erase(a[L]);
  28. }
  29. cnt[a[L]]++;
  30. }
  31. while (R>Q[i].r){
  32. cnt[a[R]]--;
  33. if (cnt[a[R]]==0){
  34. se.insert(a[R]);
  35. }
  36. R--;
  37. }
  38. while (L<Q[i].l){
  39. cnt[a[L]]--;
  40. if (cnt[a[L]]==0){
  41. se.insert(a[L]);
  42. }
  43. L++;
  44. }
  45. }
  46.  
  47. bool MO(query x, query y){
  48. int X = x.l/BLOCK_SIZE;
  49. int Y = y.l/BLOCK_SIZE;
  50.  
  51. if (X!=Y) return X<Y;
  52. if (X&1) return x.r<y.r;
  53. return x.r>y.r;
  54. }
  55.  
  56. int main(){
  57. fastIO;
  58.  
  59. cin >> n >> q;
  60. for (int i=1; i<=n; i++) cin >> a[i];
  61. for (int i=1; i<=q; i++){
  62. cin >> Q[i].l >> Q[i].r;
  63. Q[i].id = i;
  64. }
  65. BLOCK_SIZE = sqrt(n);
  66. sort(Q+1, Q+1+q, MO);
  67. for (int i=1; i<=n; i++) se.insert(i);
  68. for (int i=1; i<=q; i++){
  69. solve(i);
  70. res[Q[i].id] = *(se.begin());
  71. }
  72. for (int i=1; i<=q; i++){
  73. cout << res[i] << '\n';
  74. }
  75. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty