priyam2k

Distinct Value Queries - CSES

May 11th, 2020
1,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5.  
  6. const int N = 20 * 200005;
  7.  
  8. void io(){
  9.     ios_base::sync_with_stdio(false);
  10.     cin.tie(NULL);
  11.     cout.tie(NULL);    
  12.     #ifdef LOCAL
  13.         freopen("input.txt","r",stdin);
  14.         freopen("output.txt","w",stdout);
  15.         freopen("output.txt","w",stderr);
  16.     #endif
  17. }
  18.  
  19.  
  20. struct Vertex {
  21.     Vertex *l, *r;
  22.     int sum;
  23.  
  24.     Vertex(int val) : l(nullptr), r(nullptr), sum(val) {}
  25.     Vertex(Vertex *l, Vertex *r) : l(l), r(r), sum(0) {
  26.         if (l) sum += l->sum;
  27.         if (r) sum += r->sum;
  28.     }
  29. };
  30.  
  31. Vertex* build(int a[], int tl, int tr) {
  32.     if (tl == tr)
  33.         return new Vertex(a[tl]);
  34.     int tm = (tl + tr) / 2;
  35.     return new Vertex(build(a, tl, tm), build(a, tm+1, tr));
  36. }
  37.  
  38. int get_sum(Vertex* v, int tl, int tr, int l, int r) {
  39.     if (l > r)
  40.         return 0;
  41.     if (l == tl && tr == r)
  42.         return v->sum;
  43.     int tm = (tl + tr) / 2;
  44.     return get_sum(v->l, tl, tm, l, min(r, tm))
  45.          + get_sum(v->r, tm+1, tr, max(l, tm+1), r);
  46. }
  47.  
  48. Vertex* update(Vertex* v, int tl, int tr, int pos, int new_val) {
  49.     if (tl == tr)
  50.         return new Vertex(new_val);
  51.     int tm = (tl + tr) / 2;
  52.     if (pos <= tm)
  53.         return new Vertex(update(v->l, tl, tm, pos, new_val), v->r);
  54.     else
  55.         return new Vertex(v->l, update(v->r, tm+1, tr, pos, new_val));
  56. }
  57.  
  58. Vertex *seg[N];
  59. Vertex *st = new Vertex(NULL, NULL);
  60. int main() {
  61.     ios::sync_with_stdio(false);
  62.     cin.tie(0);
  63.     cout.tie(0);
  64.     int n, m;
  65.     cin >> n >> m;
  66.     vector<int> a(n);
  67.     for(int i = 0; i < n; i++) {
  68.         cin >> a[i];
  69.     }
  70.     vector<int> b = a;
  71.     sort(b.begin(), b.end());
  72.     b.resize(unique(b.begin(), b.end()) - b.begin());
  73.     for(int i = 0; i < n; i++) {
  74.         a[i] = lower_bound(b.begin(), b.end(), a[i])-b.begin();
  75.     }
  76.     int sz = n - 1;
  77.  
  78.     st -> l = st;
  79.     st -> r = st;
  80.     vector<int> prev(n, - 1);
  81.     for(int i = 0; i < n; i++) {
  82.         if(i == 0) {
  83.             seg[0] = update(st, 0, n - 1, i, 1);
  84.             prev[a[i]]= i;
  85.         }
  86.         else {
  87.             if(prev[a[i]] == -1) {
  88.                 seg[i] = update(seg[i - 1], 0, n - 1, i, 1);
  89.                 prev[a[i]] = i;
  90.             }
  91.             else {
  92.                 seg[i] = update(seg[i - 1], 0, n - 1, prev[a[i]], 0);
  93.                 seg[i] = update(seg[i], 0, n - 1, i, 1);
  94.                 prev[a[i]] = i;
  95.             }
  96.         }
  97.     }
  98.  
  99.     while(m--) {
  100.         int l, r;
  101.         cin >> l >> r;
  102.         l--;r--;
  103.         cout << get_sum(seg[r],0, n - 1, l, r) << "\n";
  104.     }
  105.  
  106. }
Add Comment
Please, Sign In to add comment