Advertisement
Guest User

Untitled

a guest
Jun 27th, 2018
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define forn(i, n) for(int i = 0; i < int(n); i++)
  6. #define x first
  7. #define y second
  8.  
  9. const int N = 500 * 1000 + 13;
  10. const int P = 800;
  11. const int INF = 1e9;
  12.  
  13. bool comp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b){
  14.     int num = a.x.x / P;
  15.     if (num != b.x.x / P)
  16.         return a.x.x < b.x.x;
  17.     if (num & 1)
  18.         return a.x.y < b.x.y;
  19.     return a.x.y > b.x.y;
  20. }
  21.  
  22. int val[N];
  23. int cnt[N];
  24. int bl[P];
  25. int tot;
  26.  
  27. inline void add(int x){
  28.     ++cnt[x];
  29.     if (cnt[x] == 1){
  30.         ++val[x];
  31.         ++bl[x / P];
  32.         ++tot;
  33.     }
  34.     else if (cnt[x] == 2){
  35.         --val[x];
  36.         --bl[x / P];
  37.         --tot;
  38.     }
  39. }
  40.  
  41. inline void sub(int x){
  42.     --cnt[x];
  43.     if (cnt[x] == 1){
  44.         ++val[x];
  45.         ++bl[x / P];
  46.         ++tot;
  47.     }
  48.     else if (cnt[x] == 0){
  49.         --val[x];
  50.         --bl[x / P];
  51.         --tot;
  52.     }
  53. }
  54.  
  55. int get(){
  56.     if (tot == 0)
  57.         return 0;
  58.    
  59.     forn(i, P){
  60.         if (bl[i] > 0){
  61.             for (int j = i * P;; ++j){
  62.                 if (val[j]){
  63.                     return j;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.    
  69.     assert(false);
  70. }
  71.  
  72. int n, m;
  73. int a[N], ans[N];
  74. pair<pair<int, int>, int> q[N];
  75.  
  76. int main() {
  77.     scanf("%d", &n);
  78.     forn(i, n) scanf("%d", &a[i]);
  79.     scanf("%d", &m);
  80.     forn(i, m){
  81.         scanf("%d%d", &q[i].x.x, &q[i].x.y);
  82.         --q[i].x.x, --q[i].x.y;
  83.         q[i].y = i;
  84.     }
  85.     sort(q, q + m, comp);
  86.    
  87.     int l = 0, r = -1;
  88.    
  89.     forn(i, m){
  90.         int L = q[i].x.x;
  91.         int R = q[i].x.y;
  92.        
  93.         while (r < R){
  94.             ++r;
  95.             add(a[r]);
  96.         }
  97.        
  98.         while (r > R){
  99.             sub(a[r]);
  100.             --r;
  101.         }
  102.        
  103.         while (l > L){
  104.             --l;
  105.             add(a[l]);
  106.         }
  107.        
  108.         while (l < L){
  109.             sub(a[l]);
  110.             ++l;
  111.         }
  112.        
  113.         ans[q[i].y] = get();
  114.     }
  115.    
  116.     forn(i, m)
  117.         printf("%d\n", ans[i]);
  118.    
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement