// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i, n) for(int i=0; i<(n); ++i)
#define red(i, n) for(int i=(n)-1; i>=0; --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "icebearat"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll LLinf = 1e18 + 27092008;
const int N = 2e5 + 5;
const int THRESHOLD = 450;
int n, a[N];
int cnt[N], curCnt[N];
void Compress() {
vector<int> com;
FOR(i, 1, n) com.pb(a[i]);
sort(all(com));
com.resize(unique(all(com)) - com.begin());
FOR(i, 1, n)
a[i] = lower_bound(all(com), a[i]) - com.begin() + 1;
}
ll ans = 0;
void lightQuery() {
FOR(i, 1, n) {
int mx = 0;
FOR(j, i, min(i + 2 * THRESHOLD - 1, n)) {
if (cnt[a[j]] <= THRESHOLD) {
curCnt[a[j]]++;
mx = max(mx, curCnt[a[j]]);
}
if (2 * mx > (j - i + 1)) ans++;
}
FOR(j, i, min(i + 2 * THRESHOLD - 1, n))
if (cnt[a[j]] <= THRESHOLD)
curCnt[a[j]]--;
}
}
int arr[N], num[N << 1];
ll dp[N];
void heavyQuery() {
vector<int> heavy;
FOR(i, 1, n)
if (cnt[a[i]] > THRESHOLD) {
heavy.pb(a[i]);
cnt[a[i]] = 0;
}
for(int x : heavy) {
FOR(i, 1, n)
arr[i] = (a[i] == x ? +1 : -1);
memset(num, 0, sizeof num);
memset(dp, 0, sizeof dp);
int pref = n;
num[pref]++;
FOR(i, 1, n) {
pref += arr[i];
if (arr[i] > 0) // pref[i] > pref[i - 1]
dp[i] = dp[i - 1] + num[pref - 1];
else
dp[i] = dp[i - 1] - num[pref];
ans += dp[i];
num[pref]++;
}
}
}
void solve() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
Compress();
FOR(i, 1, n) cnt[a[i]]++;
lightQuery();
heavyQuery();
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:106:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:107:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
528 KB |
Output is correct |
11 |
Correct |
3 ms |
528 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
532 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
3700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
4792 KB |
Output is correct |
2 |
Correct |
270 ms |
5396 KB |
Output is correct |
3 |
Correct |
164 ms |
4660 KB |
Output is correct |
4 |
Correct |
284 ms |
5360 KB |
Output is correct |
5 |
Correct |
292 ms |
5340 KB |
Output is correct |
6 |
Correct |
317 ms |
5552 KB |
Output is correct |
7 |
Correct |
325 ms |
5548 KB |
Output is correct |
8 |
Correct |
320 ms |
5608 KB |
Output is correct |
9 |
Correct |
334 ms |
5548 KB |
Output is correct |
10 |
Correct |
330 ms |
5616 KB |
Output is correct |
11 |
Correct |
315 ms |
5608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
4 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
528 KB |
Output is correct |
11 |
Correct |
3 ms |
528 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
532 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
5 ms |
3700 KB |
Output is correct |
18 |
Correct |
214 ms |
4792 KB |
Output is correct |
19 |
Correct |
270 ms |
5396 KB |
Output is correct |
20 |
Correct |
164 ms |
4660 KB |
Output is correct |
21 |
Correct |
284 ms |
5360 KB |
Output is correct |
22 |
Correct |
292 ms |
5340 KB |
Output is correct |
23 |
Correct |
317 ms |
5552 KB |
Output is correct |
24 |
Correct |
325 ms |
5548 KB |
Output is correct |
25 |
Correct |
320 ms |
5608 KB |
Output is correct |
26 |
Correct |
334 ms |
5548 KB |
Output is correct |
27 |
Correct |
330 ms |
5616 KB |
Output is correct |
28 |
Correct |
315 ms |
5608 KB |
Output is correct |
29 |
Correct |
323 ms |
7068 KB |
Output is correct |
30 |
Correct |
98 ms |
1112 KB |
Output is correct |
31 |
Correct |
226 ms |
1756 KB |
Output is correct |
32 |
Correct |
592 ms |
3544 KB |
Output is correct |
33 |
Correct |
263 ms |
1964 KB |
Output is correct |
34 |
Correct |
255 ms |
2004 KB |
Output is correct |
35 |
Correct |
156 ms |
1536 KB |
Output is correct |
36 |
Correct |
83 ms |
1116 KB |
Output is correct |
37 |
Correct |
109 ms |
1112 KB |
Output is correct |
38 |
Correct |
316 ms |
5548 KB |
Output is correct |
39 |
Correct |
319 ms |
5548 KB |
Output is correct |
40 |
Correct |
362 ms |
5548 KB |
Output is correct |
41 |
Correct |
330 ms |
5612 KB |
Output is correct |
42 |
Correct |
371 ms |
5552 KB |
Output is correct |
43 |
Correct |
336 ms |
7180 KB |
Output is correct |
44 |
Correct |
364 ms |
7344 KB |
Output is correct |
45 |
Correct |
341 ms |
7176 KB |
Output is correct |
46 |
Correct |
334 ms |
7184 KB |
Output is correct |
47 |
Correct |
330 ms |
7088 KB |
Output is correct |
48 |
Correct |
441 ms |
5812 KB |
Output is correct |
49 |
Correct |
439 ms |
5808 KB |
Output is correct |
50 |
Correct |
385 ms |
5776 KB |
Output is correct |
51 |
Correct |
369 ms |
5788 KB |
Output is correct |
52 |
Correct |
551 ms |
5804 KB |
Output is correct |
53 |
Correct |
498 ms |
5936 KB |
Output is correct |
54 |
Correct |
434 ms |
5804 KB |
Output is correct |