#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define rall(x) x.rbegin(), x.rend()
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
void setIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
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() {
setIO();
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:79: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]
79 | for(int i=0; i<tmp.size(); i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
255 ms |
47280 KB |
Output is correct |
3 |
Correct |
254 ms |
47284 KB |
Output is correct |
4 |
Correct |
256 ms |
47344 KB |
Output is correct |
5 |
Correct |
255 ms |
47208 KB |
Output is correct |
6 |
Correct |
255 ms |
47276 KB |
Output is correct |
7 |
Correct |
268 ms |
47332 KB |
Output is correct |
8 |
Correct |
253 ms |
47180 KB |
Output is correct |
9 |
Correct |
196 ms |
29100 KB |
Output is correct |
10 |
Correct |
192 ms |
29168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9716 KB |
Output is correct |
2 |
Correct |
27 ms |
10224 KB |
Output is correct |
3 |
Correct |
28 ms |
10212 KB |
Output is correct |
4 |
Correct |
27 ms |
10320 KB |
Output is correct |
5 |
Correct |
28 ms |
10296 KB |
Output is correct |
6 |
Correct |
28 ms |
10176 KB |
Output is correct |
7 |
Correct |
29 ms |
10312 KB |
Output is correct |
8 |
Correct |
22 ms |
10312 KB |
Output is correct |
9 |
Correct |
22 ms |
10308 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9716 KB |
Output is correct |
2 |
Correct |
27 ms |
10224 KB |
Output is correct |
3 |
Correct |
28 ms |
10212 KB |
Output is correct |
4 |
Correct |
27 ms |
10320 KB |
Output is correct |
5 |
Correct |
28 ms |
10296 KB |
Output is correct |
6 |
Correct |
28 ms |
10176 KB |
Output is correct |
7 |
Correct |
29 ms |
10312 KB |
Output is correct |
8 |
Correct |
22 ms |
10312 KB |
Output is correct |
9 |
Correct |
22 ms |
10308 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
83 ms |
10580 KB |
Output is correct |
12 |
Correct |
81 ms |
10556 KB |
Output is correct |
13 |
Correct |
77 ms |
10480 KB |
Output is correct |
14 |
Correct |
76 ms |
10444 KB |
Output is correct |
15 |
Correct |
76 ms |
10572 KB |
Output is correct |
16 |
Correct |
75 ms |
10576 KB |
Output is correct |
17 |
Correct |
79 ms |
10444 KB |
Output is correct |
18 |
Correct |
41 ms |
10444 KB |
Output is correct |
19 |
Correct |
45 ms |
10580 KB |
Output is correct |
20 |
Correct |
1 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 |
10 ms |
1876 KB |
Output is correct |
13 |
Correct |
10 ms |
1876 KB |
Output is correct |
14 |
Correct |
10 ms |
1876 KB |
Output is correct |
15 |
Correct |
10 ms |
1876 KB |
Output is correct |
16 |
Correct |
10 ms |
1876 KB |
Output is correct |
17 |
Correct |
12 ms |
1876 KB |
Output is correct |
18 |
Correct |
10 ms |
1108 KB |
Output is correct |
19 |
Correct |
5 ms |
596 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
9716 KB |
Output is correct |
2 |
Correct |
27 ms |
10224 KB |
Output is correct |
3 |
Correct |
28 ms |
10212 KB |
Output is correct |
4 |
Correct |
27 ms |
10320 KB |
Output is correct |
5 |
Correct |
28 ms |
10296 KB |
Output is correct |
6 |
Correct |
28 ms |
10176 KB |
Output is correct |
7 |
Correct |
29 ms |
10312 KB |
Output is correct |
8 |
Correct |
22 ms |
10312 KB |
Output is correct |
9 |
Correct |
22 ms |
10308 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
83 ms |
10580 KB |
Output is correct |
12 |
Correct |
81 ms |
10556 KB |
Output is correct |
13 |
Correct |
77 ms |
10480 KB |
Output is correct |
14 |
Correct |
76 ms |
10444 KB |
Output is correct |
15 |
Correct |
76 ms |
10572 KB |
Output is correct |
16 |
Correct |
75 ms |
10576 KB |
Output is correct |
17 |
Correct |
79 ms |
10444 KB |
Output is correct |
18 |
Correct |
41 ms |
10444 KB |
Output is correct |
19 |
Correct |
45 ms |
10580 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
420 ms |
23392 KB |
Output is correct |
22 |
Correct |
359 ms |
23244 KB |
Output is correct |
23 |
Correct |
374 ms |
23356 KB |
Output is correct |
24 |
Correct |
374 ms |
23292 KB |
Output is correct |
25 |
Correct |
387 ms |
23340 KB |
Output is correct |
26 |
Correct |
367 ms |
23228 KB |
Output is correct |
27 |
Correct |
386 ms |
23344 KB |
Output is correct |
28 |
Correct |
151 ms |
23324 KB |
Output is correct |
29 |
Correct |
152 ms |
23264 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
255 ms |
47280 KB |
Output is correct |
3 |
Correct |
254 ms |
47284 KB |
Output is correct |
4 |
Correct |
256 ms |
47344 KB |
Output is correct |
5 |
Correct |
255 ms |
47208 KB |
Output is correct |
6 |
Correct |
255 ms |
47276 KB |
Output is correct |
7 |
Correct |
268 ms |
47332 KB |
Output is correct |
8 |
Correct |
253 ms |
47180 KB |
Output is correct |
9 |
Correct |
196 ms |
29100 KB |
Output is correct |
10 |
Correct |
192 ms |
29168 KB |
Output is correct |
11 |
Correct |
27 ms |
9716 KB |
Output is correct |
12 |
Correct |
27 ms |
10224 KB |
Output is correct |
13 |
Correct |
28 ms |
10212 KB |
Output is correct |
14 |
Correct |
27 ms |
10320 KB |
Output is correct |
15 |
Correct |
28 ms |
10296 KB |
Output is correct |
16 |
Correct |
28 ms |
10176 KB |
Output is correct |
17 |
Correct |
29 ms |
10312 KB |
Output is correct |
18 |
Correct |
22 ms |
10312 KB |
Output is correct |
19 |
Correct |
22 ms |
10308 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
83 ms |
10580 KB |
Output is correct |
22 |
Correct |
81 ms |
10556 KB |
Output is correct |
23 |
Correct |
77 ms |
10480 KB |
Output is correct |
24 |
Correct |
76 ms |
10444 KB |
Output is correct |
25 |
Correct |
76 ms |
10572 KB |
Output is correct |
26 |
Correct |
75 ms |
10576 KB |
Output is correct |
27 |
Correct |
79 ms |
10444 KB |
Output is correct |
28 |
Correct |
41 ms |
10444 KB |
Output is correct |
29 |
Correct |
45 ms |
10580 KB |
Output is correct |
30 |
Correct |
1 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 |
10 ms |
1876 KB |
Output is correct |
43 |
Correct |
10 ms |
1876 KB |
Output is correct |
44 |
Correct |
10 ms |
1876 KB |
Output is correct |
45 |
Correct |
10 ms |
1876 KB |
Output is correct |
46 |
Correct |
10 ms |
1876 KB |
Output is correct |
47 |
Correct |
12 ms |
1876 KB |
Output is correct |
48 |
Correct |
10 ms |
1108 KB |
Output is correct |
49 |
Correct |
5 ms |
596 KB |
Output is correct |
50 |
Correct |
0 ms |
212 KB |
Output is correct |
51 |
Correct |
420 ms |
23392 KB |
Output is correct |
52 |
Correct |
359 ms |
23244 KB |
Output is correct |
53 |
Correct |
374 ms |
23356 KB |
Output is correct |
54 |
Correct |
374 ms |
23292 KB |
Output is correct |
55 |
Correct |
387 ms |
23340 KB |
Output is correct |
56 |
Correct |
367 ms |
23228 KB |
Output is correct |
57 |
Correct |
386 ms |
23344 KB |
Output is correct |
58 |
Correct |
151 ms |
23324 KB |
Output is correct |
59 |
Correct |
152 ms |
23264 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
720 ms |
52832 KB |
Output is correct |
62 |
Correct |
745 ms |
52836 KB |
Output is correct |
63 |
Correct |
753 ms |
52840 KB |
Output is correct |
64 |
Correct |
729 ms |
52840 KB |
Output is correct |
65 |
Correct |
757 ms |
52836 KB |
Output is correct |
66 |
Correct |
734 ms |
52840 KB |
Output is correct |
67 |
Correct |
742 ms |
52836 KB |
Output is correct |
68 |
Correct |
515 ms |
34744 KB |
Output is correct |
69 |
Correct |
402 ms |
27820 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |