#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=2e5;
int n, a[mxN];
map<int, vector<int>> mp;
ll ans;
struct {
int sum[1<<20], lz[1<<20];
ll isum[1<<20];
void psh(int u, int l, int r) {
if (lz[u]) {
sum[u]+=lz[u]*(r-l+1);
isum[u]+=(ll)lz[u]*(r-l+1)*(l+r)/2;
if (l!=r)
lz[2*u]+=lz[u], lz[2*u+1]+=lz[u];
lz[u]=0;
}
}
void upd(int ql, int qr, int x, int u=1, int l=0, int r=2*n) {
psh(u, l, r);
if (l>qr||r<ql)
return;
if (ql<=l&&r<=qr) {
lz[u]+=x;
psh(u, l, r);
return;
}
int mid=(l+r)/2;
upd(ql, qr, x, 2*u, l, mid);
upd(ql, qr, x ,2*u+1, mid+1, r);
sum[u]=sum[2*u]+sum[2*u+1];
isum[u]=isum[2*u]+isum[2*u+1];
}
ll qry(int ql, int qr, int t, int u=1, int l=0, int r=2*n) {
psh(u, l, r);
if (l>qr||r<ql)
return 0;
if (ql<=l&&r<=qr)
return t==0?sum[u]:isum[u];
int mid=(l+r)/2;
return qry(ql, qr, t, 2*u, l, mid)+qry(ql, qr, t, 2*u+1, mid+1, r);
}
} st;
void solve(vector<int> v) {
int pref=n;
vector<ar<int, 2>> rb;
if (v[0]) {
st.upd(n-v[0], n-1, 1);
rb.push_back({n-v[0], n-1});
pref-=v[0];
}
for (int i=0; i<v.size(); ++i) {
++pref;
ans+=st.qry(0, pref-1, 0);
//cout << ans << "\n";
st.upd(pref, pref, 1);
rb.push_back({pref, pref});
if (i+1<v.size()&&v[i]+1<v[i+1]||i+1==v.size()&&v[i]<n-1) {
int len=(i+1==v.size()?n-1:v[i+1]-1)-v[i];
//cout << "len " << len << endl;
ans+=len*st.qry(0, pref-len-1, 0);
ans+=(pref-1)*st.qry(pref-len, pref-2, 0)-st.qry(pref-len, pref-2, 1);
st.upd(pref-len, pref-1, 1);
rb.push_back({pref-len, pref-1});
pref-=len;
}
//cout << ans << "\n";
}
for (ar<int, 2> x : rb)
st.upd(x[0], x[1], -1);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i=0; i<n; ++i) {
cin >> a[i];
mp[a[i]].push_back(i);
}
st.upd(n, n, 1);
for (auto x : mp)
solve(x.second); // , cout << ans << "\n";
cout << ans;
return 0;
}
Compilation message
Main.cpp: In function 'void solve(std::vector<int>)':
Main.cpp:58:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i=0; i<v.size(); ++i) {
| ~^~~~~~~~~
Main.cpp:64:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if (i+1<v.size()&&v[i]+1<v[i+1]||i+1==v.size()&&v[i]<n-1) {
| ~~~^~~~~~~~~
Main.cpp:64:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if (i+1<v.size()&&v[i]+1<v[i+1]||i+1==v.size()&&v[i]<n-1) {
| ~~~^~~~~~~~~~
Main.cpp:64:19: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
64 | if (i+1<v.size()&&v[i]+1<v[i+1]||i+1==v.size()&&v[i]<n-1) {
Main.cpp:65:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | int len=(i+1==v.size()?n-1:v[i+1]-1)-v[i];
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
452 KB |
Output is correct |
10 |
Correct |
3 ms |
448 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
444 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
3856 KB |
Output is correct |
2 |
Correct |
191 ms |
4544 KB |
Output is correct |
3 |
Correct |
96 ms |
3260 KB |
Output is correct |
4 |
Correct |
179 ms |
5900 KB |
Output is correct |
5 |
Correct |
204 ms |
5804 KB |
Output is correct |
6 |
Correct |
205 ms |
6136 KB |
Output is correct |
7 |
Correct |
198 ms |
6008 KB |
Output is correct |
8 |
Correct |
211 ms |
6092 KB |
Output is correct |
9 |
Correct |
246 ms |
6012 KB |
Output is correct |
10 |
Correct |
197 ms |
5968 KB |
Output is correct |
11 |
Correct |
141 ms |
12968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
4 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
452 KB |
Output is correct |
10 |
Correct |
3 ms |
448 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
444 KB |
Output is correct |
14 |
Correct |
4 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
452 KB |
Output is correct |
18 |
Correct |
138 ms |
3856 KB |
Output is correct |
19 |
Correct |
191 ms |
4544 KB |
Output is correct |
20 |
Correct |
96 ms |
3260 KB |
Output is correct |
21 |
Correct |
179 ms |
5900 KB |
Output is correct |
22 |
Correct |
204 ms |
5804 KB |
Output is correct |
23 |
Correct |
205 ms |
6136 KB |
Output is correct |
24 |
Correct |
198 ms |
6008 KB |
Output is correct |
25 |
Correct |
211 ms |
6092 KB |
Output is correct |
26 |
Correct |
246 ms |
6012 KB |
Output is correct |
27 |
Correct |
197 ms |
5968 KB |
Output is correct |
28 |
Correct |
141 ms |
12968 KB |
Output is correct |
29 |
Correct |
170 ms |
12940 KB |
Output is correct |
30 |
Correct |
168 ms |
6600 KB |
Output is correct |
31 |
Correct |
347 ms |
12348 KB |
Output is correct |
32 |
Correct |
919 ms |
23740 KB |
Output is correct |
33 |
Correct |
421 ms |
12712 KB |
Output is correct |
34 |
Correct |
399 ms |
13136 KB |
Output is correct |
35 |
Correct |
233 ms |
8132 KB |
Output is correct |
36 |
Correct |
148 ms |
6044 KB |
Output is correct |
37 |
Correct |
169 ms |
7004 KB |
Output is correct |
38 |
Correct |
272 ms |
9304 KB |
Output is correct |
39 |
Correct |
272 ms |
9220 KB |
Output is correct |
40 |
Correct |
279 ms |
9288 KB |
Output is correct |
41 |
Correct |
288 ms |
9236 KB |
Output is correct |
42 |
Correct |
286 ms |
9388 KB |
Output is correct |
43 |
Correct |
355 ms |
11680 KB |
Output is correct |
44 |
Correct |
347 ms |
11688 KB |
Output is correct |
45 |
Correct |
346 ms |
10280 KB |
Output is correct |
46 |
Correct |
381 ms |
9964 KB |
Output is correct |
47 |
Correct |
332 ms |
9988 KB |
Output is correct |
48 |
Correct |
564 ms |
10456 KB |
Output is correct |
49 |
Correct |
476 ms |
10464 KB |
Output is correct |
50 |
Correct |
447 ms |
10820 KB |
Output is correct |
51 |
Correct |
445 ms |
10720 KB |
Output is correct |
52 |
Correct |
465 ms |
10368 KB |
Output is correct |
53 |
Correct |
491 ms |
10268 KB |
Output is correct |
54 |
Correct |
445 ms |
10352 KB |
Output is correct |