#include <bits/stdc++.h>
using namespace std;
const int kN = 1 << 18;
int a[kN];
int64_t ans, f[kN << 1];
bitset<kN> used;
void solve(int l, int r) {
if (l == r) {
++ans;
return;
}
int mid = (l + r) >> 1;
solve(l, mid);
solve(mid + 1, r);
vector<int> candidates;
for (int i = l; i <= r; ++i) {
f[a[i]] = 0;
used[a[i]] = false;
}
for (int i = mid; i >= l; --i) {
if (++f[a[i]] > (mid - i + 1) / 2 && !used[a[i]]) {
candidates.emplace_back(a[i]);
used[a[i]] = true;
}
}
for (int i = l; i <= mid; ++i) {
f[a[i]] = 0;
}
for (int i = mid + 1; i <= r; ++i) {
if (++f[a[i]] > (i - mid) / 2 && !used[a[i]]) {
candidates.emplace_back(a[i]);
used[a[i]] = true;
}
}
int len = r - l + 1;
for (const int &x : candidates) {
for (int i = -len - 1; i <= len + 1; ++i) {
f[i + kN] = 0;
}
int sum = 0;
for (int i = mid; i >= l; --i) {
if (a[i] == x) {
++sum;
} else {
--sum;
}
++f[sum + kN];
}
for (int i = len; i >= -len - 1; --i) {
f[i + kN] += f[i + 1 + kN];
}
sum = 0;
for (int i = mid + 1; i <= r; ++i) {
if (a[i] == x) {
++sum;
} else {
--sum;
}
ans += f[-sum + 1 + kN];
}
}
}
void testCase() {
int n;
cin >> n;
vector<int> v;
v.resize(n);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
v[i - 1] = a[i];
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; ++i) {
a[i] = distance(v.begin(), lower_bound(v.begin(), v.end(), a[i]));
}
solve(1, n);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 1; tc <= tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
3652 KB |
Output is correct |
2 |
Correct |
116 ms |
4676 KB |
Output is correct |
3 |
Correct |
64 ms |
2944 KB |
Output is correct |
4 |
Correct |
118 ms |
4840 KB |
Output is correct |
5 |
Correct |
129 ms |
5096 KB |
Output is correct |
6 |
Correct |
132 ms |
5364 KB |
Output is correct |
7 |
Correct |
128 ms |
5356 KB |
Output is correct |
8 |
Correct |
131 ms |
5316 KB |
Output is correct |
9 |
Correct |
132 ms |
5384 KB |
Output is correct |
10 |
Correct |
130 ms |
5316 KB |
Output is correct |
11 |
Correct |
85 ms |
5380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
85 ms |
3652 KB |
Output is correct |
19 |
Correct |
116 ms |
4676 KB |
Output is correct |
20 |
Correct |
64 ms |
2944 KB |
Output is correct |
21 |
Correct |
118 ms |
4840 KB |
Output is correct |
22 |
Correct |
129 ms |
5096 KB |
Output is correct |
23 |
Correct |
132 ms |
5364 KB |
Output is correct |
24 |
Correct |
128 ms |
5356 KB |
Output is correct |
25 |
Correct |
131 ms |
5316 KB |
Output is correct |
26 |
Correct |
132 ms |
5384 KB |
Output is correct |
27 |
Correct |
130 ms |
5316 KB |
Output is correct |
28 |
Correct |
85 ms |
5380 KB |
Output is correct |
29 |
Correct |
101 ms |
6916 KB |
Output is correct |
30 |
Correct |
25 ms |
1720 KB |
Output is correct |
31 |
Correct |
58 ms |
3116 KB |
Output is correct |
32 |
Correct |
142 ms |
6456 KB |
Output is correct |
33 |
Correct |
57 ms |
3268 KB |
Output is correct |
34 |
Correct |
63 ms |
3316 KB |
Output is correct |
35 |
Correct |
38 ms |
2240 KB |
Output is correct |
36 |
Correct |
22 ms |
1604 KB |
Output is correct |
37 |
Correct |
27 ms |
1732 KB |
Output is correct |
38 |
Correct |
124 ms |
5388 KB |
Output is correct |
39 |
Correct |
120 ms |
5312 KB |
Output is correct |
40 |
Correct |
126 ms |
5316 KB |
Output is correct |
41 |
Correct |
120 ms |
5372 KB |
Output is correct |
42 |
Correct |
121 ms |
5368 KB |
Output is correct |
43 |
Correct |
127 ms |
6872 KB |
Output is correct |
44 |
Correct |
127 ms |
6948 KB |
Output is correct |
45 |
Correct |
127 ms |
6980 KB |
Output is correct |
46 |
Correct |
127 ms |
6968 KB |
Output is correct |
47 |
Correct |
126 ms |
6892 KB |
Output is correct |
48 |
Correct |
123 ms |
5676 KB |
Output is correct |
49 |
Correct |
125 ms |
5648 KB |
Output is correct |
50 |
Correct |
124 ms |
5524 KB |
Output is correct |
51 |
Correct |
118 ms |
5532 KB |
Output is correct |
52 |
Correct |
112 ms |
5696 KB |
Output is correct |
53 |
Correct |
110 ms |
5680 KB |
Output is correct |
54 |
Correct |
112 ms |
5572 KB |
Output is correct |