fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 100004;
  5. int n, q, a[N], freq[N], blockSize, ans[N];
  6. int curAns = 0;
  7.  
  8. struct Query {
  9. int l, r, idx;
  10. } queryList[N];
  11.  
  12. void add(int x) {
  13. if (freq[a[x]] == 0) curAns++;
  14. freq[a[x]]++;
  15. }
  16.  
  17. void remove_(int x) {
  18. freq[a[x]]--;
  19. if (freq[a[x]] == 0) curAns--;
  20. }
  21.  
  22. signed main() {
  23. ios::sync_with_stdio(false);
  24. cin.tie(NULL);
  25. cin >> n >> q;
  26. blockSize = sqrt(n);
  27. for (int i = 0; i < n; i++) cin >> a[i];
  28. for (int i = 0; i < q; i++) {
  29. cin >> queryList[i].l >> queryList[i].r;
  30. queryList[i].idx = i;
  31. }
  32. sort(queryList, queryList + q, [](Query x, Query y) {
  33. int bx = x.l / blockSize, by = y.l / blockSize;
  34. if (bx != by) return bx < by;
  35. return (bx & 1) ? x.r > y.r : x.r < y.r;
  36. });
  37.  
  38. int l = 0, r = -1;
  39. for (int i = 0; i < q; i++) {
  40. int L = queryList[i].l, R = queryList[i].r;
  41. while (r < R) add(++r);
  42. while (r > R) remove_(r--);
  43. while (l < L) remove_(l++);
  44. while (l > L) add(--l);
  45. ans[queryList[i].idx] = curAns;
  46. }
  47.  
  48. for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  49. }
  50.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty