#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
struct segTree{
vector<ll> tree , tree2;
vector<pair<ll,bool>> lazy;
int n;
segTree(int _n){
n = _n;
tree.resize(4*(2*n+1)+5) , tree2.resize(4*(2*n+1)*5) , lazy.resize(4*(2*n+1)+5);
}
void prop(int node,int l,int r){
if(lazy[node].second){
tree[node] = 0;
tree2[node] = 0;
if(l != r){
lazy[node << 1] = {0,1};
lazy[node << 1 | 1] = {0,1};
}
lazy[node].second = 0;
}
tree[node] += lazy[node].first * (r - l + 1);
ll sz = r - l + 1;
ll x = sz * (sz + 1) / 2;
tree2[node] += (x + sz * (2*n+1 - r)) * lazy[node].first;
if(l != r){
lazy[node << 1].first += lazy[node].first;
lazy[node << 1 | 1].first += lazy[node].first;
}
lazy[node].first = 0;
}
void update(int node,int l,int r,int lQ,int rQ){
if(l > r) return;
if(lazy[node].first || lazy[node].second) prop(node,l,r);
if(l > rQ || lQ > r) return;
if(lQ <= l && r <= rQ) return lazy[node].first ++, prop(node,l,r) , void();
int md = l + r >> 1;
update(node<<1,l,md,lQ,rQ) , update(node<<1|1,md+1,r,lQ,rQ);
tree[node] = tree[node << 1] + tree[node << 1 | 1];
tree2[node] = tree2[node << 1] + tree2[node << 1 | 1];
}
ll query1(int node,int l,int r,int lQ,int rQ){
if(l > r) return 0;
if(lazy[node].first || lazy[node].second) prop(node,l,r);
if(l > rQ || lQ > r) return 0;
if(lQ <= l && r <= rQ) return tree[node];
int md = l + r >> 1;
return query1(node<<1,l,md,lQ,rQ) + query1(node<<1|1,md+1,r,lQ,rQ);
}
ll query2(int node,int l,int r,int lQ,int rQ){
if(l > r) return 0;
if(lazy[node].first || lazy[node].second) prop(node,l,r);
if(l > rQ || lQ > r) return 0;
if(lQ <= l && r <= rQ) return tree2[node];
int md = l + r >> 1;
return query2(node<<1,l,md,lQ,rQ) + query2(node<<1|1,md+1,r,lQ,rQ);
}
void clear(){
lazy[1] = {0,1};
prop(1,1,2*n+1);
}
void update(int l,int r){
if(l > r) swap(l,r);
update(1,1,2*n+1,l+n+1,r+n+1);
}
ll query1(int l,int r){
if(l > r) swap(l,r);
return query1(1,1,2*n+1,l+n+1,r+n+1);
}
ll query2(int l,int r){
if(l > r) swap(l,r);
return query2(1,1,2*n+1,l+n+1,r+n+1);
}
};
void compress(vector<int> &a){
int n = a.size();
vector<pair<int,int>> b(n);
for(int i=0;i<n;i++){
b[i].first = a[i];
b[i].second = i;
}
sort(b.begin(),b.end());
a[b[0].second] = 1;
int cnt = 1;
for(int i=1;i<n;i++){
if(b[i].first == b[i-1].first){
a[b[i].second] = a[b[i-1].second];
}
else a[b[i].second] = ++cnt;
}
}
void solve(){
int n;
cin>>n;
vector<int> a(n);
for(auto &i:a) cin>>i;
compress(a);
vector<vector<int>> idx(n+1);
for(int i=0;i<n;i++) idx[a[i]].push_back(i);
segTree sg(n);
ll ans = 0;
for(auto v : idx){
if(v.empty()) continue;
int p = 0;
v.push_back(n);
sg.update(p,p-v[0]);
for(int i=0;i<v.size()-1;i++){
p = 2 * i - v[i] + 1;
int cnt = v[i+1] - v[i];
ans += sg.query1(-n,p-cnt) * cnt;
if(cnt > 1) ans += sg.query2(p-cnt+1,p-1) - sg.query1(p-cnt+1,p-1) * (n-(p-1));
sg.update(p,p-cnt+1);
}
sg.clear();
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message
Main.cpp: In member function 'void segTree::update(int, int, int, int, int)':
Main.cpp:38:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int md = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'll segTree::query1(int, int, int, int, int)':
Main.cpp:48:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int md = l + r >> 1;
| ~~^~~
Main.cpp: In member function 'll segTree::query2(int, int, int, int, int)':
Main.cpp:56:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int md = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void solve()':
Main.cpp:108:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i=0;i<v.size()-1;i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
72208 KB |
Output is correct |
2 |
Correct |
116 ms |
94368 KB |
Output is correct |
3 |
Correct |
63 ms |
55352 KB |
Output is correct |
4 |
Correct |
116 ms |
98652 KB |
Output is correct |
5 |
Correct |
123 ms |
102492 KB |
Output is correct |
6 |
Correct |
127 ms |
108344 KB |
Output is correct |
7 |
Correct |
130 ms |
108752 KB |
Output is correct |
8 |
Correct |
128 ms |
108420 KB |
Output is correct |
9 |
Correct |
129 ms |
108340 KB |
Output is correct |
10 |
Correct |
132 ms |
108596 KB |
Output is correct |
11 |
Correct |
90 ms |
109112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
1372 KB |
Output is correct |
10 |
Correct |
3 ms |
1628 KB |
Output is correct |
11 |
Correct |
2 ms |
1372 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
2 ms |
1372 KB |
Output is correct |
14 |
Correct |
2 ms |
1372 KB |
Output is correct |
15 |
Correct |
3 ms |
1372 KB |
Output is correct |
16 |
Correct |
2 ms |
1368 KB |
Output is correct |
17 |
Correct |
2 ms |
1384 KB |
Output is correct |
18 |
Correct |
87 ms |
72208 KB |
Output is correct |
19 |
Correct |
116 ms |
94368 KB |
Output is correct |
20 |
Correct |
63 ms |
55352 KB |
Output is correct |
21 |
Correct |
116 ms |
98652 KB |
Output is correct |
22 |
Correct |
123 ms |
102492 KB |
Output is correct |
23 |
Correct |
127 ms |
108344 KB |
Output is correct |
24 |
Correct |
130 ms |
108752 KB |
Output is correct |
25 |
Correct |
128 ms |
108420 KB |
Output is correct |
26 |
Correct |
129 ms |
108340 KB |
Output is correct |
27 |
Correct |
132 ms |
108596 KB |
Output is correct |
28 |
Correct |
90 ms |
109112 KB |
Output is correct |
29 |
Correct |
108 ms |
110648 KB |
Output is correct |
30 |
Correct |
82 ms |
21808 KB |
Output is correct |
31 |
Correct |
178 ms |
43440 KB |
Output is correct |
32 |
Correct |
485 ms |
105032 KB |
Output is correct |
33 |
Correct |
185 ms |
46044 KB |
Output is correct |
34 |
Correct |
198 ms |
48336 KB |
Output is correct |
35 |
Correct |
142 ms |
31608 KB |
Output is correct |
36 |
Correct |
73 ms |
19032 KB |
Output is correct |
37 |
Correct |
89 ms |
23572 KB |
Output is correct |
38 |
Correct |
171 ms |
107832 KB |
Output is correct |
39 |
Correct |
172 ms |
107828 KB |
Output is correct |
40 |
Correct |
180 ms |
108080 KB |
Output is correct |
41 |
Correct |
174 ms |
108100 KB |
Output is correct |
42 |
Correct |
171 ms |
107852 KB |
Output is correct |
43 |
Correct |
227 ms |
109876 KB |
Output is correct |
44 |
Correct |
232 ms |
109436 KB |
Output is correct |
45 |
Correct |
212 ms |
109440 KB |
Output is correct |
46 |
Correct |
224 ms |
109740 KB |
Output is correct |
47 |
Correct |
218 ms |
109612 KB |
Output is correct |
48 |
Correct |
288 ms |
107872 KB |
Output is correct |
49 |
Correct |
291 ms |
107884 KB |
Output is correct |
50 |
Correct |
284 ms |
108124 KB |
Output is correct |
51 |
Correct |
274 ms |
108136 KB |
Output is correct |
52 |
Correct |
294 ms |
107784 KB |
Output is correct |
53 |
Correct |
304 ms |
107708 KB |
Output is correct |
54 |
Correct |
284 ms |
107760 KB |
Output is correct |