# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1082938 |
2024-09-02T07:08:08 Z |
vjudge1 |
Izbori (COCI22_izbori) |
C++17 |
|
0 ms |
0 KB |
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
struct fenwick {
vector<int> b;
int n = 0;
int m = 0;
int o = 0;
void start(int x) {
n = x;
b.resize(x + 1, o);
}
void insertVal(int x) {
m++;
update(m, x);
}
void update(int idx, int x) {
for (; idx <= n; idx += idx & -idx) b[idx] += x;
}
int query1(int idx) {
int res = 0;
for (; idx > 0; idx -= idx & -idx) res += b[idx];
return res;
}
int query(int l, int r) {
return query1(r) - query1(l - 1);
}
};
typedef long long ll;
const int MAX = 2e5 + 2;
const int s = 447;
int a[MAX], p[MAX], cnt[MAX], cnt1[MAX], n;
ll res;
map<int, int> mp;
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]]++;
}
int m = 0;
for (auto i : mp) {
mp[i.first] = ++m;
}
for (int i = 1; i <= n; i++) {
a[i] = mp[a[i]];
cnt1[a[i]]++;
}
for (int i = 1; i <= n; i++) {
int mx = 0;
for (int j = i; j <= min(n, i + 2 * s - 1); j++) {
if (cnt1[a[j]] <= s) {
cnt[a[j]]++;
mx = max(mx, cnt[a[j]]);
if (mx > (j - i + 1 - mx)) res++;
}
}
for (int j = i; j <= min(n, i + 2 * s - 1); j++) {
if (cnt1[a[j]] <= s) cnt[a[j]]--;
}
}
for (int i = 1; i <= m; i++) {
if (cnt1[i] <= s) continue;
p[j] = 0;
for (int j = 1; j <= n; j++) p[j] = p[j - 1] + (a[j] == i ? 1 : -1);
for (int j = 0; j <= n; j++) p[j] += n;
fenwick st;
st.start(2 * n);
for (int j = 1; j <= 2 * n; j++) st.insertVal(0);
st.update(p[0], 1);
for (int j = 1; j <= n; j++) {
res += st.query(1, p[j] - 1);
st.update(p[j], 1);
}
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:74:11: error: 'j' was not declared in this scope
74 | p[j] = 0;
| ^