#include <bits/stdc++.h>
using namespace std;
long long N,A[200005];
vector<long long> A1, lst[200005];
struct node {
long long s,e,m,v; pair<long long,long long> lazy; node *l, *r;
node (long long S, long long E) {
s = S, e = E, m = (s+e)/2, v = 0, lazy = make_pair(0,0);
if (s != e) l = new node(s,m), r = new node(m+1,e);
}
void propo() {
if (lazy == make_pair(0ll,0ll)) return;
v += (e-s+1) * (e-s) / 2 * lazy.first + (e-s+1) * lazy.second;
if (s != e) {
l->lazy.first += lazy.first, r->lazy.first += lazy.first;
l->lazy.second += lazy.second, r->lazy.second += lazy.second + (m-s+1) * lazy.first;
}
lazy = make_pair(0,0);
}
void update (long long x, long long y, long long a, long long b) {
propo();
if (s == x && e == y) lazy.first += a, lazy.second += b;
else {
if (y <= m) l -> update(x,y,a,b);
else if (x > m) r -> update(x,y,a,b);
else l -> update(x,m,a,b), r -> update(m+1,y,a,b+(m-x+1)*a);
l->propo(), r->propo();
v = l->v + r->v;
}
}
long long query (long long x, long long y) {
propo();
if (s == x && e == y) return v;
else if (y <= m) return l -> query(x,y);
else if (x > m) return r -> query(x,y);
else return l->query(x,m) + r->query(m+1,y);
}
}*root;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N;
for (long long i = 0; i < N; ++i) {
cin >> A[i];
A1.push_back(A[i]);
}
sort(A1.begin(),A1.end());
A1.resize(unique(A1.begin(),A1.end())-A1.begin());
for (long long i = 0; i < N; ++i) {
A[i] = lower_bound(A1.begin(),A1.end(),A[i])-A1.begin();
lst[A[i]].push_back(lst[A[i]].size()*2-i+1);
}
root = new node(0,N*2);
long long ans = 0;
for (long long i = 0; i < N; ++i) if (!lst[i].empty()) {
root -> update(N+lst[i][0]-1,N,1,1);
root -> update(N+1,N*2,0,2-lst[i][0]);
for (long long j = 1; j < lst[i].size(); ++j) {
ans += root -> query(N+lst[i][j]-2,N+lst[i][j-1]-1);
root -> update(N+lst[i][j]-1,N+lst[i][j-1],1,1);
root -> update(N+lst[i][j-1]+1,N*2,0,lst[i][j-1]-lst[i][j]+2);
}
ans += root -> query(lst[i].size()*2-1,N+lst[i].back()-1);
root -> update(N+lst[i][0]-1,N,-1,-1);
root -> update(N+1,N*2,0,lst[i][0]-2);
for (long long j = 1; j < lst[i].size(); ++j) {
root -> update(N+lst[i][j]-1,N+lst[i][j-1],-1,-1);
root -> update(N+lst[i][j-1]+1,N*2,0,lst[i][j]-lst[i][j-1]-2);
}
}
cout << ans;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:60:33: 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]
60 | for (long long j = 1; j < lst[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~
Main.cpp:68:33: 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]
68 | for (long long j = 1; j < lst[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5000 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5000 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
4 ms |
5284 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
6 ms |
5716 KB |
Output is correct |
10 |
Correct |
5 ms |
5664 KB |
Output is correct |
11 |
Correct |
5 ms |
5708 KB |
Output is correct |
12 |
Correct |
5 ms |
5708 KB |
Output is correct |
13 |
Correct |
6 ms |
5656 KB |
Output is correct |
14 |
Correct |
5 ms |
5672 KB |
Output is correct |
15 |
Correct |
5 ms |
5708 KB |
Output is correct |
16 |
Correct |
5 ms |
5668 KB |
Output is correct |
17 |
Correct |
4 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
50148 KB |
Output is correct |
2 |
Correct |
184 ms |
63528 KB |
Output is correct |
3 |
Correct |
102 ms |
39012 KB |
Output is correct |
4 |
Correct |
175 ms |
66476 KB |
Output is correct |
5 |
Correct |
183 ms |
68784 KB |
Output is correct |
6 |
Correct |
190 ms |
72284 KB |
Output is correct |
7 |
Correct |
241 ms |
72280 KB |
Output is correct |
8 |
Correct |
191 ms |
72320 KB |
Output is correct |
9 |
Correct |
214 ms |
72484 KB |
Output is correct |
10 |
Correct |
198 ms |
72504 KB |
Output is correct |
11 |
Correct |
197 ms |
72360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5000 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
4 ms |
5284 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
6 ms |
5716 KB |
Output is correct |
10 |
Correct |
5 ms |
5664 KB |
Output is correct |
11 |
Correct |
5 ms |
5708 KB |
Output is correct |
12 |
Correct |
5 ms |
5708 KB |
Output is correct |
13 |
Correct |
6 ms |
5656 KB |
Output is correct |
14 |
Correct |
5 ms |
5672 KB |
Output is correct |
15 |
Correct |
5 ms |
5708 KB |
Output is correct |
16 |
Correct |
5 ms |
5668 KB |
Output is correct |
17 |
Correct |
4 ms |
5580 KB |
Output is correct |
18 |
Correct |
125 ms |
50148 KB |
Output is correct |
19 |
Correct |
184 ms |
63528 KB |
Output is correct |
20 |
Correct |
102 ms |
39012 KB |
Output is correct |
21 |
Correct |
175 ms |
66476 KB |
Output is correct |
22 |
Correct |
183 ms |
68784 KB |
Output is correct |
23 |
Correct |
190 ms |
72284 KB |
Output is correct |
24 |
Correct |
241 ms |
72280 KB |
Output is correct |
25 |
Correct |
191 ms |
72320 KB |
Output is correct |
26 |
Correct |
214 ms |
72484 KB |
Output is correct |
27 |
Correct |
198 ms |
72504 KB |
Output is correct |
28 |
Correct |
197 ms |
72360 KB |
Output is correct |
29 |
Correct |
227 ms |
72536 KB |
Output is correct |
30 |
Correct |
62 ms |
18548 KB |
Output is correct |
31 |
Correct |
140 ms |
32192 KB |
Output is correct |
32 |
Correct |
459 ms |
71056 KB |
Output is correct |
33 |
Correct |
162 ms |
33912 KB |
Output is correct |
34 |
Correct |
159 ms |
35284 KB |
Output is correct |
35 |
Correct |
115 ms |
24840 KB |
Output is correct |
36 |
Correct |
53 ms |
16880 KB |
Output is correct |
37 |
Correct |
67 ms |
19404 KB |
Output is correct |
38 |
Correct |
260 ms |
72504 KB |
Output is correct |
39 |
Correct |
266 ms |
72712 KB |
Output is correct |
40 |
Correct |
261 ms |
72504 KB |
Output is correct |
41 |
Correct |
257 ms |
72760 KB |
Output is correct |
42 |
Correct |
280 ms |
72764 KB |
Output is correct |
43 |
Correct |
337 ms |
72844 KB |
Output is correct |
44 |
Correct |
368 ms |
73024 KB |
Output is correct |
45 |
Correct |
338 ms |
72932 KB |
Output is correct |
46 |
Correct |
370 ms |
72968 KB |
Output is correct |
47 |
Correct |
338 ms |
72932 KB |
Output is correct |
48 |
Correct |
495 ms |
72584 KB |
Output is correct |
49 |
Correct |
489 ms |
72528 KB |
Output is correct |
50 |
Correct |
558 ms |
72972 KB |
Output is correct |
51 |
Correct |
569 ms |
73016 KB |
Output is correct |
52 |
Correct |
496 ms |
72428 KB |
Output is correct |
53 |
Correct |
463 ms |
72332 KB |
Output is correct |
54 |
Correct |
527 ms |
72552 KB |
Output is correct |