#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int N = 2e5 + 5;
const int B = 450;
int a[N];
int n;
bool sub12() {
return (n <= 2000);
}
bool sub3() {
for (int i = 1; i <= n; ++i)
if (a[i] > 2) return false;
return true;
}
template <typename T>
struct Fenwick {
vector <int> f;
int n;
Fenwick(int _n) {
f.resize(_n + 5);
n = _n;
}
void upd(int x, int val) {
for (; x <= n; x += x & -x)
f[x] += val;
}
int get(int x) {
int ans = 0;
for (; x > 0; x -= x & -x)
ans += f[x];
return ans;
}
};
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#ifdef JASPER
freopen("in2", "r", stdin);
#endif
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
vector <int> vals;
for (int i = 1; i <= n; ++i)
vals.push_back(a[i]);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
vector <int> cnt(n + 1, 0);
for (int i = 1; i <= n; ++i) {
a[i] = (int) (lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin() + 1);
cnt[a[i]]++;
}
vector <bool> heavy(n + 1, false);
for (int i = 1; i <= n; ++i) {
if (cnt[i] >= B)
heavy[i] = true;
}
ll ans = 0;
// processing light queries: all elements appear less than sqrt(n)
cnt.assign(n + 1, 0);
for (int i = 1; i <= n; ++i) {
int mx = 0;
for (int j = i; j <= n && (j - i + 1) < 2 * B; ++j) {
if (!heavy[a[j]]) {
cnt[a[j]]++;
if (cnt[a[j]] > mx) mx = cnt[a[j]];
if (2 * mx > (j - i + 1)) {
++ans;
}
}
}
for (int j = i; j <= n && (j - i + 1) < 2 * B; ++j)
if (!heavy[a[j]]) cnt[a[j]]--;
}
// processing heavy queries: all elements appear at least sqrt(n)
for (int x = 1; x <= n; ++x) {
if (heavy[x]) {
auto get = [&] (int _x) { debug(_x); return _x + n; };
vector <int> f(2 * n + 5, 0);
int sum = 0;
int prf = 0;
f[get(prf)]++;
for (int i = 1; i <= n; ++i) {
prf += (a[i] == x? 1 : -1);
if (a[i] == x)
sum += f[get(prf - 1)];
else
sum -= f[get(prf)];
ans += sum;
f[get(prf)]++;
}
}
}
cout << ans << "\n";
return 0;
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
9 | #define debug(...) 166
| ^~~
Main.cpp:101:39: note: in expansion of macro 'debug'
101 | auto get = [&] (int _x) { debug(_x); return _x + n; };
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
5 ms |
336 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
5 ms |
336 KB |
Output is correct |
12 |
Correct |
5 ms |
500 KB |
Output is correct |
13 |
Correct |
5 ms |
336 KB |
Output is correct |
14 |
Correct |
6 ms |
336 KB |
Output is correct |
15 |
Correct |
5 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
336 KB |
Output is correct |
17 |
Correct |
3 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
3012 KB |
Output is correct |
2 |
Correct |
336 ms |
3780 KB |
Output is correct |
3 |
Correct |
188 ms |
2504 KB |
Output is correct |
4 |
Correct |
337 ms |
4036 KB |
Output is correct |
5 |
Correct |
350 ms |
4096 KB |
Output is correct |
6 |
Correct |
375 ms |
4480 KB |
Output is correct |
7 |
Correct |
388 ms |
4292 KB |
Output is correct |
8 |
Correct |
415 ms |
4304 KB |
Output is correct |
9 |
Correct |
383 ms |
4292 KB |
Output is correct |
10 |
Correct |
371 ms |
4480 KB |
Output is correct |
11 |
Correct |
377 ms |
4472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
5 ms |
336 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
5 ms |
336 KB |
Output is correct |
12 |
Correct |
5 ms |
500 KB |
Output is correct |
13 |
Correct |
5 ms |
336 KB |
Output is correct |
14 |
Correct |
6 ms |
336 KB |
Output is correct |
15 |
Correct |
5 ms |
336 KB |
Output is correct |
16 |
Correct |
5 ms |
336 KB |
Output is correct |
17 |
Correct |
3 ms |
336 KB |
Output is correct |
18 |
Correct |
252 ms |
3012 KB |
Output is correct |
19 |
Correct |
336 ms |
3780 KB |
Output is correct |
20 |
Correct |
188 ms |
2504 KB |
Output is correct |
21 |
Correct |
337 ms |
4036 KB |
Output is correct |
22 |
Correct |
350 ms |
4096 KB |
Output is correct |
23 |
Correct |
375 ms |
4480 KB |
Output is correct |
24 |
Correct |
388 ms |
4292 KB |
Output is correct |
25 |
Correct |
415 ms |
4304 KB |
Output is correct |
26 |
Correct |
383 ms |
4292 KB |
Output is correct |
27 |
Correct |
371 ms |
4480 KB |
Output is correct |
28 |
Correct |
377 ms |
4472 KB |
Output is correct |
29 |
Correct |
384 ms |
4484 KB |
Output is correct |
30 |
Correct |
137 ms |
980 KB |
Output is correct |
31 |
Correct |
293 ms |
1656 KB |
Output is correct |
32 |
Correct |
801 ms |
2764 KB |
Output is correct |
33 |
Correct |
308 ms |
1488 KB |
Output is correct |
34 |
Correct |
331 ms |
1500 KB |
Output is correct |
35 |
Correct |
205 ms |
1236 KB |
Output is correct |
36 |
Correct |
118 ms |
860 KB |
Output is correct |
37 |
Correct |
145 ms |
1148 KB |
Output is correct |
38 |
Correct |
408 ms |
4288 KB |
Output is correct |
39 |
Correct |
384 ms |
4292 KB |
Output is correct |
40 |
Correct |
383 ms |
4292 KB |
Output is correct |
41 |
Correct |
376 ms |
4308 KB |
Output is correct |
42 |
Correct |
388 ms |
4480 KB |
Output is correct |
43 |
Correct |
394 ms |
4292 KB |
Output is correct |
44 |
Correct |
388 ms |
4292 KB |
Output is correct |
45 |
Correct |
386 ms |
4484 KB |
Output is correct |
46 |
Correct |
396 ms |
4476 KB |
Output is correct |
47 |
Correct |
414 ms |
4476 KB |
Output is correct |
48 |
Correct |
464 ms |
4292 KB |
Output is correct |
49 |
Correct |
488 ms |
5168 KB |
Output is correct |
50 |
Correct |
423 ms |
5052 KB |
Output is correct |
51 |
Correct |
411 ms |
5044 KB |
Output is correct |
52 |
Correct |
557 ms |
5212 KB |
Output is correct |
53 |
Correct |
535 ms |
5208 KB |
Output is correct |
54 |
Correct |
456 ms |
5060 KB |
Output is correct |