#include "bits/stdc++.h"
using namespace std;
// #define endl '\n'
#define int long long
struct node {
int s, e, m, val;
node *l, *r;
node (int _s, int _e) {
s = _s;
e = _e;
m = (s + e)/2;
val = 0;
if (s == e) return;
l = new node(s, m);
r = new node(m+1, e);
}
void upd(int p, int v) {
if (s == p && s== e) {
val = v;
return;
}
if (p <= m) {
l->upd(p, v);
}
else {
r->upd(p, v);
}
val = l->val + r->val; // transition
}
int qry(int qs, int qe) {
if (qs <= s && e <= qe) {
return val;
}
else if (qs > e || s > qe) {
return 0; // base case
}
else {
return l->qry(qs, qe) + r->qry(qs, qe); // transition
}
}
} *root;
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, int> > t;
for (int i=0; i<n; i++) {
int x;
cin >> x;
t.push_back(make_pair(x, i));
}
root = new node(0, n-1);
sort(t.begin(), t.end());
vector<vector<int> > pos;
pos.push_back(vector<int>());
for (int i=0; i<t.size(); i++) {
if (i != 0 && t[i].first != t[i-1].first) pos.push_back(vector<int>());
pos.back().push_back(t[i].second);
}
int ans = 0;
for (int i=0; i<pos.size(); i++) {
for (int j=0; j<pos[i].size(); j++) {
ans += (root->qry(0, pos[i][j]-1) * root->qry(pos[i][j]+1, n-1));
}
for (int j=0; j<pos[i].size(); j++) {
root->upd(pos[i][j], 1);
}
}
cout << ans << endl;
}
Compilation message
Mountains.cpp: In function 'int main()':
Mountains.cpp:67:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<t.size(); i++) {
~^~~~~~~~~
Mountains.cpp:74:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<pos.size(); i++) {
~^~~~~~~~~~~
Mountains.cpp:75:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<pos[i].size(); j++) {
~^~~~~~~~~~~~~~
Mountains.cpp:78:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0; j<pos[i].size(); j++) {
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
254 ms |
68696 KB |
Output is correct |
3 |
Correct |
264 ms |
68692 KB |
Output is correct |
4 |
Correct |
285 ms |
68692 KB |
Output is correct |
5 |
Correct |
255 ms |
68704 KB |
Output is correct |
6 |
Correct |
264 ms |
68692 KB |
Output is correct |
7 |
Correct |
251 ms |
68796 KB |
Output is correct |
8 |
Correct |
262 ms |
68696 KB |
Output is correct |
9 |
Correct |
287 ms |
59604 KB |
Output is correct |
10 |
Correct |
257 ms |
59604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
47444 KB |
Output is correct |
2 |
Correct |
237 ms |
47444 KB |
Output is correct |
3 |
Correct |
212 ms |
47580 KB |
Output is correct |
4 |
Correct |
216 ms |
47572 KB |
Output is correct |
5 |
Correct |
215 ms |
47572 KB |
Output is correct |
6 |
Correct |
210 ms |
47516 KB |
Output is correct |
7 |
Correct |
211 ms |
47572 KB |
Output is correct |
8 |
Correct |
194 ms |
47444 KB |
Output is correct |
9 |
Correct |
188 ms |
47444 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
47444 KB |
Output is correct |
2 |
Correct |
237 ms |
47444 KB |
Output is correct |
3 |
Correct |
212 ms |
47580 KB |
Output is correct |
4 |
Correct |
216 ms |
47572 KB |
Output is correct |
5 |
Correct |
215 ms |
47572 KB |
Output is correct |
6 |
Correct |
210 ms |
47516 KB |
Output is correct |
7 |
Correct |
211 ms |
47572 KB |
Output is correct |
8 |
Correct |
194 ms |
47444 KB |
Output is correct |
9 |
Correct |
188 ms |
47444 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
472 ms |
46380 KB |
Output is correct |
12 |
Correct |
621 ms |
46292 KB |
Output is correct |
13 |
Correct |
465 ms |
46548 KB |
Output is correct |
14 |
Correct |
509 ms |
46420 KB |
Output is correct |
15 |
Correct |
626 ms |
46296 KB |
Output is correct |
16 |
Correct |
549 ms |
46420 KB |
Output is correct |
17 |
Correct |
623 ms |
46292 KB |
Output is correct |
18 |
Correct |
193 ms |
46292 KB |
Output is correct |
19 |
Correct |
186 ms |
46400 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
564 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
564 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
15 ms |
2560 KB |
Output is correct |
12 |
Correct |
15 ms |
2608 KB |
Output is correct |
13 |
Correct |
16 ms |
2560 KB |
Output is correct |
14 |
Correct |
16 ms |
2560 KB |
Output is correct |
15 |
Correct |
15 ms |
2560 KB |
Output is correct |
16 |
Correct |
15 ms |
2560 KB |
Output is correct |
17 |
Correct |
15 ms |
2560 KB |
Output is correct |
18 |
Correct |
16 ms |
2304 KB |
Output is correct |
19 |
Correct |
13 ms |
1968 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
47444 KB |
Output is correct |
2 |
Correct |
237 ms |
47444 KB |
Output is correct |
3 |
Correct |
212 ms |
47580 KB |
Output is correct |
4 |
Correct |
216 ms |
47572 KB |
Output is correct |
5 |
Correct |
215 ms |
47572 KB |
Output is correct |
6 |
Correct |
210 ms |
47516 KB |
Output is correct |
7 |
Correct |
211 ms |
47572 KB |
Output is correct |
8 |
Correct |
194 ms |
47444 KB |
Output is correct |
9 |
Correct |
188 ms |
47444 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
472 ms |
46380 KB |
Output is correct |
12 |
Correct |
621 ms |
46292 KB |
Output is correct |
13 |
Correct |
465 ms |
46548 KB |
Output is correct |
14 |
Correct |
509 ms |
46420 KB |
Output is correct |
15 |
Correct |
626 ms |
46296 KB |
Output is correct |
16 |
Correct |
549 ms |
46420 KB |
Output is correct |
17 |
Correct |
623 ms |
46292 KB |
Output is correct |
18 |
Correct |
193 ms |
46292 KB |
Output is correct |
19 |
Correct |
186 ms |
46400 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
568 ms |
51284 KB |
Output is correct |
22 |
Correct |
557 ms |
51380 KB |
Output is correct |
23 |
Correct |
471 ms |
51288 KB |
Output is correct |
24 |
Correct |
574 ms |
51156 KB |
Output is correct |
25 |
Correct |
553 ms |
51284 KB |
Output is correct |
26 |
Correct |
490 ms |
51284 KB |
Output is correct |
27 |
Correct |
563 ms |
51264 KB |
Output is correct |
28 |
Correct |
222 ms |
51156 KB |
Output is correct |
29 |
Correct |
221 ms |
51160 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
254 ms |
68696 KB |
Output is correct |
3 |
Correct |
264 ms |
68692 KB |
Output is correct |
4 |
Correct |
285 ms |
68692 KB |
Output is correct |
5 |
Correct |
255 ms |
68704 KB |
Output is correct |
6 |
Correct |
264 ms |
68692 KB |
Output is correct |
7 |
Correct |
251 ms |
68796 KB |
Output is correct |
8 |
Correct |
262 ms |
68696 KB |
Output is correct |
9 |
Correct |
287 ms |
59604 KB |
Output is correct |
10 |
Correct |
257 ms |
59604 KB |
Output is correct |
11 |
Correct |
224 ms |
47444 KB |
Output is correct |
12 |
Correct |
237 ms |
47444 KB |
Output is correct |
13 |
Correct |
212 ms |
47580 KB |
Output is correct |
14 |
Correct |
216 ms |
47572 KB |
Output is correct |
15 |
Correct |
215 ms |
47572 KB |
Output is correct |
16 |
Correct |
210 ms |
47516 KB |
Output is correct |
17 |
Correct |
211 ms |
47572 KB |
Output is correct |
18 |
Correct |
194 ms |
47444 KB |
Output is correct |
19 |
Correct |
188 ms |
47444 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
472 ms |
46380 KB |
Output is correct |
22 |
Correct |
621 ms |
46292 KB |
Output is correct |
23 |
Correct |
465 ms |
46548 KB |
Output is correct |
24 |
Correct |
509 ms |
46420 KB |
Output is correct |
25 |
Correct |
626 ms |
46296 KB |
Output is correct |
26 |
Correct |
549 ms |
46420 KB |
Output is correct |
27 |
Correct |
623 ms |
46292 KB |
Output is correct |
28 |
Correct |
193 ms |
46292 KB |
Output is correct |
29 |
Correct |
186 ms |
46400 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
6 ms |
564 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
512 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
15 ms |
2560 KB |
Output is correct |
42 |
Correct |
15 ms |
2608 KB |
Output is correct |
43 |
Correct |
16 ms |
2560 KB |
Output is correct |
44 |
Correct |
16 ms |
2560 KB |
Output is correct |
45 |
Correct |
15 ms |
2560 KB |
Output is correct |
46 |
Correct |
15 ms |
2560 KB |
Output is correct |
47 |
Correct |
15 ms |
2560 KB |
Output is correct |
48 |
Correct |
16 ms |
2304 KB |
Output is correct |
49 |
Correct |
13 ms |
1968 KB |
Output is correct |
50 |
Correct |
5 ms |
384 KB |
Output is correct |
51 |
Correct |
568 ms |
51284 KB |
Output is correct |
52 |
Correct |
557 ms |
51380 KB |
Output is correct |
53 |
Correct |
471 ms |
51288 KB |
Output is correct |
54 |
Correct |
574 ms |
51156 KB |
Output is correct |
55 |
Correct |
553 ms |
51284 KB |
Output is correct |
56 |
Correct |
490 ms |
51284 KB |
Output is correct |
57 |
Correct |
563 ms |
51264 KB |
Output is correct |
58 |
Correct |
222 ms |
51156 KB |
Output is correct |
59 |
Correct |
221 ms |
51160 KB |
Output is correct |
60 |
Correct |
5 ms |
384 KB |
Output is correct |
61 |
Correct |
610 ms |
68668 KB |
Output is correct |
62 |
Correct |
597 ms |
68692 KB |
Output is correct |
63 |
Correct |
517 ms |
65364 KB |
Output is correct |
64 |
Correct |
499 ms |
63188 KB |
Output is correct |
65 |
Correct |
560 ms |
63188 KB |
Output is correct |
66 |
Correct |
541 ms |
63164 KB |
Output is correct |
67 |
Correct |
568 ms |
63316 KB |
Output is correct |
68 |
Correct |
504 ms |
54100 KB |
Output is correct |
69 |
Correct |
471 ms |
50896 KB |
Output is correct |
70 |
Correct |
5 ms |
384 KB |
Output is correct |