Salvens

F

Aug 14th, 2023
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <array>
  4. #include <vector>
  5. #include <numeric>
  6. #include <random>
  7. #include <chrono>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11.  
  12. using namespace std;
  13.  
  14. const long long INF = 1e9 + 7;
  15. const int MAXN = 1e5 + 10;
  16. const int N = 1e5 + 10;
  17. const int MAX_VALUE = 1e6 + 1;
  18. const int B = 1000;
  19.  
  20. struct Query {
  21.     int l, r, i;
  22.  
  23.     Query() = default;
  24.  
  25.     Query(int l_, int r_, int i_) : l(l_), r(r_), i(i_) {};
  26. };
  27.  
  28. bool comp(const Query& lhs, const Query& rhs) {
  29.     return lhs.l / B < rhs.l / B || lhs.l / B == rhs.l / B && lhs.r < rhs.r;
  30. }
  31.  
  32. int k;
  33. int mp[MAX_VALUE];
  34. int ans = 0;
  35.  
  36. inline void add(int x) {
  37.     ++mp[x];
  38.     if (mp[x] == k + 1) {
  39.         --ans;
  40.     } else if (mp[x] == k) {
  41.         ++ans;
  42.     }
  43. }
  44.  
  45. inline void pop(int x) {
  46.     --mp[x];
  47.     if (mp[x] == k - 1) {
  48.         --ans;
  49.     } else if (mp[x] == k) {
  50.         ++ans;
  51.     }
  52. }
  53.  
  54. signed main() {
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(nullptr);
  57.  
  58.     int n, m, q;
  59.     cin >> n >> m >> q >> k;
  60.     vector<int> a(n);
  61.     for (int i = 0; i < n; ++i) {
  62.         cin >> a[i];
  63.     }
  64.     if (k == 0) {
  65.         ans = m;
  66.     }
  67.     vector<Query> ask;
  68.     for (int i = 0; i < q; ++i) {
  69.         int l, r;
  70.         cin >> l >> r;
  71.         ask.emplace_back(l - 1, r - 1, i);
  72.     }
  73.     sort(ask.begin(), ask.end(), comp);
  74.     int left = 0, right = -1;
  75.     vector<int> answer(q);
  76.     for (int i = 0; i < q; ++i) {
  77.         const auto [l, r, pos] = ask[i];
  78.         while (right < r) {
  79.             add(a[++right]);
  80.         }
  81.         while (left > l) {
  82.             add(a[--left]);
  83.         }
  84.         while (right > r) {
  85.             pop(a[right--]);
  86.         }
  87.         while (left < l) {
  88.             pop(a[left++]);
  89.         }
  90.         answer[pos] = ans;
  91.     }
  92.  
  93.     for (const auto i : answer) {
  94.         cout << i << '\n';
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment