Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. const int maxn = 1e5 + 100;
  10. typedef long long ll;
  11.  
  12. int n, a[maxn];
  13. int q;
  14. int block[maxn];
  15. int cnt[maxn];
  16. int cnt_dif;
  17.  
  18. struct Query
  19. {
  20.     int l, r, id;
  21.     int ans;
  22. } querys[maxn];
  23.  
  24. bool cmp(const Query & x, const Query & y)
  25. {
  26.     if (block[x.l] == block[y.l]) return x.r < y.r;
  27.     return x.l < y.l;
  28. }
  29. bool cmp_id(const Query & x, const Query & y)
  30. {
  31.     return x.id < y.id;
  32. }
  33.  
  34. void update(int p, int add)
  35. {
  36.     if (add == -1)
  37.     {
  38.         if (cnt[a[p]] == 1) --cnt_dif;
  39.         cnt[a[p]]--;
  40.     }
  41.     else
  42.     {
  43.         if (cnt[a[p]] == 0) ++cnt_dif;
  44.         cnt[a[p]]++;
  45.     }
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     while (scanf("%d%d", &n, &q) == 2)
  52.     {
  53.         memset(cnt, 0, sizeof(int)*(1+n));
  54.         cnt_dif = 0;
  55.         for (int i = 1; i <= n; ++i)
  56.         {
  57.             scanf("%d", a + i);
  58.             if (cnt[a[i]] == 0) ++cnt_dif;
  59.             cnt[a[i]]++;
  60.         }
  61.         int block_size = (int)sqrt(n);
  62.         for (int i = 1; i <= n; ++i)
  63.             block[i] = (i - 1) / block_size + 1;
  64.         for (int i = 1; i <= q; ++i)
  65.         {
  66.             scanf("%d%d", &querys[i].l, &querys[i].r);
  67.             if(querys[i].l >= querys[i].r) querys[i].r = querys[i].l+1;
  68.             querys[i].id = i;
  69.         }
  70.  
  71.         sort(querys + 1, querys + 1 + q, cmp);
  72.  
  73.         int l = 1, r = 2;
  74.         for (int i = 1; i <= q; ++i)
  75.         {
  76.             for (; r < querys[i].r; ++r) update(r, -1);
  77.             for (; r > querys[i].r; --r) update(r - 1, 1);
  78.             for (; l < querys[i].l; ++l) update(l + 1, 1);
  79.             for (; l > querys[i].l; --l) update(l, 1);
  80.             querys[i].ans = cnt_dif;
  81.         }
  82.         sort(querys + 1, querys + 1 + q, cmp_id);
  83.         for (int i = 1; i <= q; ++i) printf("%d\n", querys[i].ans);
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement