# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40978 | 2018-02-10T18:27:55 Z | alenam0161 | Poklon (COCI17_poklon) | C++14 | 3595 ms | 17328 KB |
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int N = 5e5 + 7; const int Block = 307; int cnt[N]; int a[N]; int id[N], timer; pair<int,int> b[N]; struct ban { int l, r, ind; }; ban Q[N]; int ans = 0; void add(int x) { if (cnt[id[x]] == 1)ans++; cnt[id[x]]++; if (cnt[id[x]] == 3)ans--; } void rem(int x) { if (cnt[id[x]] == 2)ans--; cnt[id[x]]--; if (cnt[id[x]] == 2)ans++; } int pat[N]; int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); b[i] = { a[i],i }; } sort(b + 1, b + 1 + n); for (int i = 1; i <= n; ++i)if (i == 1 || b[i].first != b[i - 1].first) { id[b[i].second] = ++timer; } else { id[b[i].second] = id[b[i - 1].second]; } for (int i = 1; i <= q; ++i) { int l, r, ind; scanf("%d%d", &l, &r); ind = i; Q[i] = { l,r,ind }; } sort(Q + 1, Q + 1 + q, [&](const ban &a1, const ban &a2){ if (a1.l / Block == a2.l / Block) { return a1.r < a2.r; } else return a1.l / Block < a2.l / Block; }); int L=0, R=0; for (int i = 1; i <= q; ++i) { // cout << Q[i].l << " " << Q[i].r << " ID:" << Q[i].ind << endl; while (L < Q[i].l)rem(L++); while (L > Q[i].l)add(--L); while (R < Q[i].r)add(++R); while (R > Q[i].r)rem(R--); pat[Q[i].ind] = ans; } for (int i = 1; i <= q; ++i) { printf("%d\n", pat[i]); } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 376 KB | Output is correct |
2 | Correct | 3 ms | 592 KB | Output is correct |
3 | Correct | 3 ms | 592 KB | Output is correct |
4 | Correct | 7 ms | 664 KB | Output is correct |
5 | Correct | 221 ms | 3768 KB | Output is correct |
6 | Correct | 214 ms | 3820 KB | Output is correct |
7 | Correct | 707 ms | 7424 KB | Output is correct |
8 | Correct | 1418 ms | 10580 KB | Output is correct |
9 | Correct | 2263 ms | 13872 KB | Output is correct |
10 | Correct | 3595 ms | 17328 KB | Output is correct |