#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
struct BIT {
int n;
vector<int> tree;
BIT(int _n) {
n = _n + 10;
tree.resize(_n+60);
}
void update(int p, int v) {
for(p++; p<n; p+=p&-p) tree[p] += v;
}
int query(int p) {
int ans = 0;
for(p++; p>0; p-=p&-p) ans += tree[p];
return ans;
}
int query(int l, int r) { return query(r) - query(l-1); }
void reset() { for(int &x : tree) x = 0; }
};
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n;
ll ans = 0;
cin >> n;
int B = sqrt(n);
vector<int> v(n+1), comp;
map<int, int> cnt;
for(int i=1; i<=n; i++) {
cin >> v[i];
if(++cnt[v[i]] == 1) comp.push_back(v[i]);
}
BIT bit(2*n+1000);
for(int &x : comp) {
if(cnt[x] >= B) {
int sum = 0;
bit.update(n+1, 1);
for(int i=1; i<=n; i++) {
sum += (v[i] == x);
ans += bit.query(2*sum - i - 1 + (n + 1));
bit.update(2*sum - i + (n + 1), 1);
}
bit.reset();
} else {
ans += cnt[x];
vector<int> p; p.push_back(0);
for(int i=1; i<=n; i++)
if(v[i] == x) p.push_back(i);
p.push_back(n+1);
for(int i=1; i<p.size()-1; i++) {
for(int j=i+1; j<p.size()-1; j++) {
if(2 * (j - i + 1) <= p[j] - p[i] + 1) continue;
int left = 2 * (j - i + 1) - (p[j] - p[i] + 1) - 1;
int L = min(left, p[i] - p[i-1] - 1);
int R = p[j+1] - p[j] - 1;
//for(int k=0; k<=L; k++) ans += min(R, left - k);
ans += L + 1;
if(left < R) {
ans += 1ll * left * (left + 1) / 2;
} else {
ans += R * (left - R + 1);
ans += 1ll * (R - 1) * R / 2;
}
}
}
}
}
cout << ans << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:66:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=1; i<p.size()-1; i++) {
| ~^~~~~~~~~~~
Main.cpp:67:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int j=i+1; j<p.size()-1; j++) {
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
1884 KB |
Output is correct |
2 |
Correct |
16 ms |
2396 KB |
Output is correct |
3 |
Correct |
9 ms |
1628 KB |
Output is correct |
4 |
Correct |
17 ms |
2612 KB |
Output is correct |
5 |
Correct |
17 ms |
2652 KB |
Output is correct |
6 |
Correct |
19 ms |
2652 KB |
Output is correct |
7 |
Correct |
26 ms |
2812 KB |
Output is correct |
8 |
Correct |
18 ms |
2652 KB |
Output is correct |
9 |
Correct |
18 ms |
2828 KB |
Output is correct |
10 |
Correct |
18 ms |
2648 KB |
Output is correct |
11 |
Correct |
12 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
12 ms |
1884 KB |
Output is correct |
19 |
Correct |
16 ms |
2396 KB |
Output is correct |
20 |
Correct |
9 ms |
1628 KB |
Output is correct |
21 |
Correct |
17 ms |
2612 KB |
Output is correct |
22 |
Correct |
17 ms |
2652 KB |
Output is correct |
23 |
Correct |
19 ms |
2652 KB |
Output is correct |
24 |
Correct |
26 ms |
2812 KB |
Output is correct |
25 |
Correct |
18 ms |
2652 KB |
Output is correct |
26 |
Correct |
18 ms |
2828 KB |
Output is correct |
27 |
Correct |
18 ms |
2648 KB |
Output is correct |
28 |
Correct |
12 ms |
2652 KB |
Output is correct |
29 |
Correct |
20 ms |
2652 KB |
Output is correct |
30 |
Correct |
805 ms |
2720 KB |
Output is correct |
31 |
Execution timed out |
3006 ms |
4592 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |