fork download
  1. /*
  2. * @Author: hungeazy
  3. * @Date: 2026-03-04 23:24:15
  4. * @Last Modified by: hungeazy
  5. * @Last Modified time: 2026-03-08 15:41:23
  6. */
  7. #include <bits/stdc++.h>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10. // #pragma GCC optimize("O3")
  11. // #pragma GCC optimize("unroll-loops")
  12. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  13. using namespace std;
  14. using namespace __gnu_pbds;
  15. bool M1;
  16. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  17. #define int long long
  18. #define ll long long
  19. #define ull unsigned long long
  20. #define sz(x) x.size()
  21. #define sqr(x) (1LL * (x) * (x))
  22. #define all(x) x.begin(), x.end()
  23. #define fill(f,x) memset(f,x,sizeof(f))
  24. #define FOR(i,l,r) for(int i=l;i<=r;i++)
  25. #define FOD(i,r,l) for(int i=r;i>=l;i--)
  26. #define debug(x) cout << #x << " = " << x << '\n'
  27. #define ii pair<int,int>
  28. #define iii pair<int,ii>
  29. #define di pair<ii,ii>
  30. #define vi vector<int>
  31. #define vii vector<ii>
  32. #define mii map<int,int>
  33. #define fi first
  34. #define se second
  35. #define pb push_back
  36. #define MOD 1000000007
  37. #define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
  38. #define YES cout << "YES\n"
  39. #define NO cout << "NO\n"
  40. #define MASK(i) (1LL << (i))
  41. #define c_bit(i) __builtin_popcountll(i)
  42. #define BIT(x,i) ((x) & MASK(i))
  43. #define SET_ON(x,i) ((x) | MASK(i))
  44. #define SET_OFF(x,i) ((x) & ~MASK(i))
  45. #define oo 1e18
  46. #define name "test"
  47. #define endl '\n'
  48. #define memory() cerr << abs(&M2-&M1)/1024.0/1024 << " MB" << endl
  49. #define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms." << endl
  50. template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
  51. template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
  52. template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
  53. const int N = (int)2e5+10;
  54. int n,a[N],q,S;
  55.  
  56. struct Query {
  57. int l,r,id;
  58. void input(int i) {
  59. cin >> l >> r;
  60. id = i;
  61. }
  62. bool operator<(const Query &other) const
  63. {
  64. if (l/S != other.l/S)
  65. return l/S < other.l/S;
  66. return r < other.r;
  67. }
  68. } query[N];
  69.  
  70. namespace hungeazy {
  71.  
  72. const int M = (int)1e6+10;
  73. int cnt[M],ans = 0,res[N];
  74.  
  75. void solve(void)
  76. {
  77. S = sqrt(n);
  78. sort(query+1,query+q+1);
  79. int L = 1, R = 0;
  80. FOR(i,1,q)
  81. {
  82. int l = query[i].l, r = query[i].r, id = query[i].id;
  83. while (L > l)
  84. {
  85. L--;
  86. cnt[a[L]]++;
  87. if (cnt[a[L]] == 1) ans++;
  88. }
  89. while (L < l)
  90. {
  91. cnt[a[L]]--;
  92. if (!cnt[a[L]]) ans--;
  93. L++;
  94. }
  95. while (R < r)
  96. {
  97. R++;
  98. cnt[a[R]]++;
  99. if (cnt[a[R]] == 1) ans++;
  100. }
  101. while (R > r)
  102. {
  103. cnt[a[R]]--;
  104. if (!cnt[a[R]]) ans--;
  105. R--;
  106. }
  107. res[id] = ans;
  108. }
  109. FOR(i,1,q) cout << res[i] << endl;
  110. }
  111.  
  112. }
  113.  
  114. bool M2;
  115. signed main()
  116. {
  117. fast;
  118. if (fopen(name".inp","r"))
  119. {
  120. freopen(name".inp","r",stdin);
  121. freopen(name".out","w",stdout);
  122. }
  123. cin >> n;
  124. FOR(i,1,n) cin >> a[i];
  125. cin >> q;
  126. FOR(i,1,q) query[i].input(i);
  127. hungeazy::solve();
  128. time();
  129. memory();
  130. return 0;
  131. }
  132. // ██░ ██ █ ██ ███▄ █ ▄████
  133. //▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
  134. //▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
  135. //░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
  136. //░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
  137. // ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
  138. // ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
  139. // ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
  140. // ░ ░ ░ ░ ░ ░
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
-------------Time:5.988ms.
15.2593 MB