#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[0] = 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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
2768 KB |
Output is correct |
2 |
Correct |
164 ms |
3412 KB |
Output is correct |
3 |
Correct |
95 ms |
2132 KB |
Output is correct |
4 |
Correct |
173 ms |
3660 KB |
Output is correct |
5 |
Correct |
179 ms |
3660 KB |
Output is correct |
6 |
Correct |
223 ms |
3916 KB |
Output is correct |
7 |
Correct |
202 ms |
3916 KB |
Output is correct |
8 |
Correct |
192 ms |
3916 KB |
Output is correct |
9 |
Correct |
192 ms |
3916 KB |
Output is correct |
10 |
Correct |
212 ms |
3936 KB |
Output is correct |
11 |
Correct |
186 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
4 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
153 ms |
2768 KB |
Output is correct |
19 |
Correct |
164 ms |
3412 KB |
Output is correct |
20 |
Correct |
95 ms |
2132 KB |
Output is correct |
21 |
Correct |
173 ms |
3660 KB |
Output is correct |
22 |
Correct |
179 ms |
3660 KB |
Output is correct |
23 |
Correct |
223 ms |
3916 KB |
Output is correct |
24 |
Correct |
202 ms |
3916 KB |
Output is correct |
25 |
Correct |
192 ms |
3916 KB |
Output is correct |
26 |
Correct |
192 ms |
3916 KB |
Output is correct |
27 |
Correct |
212 ms |
3936 KB |
Output is correct |
28 |
Correct |
186 ms |
3932 KB |
Output is correct |
29 |
Correct |
193 ms |
5456 KB |
Output is correct |
30 |
Correct |
145 ms |
2648 KB |
Output is correct |
31 |
Correct |
270 ms |
4688 KB |
Output is correct |
32 |
Correct |
722 ms |
9068 KB |
Output is correct |
33 |
Correct |
302 ms |
4948 KB |
Output is correct |
34 |
Correct |
308 ms |
4944 KB |
Output is correct |
35 |
Correct |
196 ms |
3924 KB |
Output is correct |
36 |
Correct |
100 ms |
2468 KB |
Output is correct |
37 |
Correct |
127 ms |
2648 KB |
Output is correct |
38 |
Correct |
203 ms |
3924 KB |
Output is correct |
39 |
Correct |
235 ms |
3796 KB |
Output is correct |
40 |
Correct |
206 ms |
3916 KB |
Output is correct |
41 |
Correct |
204 ms |
3920 KB |
Output is correct |
42 |
Correct |
205 ms |
4172 KB |
Output is correct |
43 |
Correct |
243 ms |
5460 KB |
Output is correct |
44 |
Correct |
269 ms |
5496 KB |
Output is correct |
45 |
Correct |
237 ms |
5492 KB |
Output is correct |
46 |
Correct |
237 ms |
5432 KB |
Output is correct |
47 |
Correct |
245 ms |
5500 KB |
Output is correct |
48 |
Correct |
978 ms |
4152 KB |
Output is correct |
49 |
Correct |
976 ms |
4264 KB |
Output is correct |
50 |
Correct |
497 ms |
4152 KB |
Output is correct |
51 |
Correct |
501 ms |
4096 KB |
Output is correct |
52 |
Correct |
1779 ms |
4284 KB |
Output is correct |
53 |
Correct |
1590 ms |
4276 KB |
Output is correct |
54 |
Correct |
947 ms |
4176 KB |
Output is correct |