#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define int long long
using namespace std;
using ll = long long;
struct SegTree {
int n;
vector<int> tree;
SegTree(int n) : n(n), tree(4*n+5) {}
void update(int u, int tl, int tr, int pos, int val) {
if(tl == tr) {
tree[u] += val;
} else {
int tm = (tl + tr) / 2;
if(pos <= tm)
update(2*u, tl, tm, pos, val);
else
update(2*u+1, tm+1, tr, pos, val);
tree[u] = tree[2*u] + tree[2*u+1];
}
}
int query(int u, int tl, int tr, int l, int r) {
if(tl > tr || l > tr || tl > r) return 0;
if(l <= tl && tr <= r) return tree[u];
int tm = (tl + tr) / 2;
return query(2*u, tl, tm, l, r)
+ query(2*u+1, tm+1, tr, l, r);
}
void update(int pos, int val) {
update(1, 0, n-1, pos, val);
}
int query(int l, int r) {
return query(1, 0, n-1, l, r);
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> v(n);
for(ll &x : v) cin >> x;
vector<ll> tmp(v);
map<ll, int> key;
sort(all(tmp));
uniq(tmp);
for(int i=0; i<tmp.size(); i++)
key[tmp[i]] = i;
vector<int> dp_left(n), dp_right(n);
SegTree tree_left(tmp.size());
SegTree tree_right(tmp.size());
//calcs elements smaller than x left from x
for(int i=0; i<n; i++) {
int pos = key[v[i]];
dp_left[i] = tree_left.query(0, pos-1);
tree_left.update(pos, 1);
}
//calcs elements smaller than x right from x
for(int i=n-1; i>=0; i--) {
int pos = key[v[i]];
dp_right[i] = tree_right.query(0, pos-1);
tree_right.update(pos, 1);
}
ll ans = 0;
for(int i=0; i<n; i++)
ans += 1ll * (dp_left[i] * dp_right[i]);
cout << ans << '\n';
return 0;
}
Compilation message
Mountains.cpp: In function 'int32_t main()':
Mountains.cpp:59:19: 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]
59 | for(int i=0; i<tmp.size(); i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
253 ms |
47284 KB |
Output is correct |
3 |
Correct |
259 ms |
47180 KB |
Output is correct |
4 |
Correct |
252 ms |
47276 KB |
Output is correct |
5 |
Correct |
257 ms |
47288 KB |
Output is correct |
6 |
Correct |
254 ms |
47168 KB |
Output is correct |
7 |
Correct |
257 ms |
47284 KB |
Output is correct |
8 |
Correct |
270 ms |
47180 KB |
Output is correct |
9 |
Correct |
201 ms |
29156 KB |
Output is correct |
10 |
Correct |
202 ms |
29180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9684 KB |
Output is correct |
2 |
Correct |
27 ms |
9684 KB |
Output is correct |
3 |
Correct |
27 ms |
9684 KB |
Output is correct |
4 |
Correct |
27 ms |
9684 KB |
Output is correct |
5 |
Correct |
27 ms |
9684 KB |
Output is correct |
6 |
Correct |
28 ms |
9708 KB |
Output is correct |
7 |
Correct |
26 ms |
9684 KB |
Output is correct |
8 |
Correct |
21 ms |
9684 KB |
Output is correct |
9 |
Correct |
23 ms |
9684 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9684 KB |
Output is correct |
2 |
Correct |
27 ms |
9684 KB |
Output is correct |
3 |
Correct |
27 ms |
9684 KB |
Output is correct |
4 |
Correct |
27 ms |
9684 KB |
Output is correct |
5 |
Correct |
27 ms |
9684 KB |
Output is correct |
6 |
Correct |
28 ms |
9708 KB |
Output is correct |
7 |
Correct |
26 ms |
9684 KB |
Output is correct |
8 |
Correct |
21 ms |
9684 KB |
Output is correct |
9 |
Correct |
23 ms |
9684 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
75 ms |
9684 KB |
Output is correct |
12 |
Correct |
75 ms |
9684 KB |
Output is correct |
13 |
Correct |
79 ms |
9712 KB |
Output is correct |
14 |
Correct |
82 ms |
9680 KB |
Output is correct |
15 |
Correct |
77 ms |
9684 KB |
Output is correct |
16 |
Correct |
76 ms |
9720 KB |
Output is correct |
17 |
Correct |
77 ms |
9716 KB |
Output is correct |
18 |
Correct |
41 ms |
9684 KB |
Output is correct |
19 |
Correct |
41 ms |
9644 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
10 ms |
1876 KB |
Output is correct |
12 |
Correct |
11 ms |
2004 KB |
Output is correct |
13 |
Correct |
10 ms |
1876 KB |
Output is correct |
14 |
Correct |
10 ms |
1876 KB |
Output is correct |
15 |
Correct |
11 ms |
1876 KB |
Output is correct |
16 |
Correct |
11 ms |
1876 KB |
Output is correct |
17 |
Correct |
10 ms |
1876 KB |
Output is correct |
18 |
Correct |
8 ms |
1108 KB |
Output is correct |
19 |
Correct |
5 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9684 KB |
Output is correct |
2 |
Correct |
27 ms |
9684 KB |
Output is correct |
3 |
Correct |
27 ms |
9684 KB |
Output is correct |
4 |
Correct |
27 ms |
9684 KB |
Output is correct |
5 |
Correct |
27 ms |
9684 KB |
Output is correct |
6 |
Correct |
28 ms |
9708 KB |
Output is correct |
7 |
Correct |
26 ms |
9684 KB |
Output is correct |
8 |
Correct |
21 ms |
9684 KB |
Output is correct |
9 |
Correct |
23 ms |
9684 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
75 ms |
9684 KB |
Output is correct |
12 |
Correct |
75 ms |
9684 KB |
Output is correct |
13 |
Correct |
79 ms |
9712 KB |
Output is correct |
14 |
Correct |
82 ms |
9680 KB |
Output is correct |
15 |
Correct |
77 ms |
9684 KB |
Output is correct |
16 |
Correct |
76 ms |
9720 KB |
Output is correct |
17 |
Correct |
77 ms |
9716 KB |
Output is correct |
18 |
Correct |
41 ms |
9684 KB |
Output is correct |
19 |
Correct |
41 ms |
9644 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
372 ms |
21708 KB |
Output is correct |
22 |
Correct |
367 ms |
21596 KB |
Output is correct |
23 |
Correct |
363 ms |
21608 KB |
Output is correct |
24 |
Correct |
366 ms |
21600 KB |
Output is correct |
25 |
Correct |
360 ms |
21692 KB |
Output is correct |
26 |
Correct |
362 ms |
21612 KB |
Output is correct |
27 |
Correct |
363 ms |
21676 KB |
Output is correct |
28 |
Correct |
152 ms |
21716 KB |
Output is correct |
29 |
Correct |
147 ms |
21580 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
253 ms |
47284 KB |
Output is correct |
3 |
Correct |
259 ms |
47180 KB |
Output is correct |
4 |
Correct |
252 ms |
47276 KB |
Output is correct |
5 |
Correct |
257 ms |
47288 KB |
Output is correct |
6 |
Correct |
254 ms |
47168 KB |
Output is correct |
7 |
Correct |
257 ms |
47284 KB |
Output is correct |
8 |
Correct |
270 ms |
47180 KB |
Output is correct |
9 |
Correct |
201 ms |
29156 KB |
Output is correct |
10 |
Correct |
202 ms |
29180 KB |
Output is correct |
11 |
Correct |
27 ms |
9684 KB |
Output is correct |
12 |
Correct |
27 ms |
9684 KB |
Output is correct |
13 |
Correct |
27 ms |
9684 KB |
Output is correct |
14 |
Correct |
27 ms |
9684 KB |
Output is correct |
15 |
Correct |
27 ms |
9684 KB |
Output is correct |
16 |
Correct |
28 ms |
9708 KB |
Output is correct |
17 |
Correct |
26 ms |
9684 KB |
Output is correct |
18 |
Correct |
21 ms |
9684 KB |
Output is correct |
19 |
Correct |
23 ms |
9684 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
75 ms |
9684 KB |
Output is correct |
22 |
Correct |
75 ms |
9684 KB |
Output is correct |
23 |
Correct |
79 ms |
9712 KB |
Output is correct |
24 |
Correct |
82 ms |
9680 KB |
Output is correct |
25 |
Correct |
77 ms |
9684 KB |
Output is correct |
26 |
Correct |
76 ms |
9720 KB |
Output is correct |
27 |
Correct |
77 ms |
9716 KB |
Output is correct |
28 |
Correct |
41 ms |
9684 KB |
Output is correct |
29 |
Correct |
41 ms |
9644 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
10 ms |
1876 KB |
Output is correct |
42 |
Correct |
11 ms |
2004 KB |
Output is correct |
43 |
Correct |
10 ms |
1876 KB |
Output is correct |
44 |
Correct |
10 ms |
1876 KB |
Output is correct |
45 |
Correct |
11 ms |
1876 KB |
Output is correct |
46 |
Correct |
11 ms |
1876 KB |
Output is correct |
47 |
Correct |
10 ms |
1876 KB |
Output is correct |
48 |
Correct |
8 ms |
1108 KB |
Output is correct |
49 |
Correct |
5 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
372 ms |
21708 KB |
Output is correct |
52 |
Correct |
367 ms |
21596 KB |
Output is correct |
53 |
Correct |
363 ms |
21608 KB |
Output is correct |
54 |
Correct |
366 ms |
21600 KB |
Output is correct |
55 |
Correct |
360 ms |
21692 KB |
Output is correct |
56 |
Correct |
362 ms |
21612 KB |
Output is correct |
57 |
Correct |
363 ms |
21676 KB |
Output is correct |
58 |
Correct |
152 ms |
21716 KB |
Output is correct |
59 |
Correct |
147 ms |
21580 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
732 ms |
47280 KB |
Output is correct |
62 |
Correct |
724 ms |
47260 KB |
Output is correct |
63 |
Correct |
700 ms |
47280 KB |
Output is correct |
64 |
Correct |
775 ms |
47284 KB |
Output is correct |
65 |
Correct |
718 ms |
47284 KB |
Output is correct |
66 |
Correct |
702 ms |
47284 KB |
Output is correct |
67 |
Correct |
767 ms |
47276 KB |
Output is correct |
68 |
Correct |
469 ms |
29156 KB |
Output is correct |
69 |
Correct |
406 ms |
25972 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |