Submission #755461

#TimeUsernameProblemLanguageResultExecution timeMemory
755461MilosMilutinovicIzbori (COCI22_izbori)C++14
110 / 110
2655 ms3504 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int M = 2 * N; const int B = sqrt(N) / 2; int n, a[N], c[M], cnt[N], occ[N]; void modify(int p, int v) { for (p += N; p < M; p += p & -p) c[p] += v; } int query(int p) { int res = 0; for (p += N; p > 0; p -= p & -p) res += c[p]; return res; } void compress() { vector<int> xs; for (int i = 1; i <= n; i++) xs.push_back(a[i]); sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 1; i <= n; i++) { a[i] = (int) (lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin()) + 1; } } int readint() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int x = 0; while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } int main() { n = readint(); for (int i = 1; i <= n; i++) { a[i] = readint(); } compress(); for (int i = 1; i <= n; i++) { occ[a[i]] += 1; } long long ans = 0; for (int v = 1; v <= n; v++) { if (occ[v] >= B) { modify(0, +1); int bal = 0; for (int i = 1; i <= n; i++) { bal += (a[i] == v ? 1 : -1); ans += query(bal - 1); modify(bal, +1); } modify(0, -1); for (int i = 1; i < M; i++) c[i] = 0; } } for (int l = 1; l <= n; l++) { int f = a[l]; for (int r = l; r <= min(l + 2 * B, n); r++) { cnt[a[r]] += 1; if (cnt[a[r]] > cnt[f]) f = a[r]; if (cnt[f] * 2 > r - l + 1 && occ[f] < B) ans++; } for (int r = l; r <= min(l + 2 * B, n); r++) { cnt[a[r]] = 0; } } printf("%lld", ans); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...