rembocoder

Untitled

Feb 18th, 2023
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int int64_t
  6.  
  7. const int B = 300;
  8.  
  9. int32_t main() {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(0); cout.tie(0);
  12.     int n, m;
  13.     cin >> n >> m;
  14.     vector<int> a(n);
  15.     for (int i = 0; i < n; i++) {
  16.         cin >> a[i];
  17.     }
  18.     vector<int> b = a;
  19.     sort(b.begin(), b.end());
  20.     for (int i = 0; i < n; i++) {
  21.         a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  22.     }
  23.     vector<vector<pair<pair<int, int>, int>>> queries((n - 1) / B + 1); // r, l, ind
  24.     for (int i = 0; i < m; i++) {
  25.         int l, r;
  26.         cin >> l >> r;
  27.         l--;
  28.         queries[l / B].push_back({{r, l}, i});
  29.     }
  30.     vector<int> ans(m);
  31.     for (int g = 0; g < queries.size(); g++) {
  32.         sort(queries[g].begin(), queries[g].end());
  33.         int l = g * B, r = g * B;
  34.         vector<int> fr(n);
  35.         int cur_ans = 0;
  36.         for (auto query: queries[g]) {
  37.             int q_l = query.first.second;
  38.             int q_r = query.first.first;
  39.             int q_ind = query.second;
  40.             while (r < q_r) {
  41.                 if (fr[a[r]] == b[a[r]]) {
  42.                     cur_ans--;
  43.                 }
  44.                 fr[a[r]]++;
  45.                 if (fr[a[r]] == b[a[r]]) {
  46.                     cur_ans++;
  47.                 }
  48.                 r++;
  49.             }
  50.             while (l > q_l) {
  51.                 l--;
  52.                 if (fr[a[l]] == b[a[l]]) {
  53.                     cur_ans--;
  54.                 }
  55.                 fr[a[l]]++;
  56.                 if (fr[a[l]] == b[a[l]]) {
  57.                     cur_ans++;
  58.                 }
  59.             }
  60.             while (l < q_l) {
  61.                 if (fr[a[l]] == b[a[l]]) {
  62.                     cur_ans--;
  63.                 }
  64.                 fr[a[l]]--;
  65.                 if (fr[a[l]] == b[a[l]]) {
  66.                     cur_ans++;
  67.                 }
  68.                 l++;
  69.             }
  70.             ans[q_ind] = cur_ans;
  71.         }
  72.     }
  73.     for (int i = 0; i < m; i++) {
  74.         cout << ans[i] << '\n';
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment