#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int oo = 1e9, mod = 1e9 + 7;
const int S=450, N=2e5+1, M=N*2+10;
int n, a[N], s[N], t[M];
int cnt[N], mp[N];
void update(int pos, int val){
for (int i=pos; i<M; i+=i&(-i)) t[i]+=val;
}
int get(int pos){
if (pos<0) return 0;
int sum=0;
for (int i=pos; i; i-=i&(-i)) sum+=t[i];
return sum;
}
void process(){
cin >> n;
long long ans=0;
for (int i=1; i<=n; ++i) cin >> a[i];
vector<int> sus(a+1, a+n+1);
sort(sus.begin(), sus.end()); sus.resize(unique(sus.begin(), sus.end())-sus.begin());
for (int i=1; i<=n; ++i) a[i]=lower_bound(sus.begin(), sus.end(), a[i])-sus.begin()+1;
for (int i=1; i<=n; ++i) ++mp[a[i]];
for (int l=1; l<=n; ++l){
int mx=0;
for (int r=l; r<=n && r-l+1<=2*S; ++r){
if (mp[a[r]]<=S){
++cnt[a[r]];
mx=max(mx, cnt[a[r]]);
}
if (mx*2>r-l+1) ++ans;
}
for (int r=l; r<=n && r-l+1<=2*S; ++r){
if (mp[a[r]]<=S) --cnt[a[r]];
}
}
for (int i=1; i<=n; ++i){
if (mp[i]<=S) continue;
memset(s, 0, sizeof s);
memset(t, 0, sizeof t);
for (int j=1; j<=n; ++j) if (a[j]==i) ++s[j]; else --s[j];
partial_sum(s, s+n+1, s);
update(0+n+1, 1);
for (int j=1; j<=n; ++j){
ans+=get(s[j]-1+n+1);
update(s[j]+n+1, 1);
}
}
cout << ans;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2396 KB |
Output is correct |
10 |
Correct |
6 ms |
2392 KB |
Output is correct |
11 |
Correct |
4 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2396 KB |
Output is correct |
13 |
Correct |
6 ms |
2392 KB |
Output is correct |
14 |
Correct |
4 ms |
2396 KB |
Output is correct |
15 |
Correct |
5 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
2396 KB |
Output is correct |
17 |
Correct |
4 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
8124 KB |
Output is correct |
2 |
Correct |
293 ms |
9112 KB |
Output is correct |
3 |
Correct |
162 ms |
7760 KB |
Output is correct |
4 |
Correct |
292 ms |
9044 KB |
Output is correct |
5 |
Correct |
288 ms |
9372 KB |
Output is correct |
6 |
Correct |
340 ms |
9564 KB |
Output is correct |
7 |
Correct |
335 ms |
9572 KB |
Output is correct |
8 |
Correct |
313 ms |
9516 KB |
Output is correct |
9 |
Correct |
316 ms |
9564 KB |
Output is correct |
10 |
Correct |
334 ms |
9568 KB |
Output is correct |
11 |
Correct |
322 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2396 KB |
Output is correct |
10 |
Correct |
6 ms |
2392 KB |
Output is correct |
11 |
Correct |
4 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2396 KB |
Output is correct |
13 |
Correct |
6 ms |
2392 KB |
Output is correct |
14 |
Correct |
4 ms |
2396 KB |
Output is correct |
15 |
Correct |
5 ms |
2396 KB |
Output is correct |
16 |
Correct |
5 ms |
2396 KB |
Output is correct |
17 |
Correct |
4 ms |
6492 KB |
Output is correct |
18 |
Correct |
205 ms |
8124 KB |
Output is correct |
19 |
Correct |
293 ms |
9112 KB |
Output is correct |
20 |
Correct |
162 ms |
7760 KB |
Output is correct |
21 |
Correct |
292 ms |
9044 KB |
Output is correct |
22 |
Correct |
288 ms |
9372 KB |
Output is correct |
23 |
Correct |
340 ms |
9564 KB |
Output is correct |
24 |
Correct |
335 ms |
9572 KB |
Output is correct |
25 |
Correct |
313 ms |
9516 KB |
Output is correct |
26 |
Correct |
316 ms |
9564 KB |
Output is correct |
27 |
Correct |
334 ms |
9568 KB |
Output is correct |
28 |
Correct |
322 ms |
9564 KB |
Output is correct |
29 |
Correct |
318 ms |
10884 KB |
Output is correct |
30 |
Correct |
127 ms |
5212 KB |
Output is correct |
31 |
Correct |
257 ms |
5724 KB |
Output is correct |
32 |
Correct |
711 ms |
8400 KB |
Output is correct |
33 |
Correct |
288 ms |
5840 KB |
Output is correct |
34 |
Correct |
304 ms |
5872 KB |
Output is correct |
35 |
Correct |
196 ms |
5340 KB |
Output is correct |
36 |
Correct |
112 ms |
5080 KB |
Output is correct |
37 |
Correct |
136 ms |
5148 KB |
Output is correct |
38 |
Correct |
361 ms |
9572 KB |
Output is correct |
39 |
Correct |
339 ms |
9556 KB |
Output is correct |
40 |
Correct |
331 ms |
9568 KB |
Output is correct |
41 |
Correct |
320 ms |
9556 KB |
Output is correct |
42 |
Correct |
349 ms |
9580 KB |
Output is correct |
43 |
Correct |
378 ms |
11152 KB |
Output is correct |
44 |
Correct |
389 ms |
11156 KB |
Output is correct |
45 |
Correct |
420 ms |
11092 KB |
Output is correct |
46 |
Correct |
429 ms |
11148 KB |
Output is correct |
47 |
Correct |
379 ms |
11160 KB |
Output is correct |
48 |
Correct |
1557 ms |
9872 KB |
Output is correct |
49 |
Correct |
1577 ms |
10068 KB |
Output is correct |
50 |
Correct |
795 ms |
9740 KB |
Output is correct |
51 |
Correct |
827 ms |
9748 KB |
Output is correct |
52 |
Correct |
2697 ms |
9924 KB |
Output is correct |
53 |
Correct |
2476 ms |
9916 KB |
Output is correct |
54 |
Correct |
1520 ms |
9868 KB |
Output is correct |