#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 4e5;
int num[N + 1],n;
map<int,vector<int>> mp;
struct node{
int len = 0,val = 0,sum = 0;
};
int lz[4*N + 1];
node seg[4*N + 1];
void build(int id,int l,int r) {
seg[id].len = (r - l + 1);
if(l == r) {
return;
}
int mid = (l + r)/2;
build(id*2,l,mid);
build(id*2 + 1,mid + 1,r);
}
node merge(node i1,node i2) {
node res;
res.len = i1.len + i2.len;
res.val = (i1.val + i1.sum * i2.len + i2.val);
res.sum = (i1.sum + i2.sum);
return res;
}
void add(int id,int l,int r,int x) {
lz[id] += x;
int sz = seg[id].len,asd = (sz + 1) * sz / 2;
seg[id].sum += (x * sz);
seg[id].val += (x * asd);
}
void down(int id,int l,int r) {
int t = lz[id];
if(lz[id] == 0) return;
lz[id] = 0;
int mid = (l + r)/2;
add(id*2,l,mid,t);
add(id*2 + 1,mid + 1,r,t);
}
void update(int id,int l,int r,int u,int v,int x) {
if(v < l || u > r) return;
if(u <= l && v >= r) {
add(id,l,r,x);
return;
}
down(id,l,r);
int mid = (l + r)/2;
update(id*2,l,mid,u,v,x);
update(id*2 + 1,mid + 1,r,u,v,x);
seg[id] = merge(seg[id*2],seg[id*2 + 1]);
}
node query(int id,int l,int r,int u,int v) {
if(v < l || u > r) return {0,0,0};
if(u <= l && v >= r) {
// cout<<l<<" "<<r<<endl;
// cout<<seg[id].len<<" "<<seg[id].val<<" "<<seg[id].sum<<endl;
return seg[id];
}
down(id,l,r);
int mid = (l + r)/2;
return merge(query(id*2,l,mid,u,v),query(id*2 + 1,mid + 1,r,u,v));
}
struct change{
int l,r,x;
};
vector<change> thay;
inline void upd(int l,int r,int x) {
thay.push_back({l,r,-x});
update(1,0,2*n,l,r,x);
}
int res = 0;
inline void cal(vector<int> st) {
int lst = 0;
upd(n - (st[0] - 1),n,1);
for(int i = 0;i < st.size();i++) {
int nxt;
lst++;
if(i < st.size() - 1) {
nxt = st[i + 1] - 1;
} else {
nxt = n;
}
node tmp = query(1,0,2*n,2*lst + n - nxt - 1,2*lst + n - st[i] - 1);
res += tmp.val;
tmp = query(1,0,2*n,0,2*lst + n - nxt - 2);
res += tmp.sum * (nxt - st[i] + 1);
upd(n - nxt + 2*lst,n - st[i] + 2*lst,1);
}
for(auto u : thay) {
update(1,0,2*n,u.l,u.r,u.x);
}
thay.clear();
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cin>>n;
for(int i = 1;i <= n;i++) {
cin>>num[i];
mp[num[i]].push_back(i);
}
build(1,0,2*n);
for(auto u : mp) {
cal(u.second);
}
cout<<res<<endl;
}
Compilation message
Main.cpp: In function 'void cal(std::vector<long long int>)':
Main.cpp:112:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0;i < st.size();i++) {
| ~~^~~~~~~~~~~
Main.cpp:117:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | if(i < st.size() - 1) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
656 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
28096 KB |
Output is correct |
2 |
Correct |
179 ms |
32920 KB |
Output is correct |
3 |
Correct |
107 ms |
17008 KB |
Output is correct |
4 |
Correct |
182 ms |
33096 KB |
Output is correct |
5 |
Correct |
193 ms |
33184 KB |
Output is correct |
6 |
Correct |
202 ms |
33472 KB |
Output is correct |
7 |
Correct |
202 ms |
33520 KB |
Output is correct |
8 |
Correct |
202 ms |
33516 KB |
Output is correct |
9 |
Correct |
197 ms |
33496 KB |
Output is correct |
10 |
Correct |
207 ms |
33556 KB |
Output is correct |
11 |
Correct |
200 ms |
40916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
656 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
132 ms |
28096 KB |
Output is correct |
19 |
Correct |
179 ms |
32920 KB |
Output is correct |
20 |
Correct |
107 ms |
17008 KB |
Output is correct |
21 |
Correct |
182 ms |
33096 KB |
Output is correct |
22 |
Correct |
193 ms |
33184 KB |
Output is correct |
23 |
Correct |
202 ms |
33472 KB |
Output is correct |
24 |
Correct |
202 ms |
33520 KB |
Output is correct |
25 |
Correct |
202 ms |
33516 KB |
Output is correct |
26 |
Correct |
197 ms |
33496 KB |
Output is correct |
27 |
Correct |
207 ms |
33556 KB |
Output is correct |
28 |
Correct |
200 ms |
40916 KB |
Output is correct |
29 |
Correct |
211 ms |
40884 KB |
Output is correct |
30 |
Correct |
105 ms |
11700 KB |
Output is correct |
31 |
Correct |
216 ms |
22308 KB |
Output is correct |
32 |
Correct |
586 ms |
44828 KB |
Output is correct |
33 |
Correct |
223 ms |
23248 KB |
Output is correct |
34 |
Correct |
238 ms |
23500 KB |
Output is correct |
35 |
Correct |
150 ms |
13640 KB |
Output is correct |
36 |
Correct |
90 ms |
10736 KB |
Output is correct |
37 |
Correct |
103 ms |
12128 KB |
Output is correct |
38 |
Correct |
225 ms |
33060 KB |
Output is correct |
39 |
Correct |
233 ms |
33072 KB |
Output is correct |
40 |
Correct |
225 ms |
33000 KB |
Output is correct |
41 |
Correct |
229 ms |
32996 KB |
Output is correct |
42 |
Correct |
241 ms |
32960 KB |
Output is correct |
43 |
Correct |
277 ms |
33196 KB |
Output is correct |
44 |
Correct |
274 ms |
33384 KB |
Output is correct |
45 |
Correct |
274 ms |
33060 KB |
Output is correct |
46 |
Correct |
274 ms |
33160 KB |
Output is correct |
47 |
Correct |
312 ms |
33292 KB |
Output is correct |
48 |
Correct |
381 ms |
33520 KB |
Output is correct |
49 |
Correct |
393 ms |
33432 KB |
Output is correct |
50 |
Correct |
329 ms |
34112 KB |
Output is correct |
51 |
Correct |
358 ms |
34076 KB |
Output is correct |
52 |
Correct |
353 ms |
33228 KB |
Output is correct |
53 |
Correct |
393 ms |
32996 KB |
Output is correct |
54 |
Correct |
373 ms |
33216 KB |
Output is correct |