Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int int64_t
- const int B = 300;
- int32_t main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- int n, m;
- cin >> n >> m;
- vector<int> a(n);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- vector<int> b = a;
- sort(b.begin(), b.end());
- for (int i = 0; i < n; i++) {
- a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
- }
- vector<vector<pair<pair<int, int>, int>>> queries((n - 1) / B + 1); // r, l, ind
- for (int i = 0; i < m; i++) {
- int l, r;
- cin >> l >> r;
- l--;
- queries[l / B].push_back({{r, l}, i});
- }
- vector<int> ans(m);
- for (int g = 0; g < queries.size(); g++) {
- sort(queries[g].begin(), queries[g].end());
- int l = g * B, r = g * B;
- vector<int> fr(n);
- int cur_ans = 0;
- for (auto query: queries[g]) {
- int q_l = query.first.second;
- int q_r = query.first.first;
- int q_ind = query.second;
- while (r < q_r) {
- if (fr[a[r]] == b[a[r]]) {
- cur_ans--;
- }
- fr[a[r]]++;
- if (fr[a[r]] == b[a[r]]) {
- cur_ans++;
- }
- r++;
- }
- while (l > q_l) {
- l--;
- if (fr[a[l]] == b[a[l]]) {
- cur_ans--;
- }
- fr[a[l]]++;
- if (fr[a[l]] == b[a[l]]) {
- cur_ans++;
- }
- }
- while (l < q_l) {
- if (fr[a[l]] == b[a[l]]) {
- cur_ans--;
- }
- fr[a[l]]--;
- if (fr[a[l]] == b[a[l]]) {
- cur_ans++;
- }
- l++;
- }
- ans[q_ind] = cur_ans;
- }
- }
- for (int i = 0; i < m; i++) {
- cout << ans[i] << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment