#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], 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]];
for (int i = 1; i <= n; i++) {
int mx = 0;
for (int j = i; j <= min(n, i + 2 * s - 1); j++) {
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++) cnt[a[j]]--;
}
for (int i = 1; i <= n; i++) cnt[a[i]]++;
for (int i = 1; i <= n; i++) {
if (cnt[i] <= s) continue;
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);
int k = 0;
for (int j = 1; j <= n; j++) {
while (k + 2 * s < j) {
st.update(p[k], 1);
k++;
}
res += st.query(1, p[j] - 1);
}
}
cout << res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2512 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2512 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2648 KB |
Output is correct |
10 |
Correct |
4 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2532 KB |
Output is correct |
13 |
Correct |
3 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
3 ms |
2536 KB |
Output is correct |
16 |
Correct |
3 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
2532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
372 ms |
3772 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2512 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2648 KB |
Output is correct |
10 |
Correct |
4 ms |
2396 KB |
Output is correct |
11 |
Correct |
3 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2532 KB |
Output is correct |
13 |
Correct |
3 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2396 KB |
Output is correct |
15 |
Correct |
3 ms |
2536 KB |
Output is correct |
16 |
Correct |
3 ms |
2396 KB |
Output is correct |
17 |
Correct |
3 ms |
2532 KB |
Output is correct |
18 |
Incorrect |
372 ms |
3772 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |