#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 4e5 + 5;
const int S = 600;
const int inf = 1e9;
int n;
struct Fenwick {
int bit[N];
void Reset() {
for (int i = 1; i < N; i++) bit[i] = 0;
}
void Add(int x) {
x += n + 1;
for (; x < N; x += x & (-x)) bit[x] += 1;
}
int Get(int x) {
int ans = 0; x += n;
for (; x > 0; x -= x & (-x)) ans += bit[x];
return ans;
}
}f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
map<int, int> hsh;
int z = 0;
vector<int> a(n);
vector<vector<int>> pos(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (hsh.find(a[i]) == hsh.end()) hsh[a[i]] = z++;
a[i] = hsh[a[i]];
pos[a[i]].push_back(i);
}
vector<int> big(n);
for (int i = 0; i < n; i++) if (pos[i].size() >= S) big[i] = 1;
ll ans = 0;
vector<int> cnt(n);
for (int i = 0; i < n; i++) {
int mx = a[i];
for (int j = i; j >= max(0, i - 2 * S); j--) {
cnt[a[j]] += 1;
if (cnt[a[j]] > cnt[mx]) mx = a[j];
if (2 * cnt[mx] > i - j + 1 && big[mx] == 0) ans += 1;
}
for (int j = i; j >= max(0, i - 2 * S); j--) cnt[a[j]] = 0;
}
for (int p = 0; p < n; p++) {
if (big[p] == 0) continue;
vector<int> pref(n);
f.Reset();
f.Add(0);
for (int i = 0; i < n; i++) {
if (a[i] == p) pref[i] += 1;
else pref[i] -= 1;
if (i > 0) pref[i] += pref[i - 1];
ans += f.Get(pref[i]);
f.Add(pref[i]);
}
}
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
10 ms |
424 KB |
Output is correct |
10 |
Correct |
10 ms |
428 KB |
Output is correct |
11 |
Correct |
11 ms |
424 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
10 ms |
340 KB |
Output is correct |
14 |
Correct |
11 ms |
340 KB |
Output is correct |
15 |
Correct |
11 ms |
428 KB |
Output is correct |
16 |
Correct |
10 ms |
340 KB |
Output is correct |
17 |
Correct |
12 ms |
1956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
978 ms |
7804 KB |
Output is correct |
2 |
Correct |
1310 ms |
9576 KB |
Output is correct |
3 |
Correct |
779 ms |
6564 KB |
Output is correct |
4 |
Correct |
1389 ms |
9896 KB |
Output is correct |
5 |
Correct |
1446 ms |
10304 KB |
Output is correct |
6 |
Correct |
1503 ms |
10816 KB |
Output is correct |
7 |
Correct |
1526 ms |
10808 KB |
Output is correct |
8 |
Correct |
1532 ms |
10796 KB |
Output is correct |
9 |
Correct |
1508 ms |
10816 KB |
Output is correct |
10 |
Correct |
1468 ms |
10832 KB |
Output is correct |
11 |
Correct |
1474 ms |
10728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
10 ms |
424 KB |
Output is correct |
10 |
Correct |
10 ms |
428 KB |
Output is correct |
11 |
Correct |
11 ms |
424 KB |
Output is correct |
12 |
Correct |
10 ms |
340 KB |
Output is correct |
13 |
Correct |
10 ms |
340 KB |
Output is correct |
14 |
Correct |
11 ms |
340 KB |
Output is correct |
15 |
Correct |
11 ms |
428 KB |
Output is correct |
16 |
Correct |
10 ms |
340 KB |
Output is correct |
17 |
Correct |
12 ms |
1956 KB |
Output is correct |
18 |
Correct |
978 ms |
7804 KB |
Output is correct |
19 |
Correct |
1310 ms |
9576 KB |
Output is correct |
20 |
Correct |
779 ms |
6564 KB |
Output is correct |
21 |
Correct |
1389 ms |
9896 KB |
Output is correct |
22 |
Correct |
1446 ms |
10304 KB |
Output is correct |
23 |
Correct |
1503 ms |
10816 KB |
Output is correct |
24 |
Correct |
1526 ms |
10808 KB |
Output is correct |
25 |
Correct |
1532 ms |
10796 KB |
Output is correct |
26 |
Correct |
1508 ms |
10816 KB |
Output is correct |
27 |
Correct |
1468 ms |
10832 KB |
Output is correct |
28 |
Correct |
1474 ms |
10728 KB |
Output is correct |
29 |
Correct |
1509 ms |
10732 KB |
Output is correct |
30 |
Correct |
283 ms |
4436 KB |
Output is correct |
31 |
Correct |
563 ms |
8168 KB |
Output is correct |
32 |
Correct |
1432 ms |
16668 KB |
Output is correct |
33 |
Correct |
606 ms |
8580 KB |
Output is correct |
34 |
Correct |
663 ms |
8908 KB |
Output is correct |
35 |
Correct |
392 ms |
6204 KB |
Output is correct |
36 |
Correct |
245 ms |
4052 KB |
Output is correct |
37 |
Correct |
262 ms |
4656 KB |
Output is correct |
38 |
Correct |
1425 ms |
10848 KB |
Output is correct |
39 |
Correct |
1445 ms |
10808 KB |
Output is correct |
40 |
Correct |
1445 ms |
10760 KB |
Output is correct |
41 |
Correct |
1487 ms |
10684 KB |
Output is correct |
42 |
Correct |
1474 ms |
10848 KB |
Output is correct |
43 |
Correct |
1520 ms |
11004 KB |
Output is correct |
44 |
Correct |
1521 ms |
10932 KB |
Output is correct |
45 |
Correct |
1521 ms |
10948 KB |
Output is correct |
46 |
Correct |
1438 ms |
10968 KB |
Output is correct |
47 |
Correct |
1487 ms |
11032 KB |
Output is correct |
48 |
Correct |
2726 ms |
10768 KB |
Output is correct |
49 |
Correct |
2747 ms |
10856 KB |
Output is correct |
50 |
Correct |
1850 ms |
11152 KB |
Output is correct |
51 |
Correct |
1971 ms |
11140 KB |
Output is correct |
52 |
Correct |
1431 ms |
8320 KB |
Output is correct |
53 |
Correct |
1390 ms |
8968 KB |
Output is correct |
54 |
Correct |
2674 ms |
11356 KB |
Output is correct |