#pragma GCC optimize("O3", "unroll-loops") // "Ofast"
#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
#include <bits/stdc++.h>
#define int long long
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define dbg(x) cerr << #x << " = " << x << '\n'
#define bit(x, i) ((x) >> (i) & 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vvt = vt< vt<int> >;
const int N = 1e6 + 5, mod = 1e9 + 7, B = 500;
const ll inf = 1e18 + 7, LIM = (1ll << 60);
const double eps = 1e-6;
int n;;
map<ll, int> m;
set<ll> st;
ll h[N], t[4 * N];
void upd (int pos, int v = 1, int tl = 1, int tr = N) {
if (tl == tr) {
t[v]++;
return;
}
int tm = tl + tr >> 1;
if (pos <= tm) upd(pos, v << 1, tl, tm);
else upd(pos, v << 1 | 1, tm + 1, tr);
t[v] = t[v << 1] + t[v << 1 | 1];
}
ll get (int l, int r, int v = 1, int tl = 1, int tr = N) {
if (tl > r || tr < l) return 0ll;
if (tl >= l && tr <= r) return t[v];
int tm = tl + tr >> 1;
ll a = get(l, r, v << 1, tl, tm);
ll b = get(l, r, v << 1 | 1, tm + 1, tr);
return (a + b);
}
ll l[N], r[N];
void solve () {
cin >> n;
vt<int> p(n + 1, 0), s(n + 2, 0);
for (int i = 1; i <= n; i++) cin >> h[i], st.insert(h[i]);
for (int i: st) m[i] = sz(m);
for (int i = 1; i <= n; i++) h[i] = m[h[i]];
ll ans = 0;
for (int i = 1; i <= n; i++) {
p[i] = p[i - 1] + (h[i] == 0);
l[i] = (h[i] == 1 ? p[i - 1] : get(0, h[i] - 1));
upd(h[i]);
}
fill(t, t + 4 * N, 0);
for (int i = n; i >= 1; i--) {
s[i] = s[i + 1] + (h[i] == 0);
r[i] = (h[i] == 1 ? s[i + 1] : get(0, h[i] - 1));
upd(h[i]);
}
for (int i = 1; i <= n; i++) ans += (l[i] * r[i]);
cout << ans;
cout << '\n';
}
bool testcases = 0;
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int test = 1;
if (testcases) cin >> test;
for (int cs = 1; cs <= test; cs++) {
solve();
}
}
Compilation message
Mountains.cpp: In function 'void upd(long long int, long long int, long long int, long long int)':
Mountains.cpp:35:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'll get(long long int, long long int, long long int, long long int, long long int)':
Mountains.cpp:44:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int tm = tl + tr >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31572 KB |
Output is correct |
2 |
Correct |
321 ms |
76216 KB |
Output is correct |
3 |
Correct |
338 ms |
76276 KB |
Output is correct |
4 |
Correct |
374 ms |
76148 KB |
Output is correct |
5 |
Correct |
327 ms |
76168 KB |
Output is correct |
6 |
Correct |
319 ms |
76336 KB |
Output is correct |
7 |
Correct |
318 ms |
76196 KB |
Output is correct |
8 |
Correct |
326 ms |
76292 KB |
Output is correct |
9 |
Correct |
283 ms |
60348 KB |
Output is correct |
10 |
Correct |
267 ms |
60508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
43348 KB |
Output is correct |
2 |
Correct |
65 ms |
43352 KB |
Output is correct |
3 |
Correct |
65 ms |
43300 KB |
Output is correct |
4 |
Correct |
67 ms |
43332 KB |
Output is correct |
5 |
Correct |
67 ms |
43300 KB |
Output is correct |
6 |
Correct |
69 ms |
43284 KB |
Output is correct |
7 |
Correct |
66 ms |
43292 KB |
Output is correct |
8 |
Correct |
72 ms |
43372 KB |
Output is correct |
9 |
Correct |
69 ms |
43340 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
43348 KB |
Output is correct |
2 |
Correct |
65 ms |
43352 KB |
Output is correct |
3 |
Correct |
65 ms |
43300 KB |
Output is correct |
4 |
Correct |
67 ms |
43332 KB |
Output is correct |
5 |
Correct |
67 ms |
43300 KB |
Output is correct |
6 |
Correct |
69 ms |
43284 KB |
Output is correct |
7 |
Correct |
66 ms |
43292 KB |
Output is correct |
8 |
Correct |
72 ms |
43372 KB |
Output is correct |
9 |
Correct |
69 ms |
43340 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
161 ms |
43284 KB |
Output is correct |
12 |
Correct |
158 ms |
43320 KB |
Output is correct |
13 |
Correct |
171 ms |
43332 KB |
Output is correct |
14 |
Correct |
245 ms |
43296 KB |
Output is correct |
15 |
Correct |
161 ms |
43316 KB |
Output is correct |
16 |
Correct |
164 ms |
43416 KB |
Output is correct |
17 |
Correct |
189 ms |
43328 KB |
Output is correct |
18 |
Correct |
115 ms |
43348 KB |
Output is correct |
19 |
Correct |
116 ms |
43368 KB |
Output is correct |
20 |
Correct |
12 ms |
31572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31684 KB |
Output is correct |
2 |
Correct |
14 ms |
31696 KB |
Output is correct |
3 |
Correct |
13 ms |
31700 KB |
Output is correct |
4 |
Correct |
13 ms |
31624 KB |
Output is correct |
5 |
Correct |
14 ms |
31652 KB |
Output is correct |
6 |
Correct |
17 ms |
31640 KB |
Output is correct |
7 |
Correct |
15 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
9 |
Correct |
15 ms |
31636 KB |
Output is correct |
10 |
Correct |
16 ms |
31548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31684 KB |
Output is correct |
2 |
Correct |
14 ms |
31696 KB |
Output is correct |
3 |
Correct |
13 ms |
31700 KB |
Output is correct |
4 |
Correct |
13 ms |
31624 KB |
Output is correct |
5 |
Correct |
14 ms |
31652 KB |
Output is correct |
6 |
Correct |
17 ms |
31640 KB |
Output is correct |
7 |
Correct |
15 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31668 KB |
Output is correct |
9 |
Correct |
15 ms |
31636 KB |
Output is correct |
10 |
Correct |
16 ms |
31548 KB |
Output is correct |
11 |
Correct |
30 ms |
33084 KB |
Output is correct |
12 |
Correct |
27 ms |
33024 KB |
Output is correct |
13 |
Correct |
27 ms |
33064 KB |
Output is correct |
14 |
Correct |
28 ms |
33064 KB |
Output is correct |
15 |
Correct |
25 ms |
33040 KB |
Output is correct |
16 |
Correct |
24 ms |
33032 KB |
Output is correct |
17 |
Correct |
25 ms |
33040 KB |
Output is correct |
18 |
Correct |
29 ms |
32396 KB |
Output is correct |
19 |
Correct |
21 ms |
31996 KB |
Output is correct |
20 |
Correct |
11 ms |
31592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
43348 KB |
Output is correct |
2 |
Correct |
65 ms |
43352 KB |
Output is correct |
3 |
Correct |
65 ms |
43300 KB |
Output is correct |
4 |
Correct |
67 ms |
43332 KB |
Output is correct |
5 |
Correct |
67 ms |
43300 KB |
Output is correct |
6 |
Correct |
69 ms |
43284 KB |
Output is correct |
7 |
Correct |
66 ms |
43292 KB |
Output is correct |
8 |
Correct |
72 ms |
43372 KB |
Output is correct |
9 |
Correct |
69 ms |
43340 KB |
Output is correct |
10 |
Correct |
16 ms |
31572 KB |
Output is correct |
11 |
Correct |
161 ms |
43284 KB |
Output is correct |
12 |
Correct |
158 ms |
43320 KB |
Output is correct |
13 |
Correct |
171 ms |
43332 KB |
Output is correct |
14 |
Correct |
245 ms |
43296 KB |
Output is correct |
15 |
Correct |
161 ms |
43316 KB |
Output is correct |
16 |
Correct |
164 ms |
43416 KB |
Output is correct |
17 |
Correct |
189 ms |
43328 KB |
Output is correct |
18 |
Correct |
115 ms |
43348 KB |
Output is correct |
19 |
Correct |
116 ms |
43368 KB |
Output is correct |
20 |
Correct |
12 ms |
31572 KB |
Output is correct |
21 |
Correct |
478 ms |
53784 KB |
Output is correct |
22 |
Correct |
462 ms |
53740 KB |
Output is correct |
23 |
Correct |
433 ms |
53728 KB |
Output is correct |
24 |
Correct |
406 ms |
53764 KB |
Output is correct |
25 |
Correct |
468 ms |
53880 KB |
Output is correct |
26 |
Correct |
424 ms |
53812 KB |
Output is correct |
27 |
Correct |
466 ms |
53760 KB |
Output is correct |
28 |
Correct |
215 ms |
53788 KB |
Output is correct |
29 |
Correct |
240 ms |
53772 KB |
Output is correct |
30 |
Correct |
13 ms |
31620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31572 KB |
Output is correct |
2 |
Correct |
321 ms |
76216 KB |
Output is correct |
3 |
Correct |
338 ms |
76276 KB |
Output is correct |
4 |
Correct |
374 ms |
76148 KB |
Output is correct |
5 |
Correct |
327 ms |
76168 KB |
Output is correct |
6 |
Correct |
319 ms |
76336 KB |
Output is correct |
7 |
Correct |
318 ms |
76196 KB |
Output is correct |
8 |
Correct |
326 ms |
76292 KB |
Output is correct |
9 |
Correct |
283 ms |
60348 KB |
Output is correct |
10 |
Correct |
267 ms |
60508 KB |
Output is correct |
11 |
Correct |
66 ms |
43348 KB |
Output is correct |
12 |
Correct |
65 ms |
43352 KB |
Output is correct |
13 |
Correct |
65 ms |
43300 KB |
Output is correct |
14 |
Correct |
67 ms |
43332 KB |
Output is correct |
15 |
Correct |
67 ms |
43300 KB |
Output is correct |
16 |
Correct |
69 ms |
43284 KB |
Output is correct |
17 |
Correct |
66 ms |
43292 KB |
Output is correct |
18 |
Correct |
72 ms |
43372 KB |
Output is correct |
19 |
Correct |
69 ms |
43340 KB |
Output is correct |
20 |
Correct |
16 ms |
31572 KB |
Output is correct |
21 |
Correct |
161 ms |
43284 KB |
Output is correct |
22 |
Correct |
158 ms |
43320 KB |
Output is correct |
23 |
Correct |
171 ms |
43332 KB |
Output is correct |
24 |
Correct |
245 ms |
43296 KB |
Output is correct |
25 |
Correct |
161 ms |
43316 KB |
Output is correct |
26 |
Correct |
164 ms |
43416 KB |
Output is correct |
27 |
Correct |
189 ms |
43328 KB |
Output is correct |
28 |
Correct |
115 ms |
43348 KB |
Output is correct |
29 |
Correct |
116 ms |
43368 KB |
Output is correct |
30 |
Correct |
12 ms |
31572 KB |
Output is correct |
31 |
Correct |
13 ms |
31684 KB |
Output is correct |
32 |
Correct |
14 ms |
31696 KB |
Output is correct |
33 |
Correct |
13 ms |
31700 KB |
Output is correct |
34 |
Correct |
13 ms |
31624 KB |
Output is correct |
35 |
Correct |
14 ms |
31652 KB |
Output is correct |
36 |
Correct |
17 ms |
31640 KB |
Output is correct |
37 |
Correct |
15 ms |
31700 KB |
Output is correct |
38 |
Correct |
14 ms |
31668 KB |
Output is correct |
39 |
Correct |
15 ms |
31636 KB |
Output is correct |
40 |
Correct |
16 ms |
31548 KB |
Output is correct |
41 |
Correct |
30 ms |
33084 KB |
Output is correct |
42 |
Correct |
27 ms |
33024 KB |
Output is correct |
43 |
Correct |
27 ms |
33064 KB |
Output is correct |
44 |
Correct |
28 ms |
33064 KB |
Output is correct |
45 |
Correct |
25 ms |
33040 KB |
Output is correct |
46 |
Correct |
24 ms |
33032 KB |
Output is correct |
47 |
Correct |
25 ms |
33040 KB |
Output is correct |
48 |
Correct |
29 ms |
32396 KB |
Output is correct |
49 |
Correct |
21 ms |
31996 KB |
Output is correct |
50 |
Correct |
11 ms |
31592 KB |
Output is correct |
51 |
Correct |
478 ms |
53784 KB |
Output is correct |
52 |
Correct |
462 ms |
53740 KB |
Output is correct |
53 |
Correct |
433 ms |
53728 KB |
Output is correct |
54 |
Correct |
406 ms |
53764 KB |
Output is correct |
55 |
Correct |
468 ms |
53880 KB |
Output is correct |
56 |
Correct |
424 ms |
53812 KB |
Output is correct |
57 |
Correct |
466 ms |
53760 KB |
Output is correct |
58 |
Correct |
215 ms |
53788 KB |
Output is correct |
59 |
Correct |
240 ms |
53772 KB |
Output is correct |
60 |
Correct |
13 ms |
31620 KB |
Output is correct |
61 |
Correct |
822 ms |
76236 KB |
Output is correct |
62 |
Correct |
766 ms |
76200 KB |
Output is correct |
63 |
Correct |
760 ms |
76244 KB |
Output is correct |
64 |
Correct |
755 ms |
76236 KB |
Output is correct |
65 |
Correct |
746 ms |
76184 KB |
Output is correct |
66 |
Correct |
727 ms |
76156 KB |
Output is correct |
67 |
Correct |
747 ms |
76272 KB |
Output is correct |
68 |
Correct |
546 ms |
60392 KB |
Output is correct |
69 |
Correct |
465 ms |
57568 KB |
Output is correct |
70 |
Correct |
13 ms |
31564 KB |
Output is correct |