Submission #1024020

#TimeUsernameProblemLanguageResultExecution timeMemory
1024020kustizusPoklon (COCI17_poklon)C++17
140 / 140
2197 ms19636 KiB
#include <bits/stdc++.h> using namespace std; const int N = 5e5; int n, q, cn, a[N + 5], ans[N + 5]; struct Node { int l, r, pos; }; vector<Node> qu; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // freopen ("file.inp","r",stdin); // freopen ("file.out","w",stdout); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; qu.push_back({l, r, i}); } cn = sqrt(n); sort(qu.begin(), qu.end(), [&](Node a, Node b) { return a.l / cn < b.l / cn || (a.l / cn == b.l / cn && a.r < b.r); }); int l = 1, r = 0, now = 0; unordered_map<int, int> mp; for (int i = 0; i < q; i++) { while (l < qu[i].l) { int x = --mp[a[l]]; if (x == 2) now++; else if (x == 1) now--; ++l; } while (l > qu[i].l) { --l; int x = ++mp[a[l]]; if (x == 2) now++; else if (x == 3) now--; } while (r > qu[i].r) { int x = --mp[a[r]]; if (x == 2) now++; else if (x == 1) now--; r--; } while (r < qu[i].r) { r++; int x = ++mp[a[r]]; if (x == 2) now++; else if (x == 3) now--; } ans[qu[i].pos] = now; } for (int i = 1; i <= q; i++) cout << ans[i] << "\n"; return 0; } // hay
#Verdict Execution timeMemoryGrader output
Fetching results...