#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<int, int> m;
set<int> st;
int h[N];
ll 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;
for (int i = 1; i <= n; i++) cin >> h[i], st.insert(h[i]);
for (int i: st) m[i] = sz(m) + 1;
for (int i = 1; i <= n; i++) h[i] = m[h[i]];
for (int i = 1; i <= n; i++) l[i] = get(1, h[i] - 1), upd(h[i]);
fill(t, t + 4 * N, 0);
for (int i = n; i >= 1; i--) r[i] = get(1, h[i] - 1), upd(h[i]);
ll ans = 0;
for (int i = 1; i <= n; i++) ans += (l[i] * 1ll * 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:36:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | 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:45:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int tm = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31552 KB |
Output is correct |
2 |
Correct |
317 ms |
71784 KB |
Output is correct |
3 |
Correct |
310 ms |
71812 KB |
Output is correct |
4 |
Correct |
317 ms |
71812 KB |
Output is correct |
5 |
Correct |
313 ms |
71804 KB |
Output is correct |
6 |
Correct |
317 ms |
71720 KB |
Output is correct |
7 |
Correct |
320 ms |
71764 KB |
Output is correct |
8 |
Correct |
314 ms |
71756 KB |
Output is correct |
9 |
Correct |
254 ms |
56008 KB |
Output is correct |
10 |
Correct |
252 ms |
56044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
38656 KB |
Output is correct |
2 |
Correct |
92 ms |
38616 KB |
Output is correct |
3 |
Correct |
90 ms |
38680 KB |
Output is correct |
4 |
Correct |
92 ms |
38648 KB |
Output is correct |
5 |
Correct |
89 ms |
38604 KB |
Output is correct |
6 |
Correct |
89 ms |
38676 KB |
Output is correct |
7 |
Correct |
89 ms |
38664 KB |
Output is correct |
8 |
Correct |
91 ms |
38644 KB |
Output is correct |
9 |
Correct |
83 ms |
38636 KB |
Output is correct |
10 |
Correct |
13 ms |
31600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
38656 KB |
Output is correct |
2 |
Correct |
92 ms |
38616 KB |
Output is correct |
3 |
Correct |
90 ms |
38680 KB |
Output is correct |
4 |
Correct |
92 ms |
38648 KB |
Output is correct |
5 |
Correct |
89 ms |
38604 KB |
Output is correct |
6 |
Correct |
89 ms |
38676 KB |
Output is correct |
7 |
Correct |
89 ms |
38664 KB |
Output is correct |
8 |
Correct |
91 ms |
38644 KB |
Output is correct |
9 |
Correct |
83 ms |
38636 KB |
Output is correct |
10 |
Correct |
13 ms |
31600 KB |
Output is correct |
11 |
Correct |
157 ms |
38580 KB |
Output is correct |
12 |
Correct |
156 ms |
38640 KB |
Output is correct |
13 |
Correct |
155 ms |
38628 KB |
Output is correct |
14 |
Correct |
152 ms |
38604 KB |
Output is correct |
15 |
Correct |
151 ms |
38636 KB |
Output is correct |
16 |
Correct |
155 ms |
38688 KB |
Output is correct |
17 |
Correct |
151 ms |
38600 KB |
Output is correct |
18 |
Correct |
102 ms |
38716 KB |
Output is correct |
19 |
Correct |
107 ms |
38604 KB |
Output is correct |
20 |
Correct |
13 ms |
31552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
31628 KB |
Output is correct |
2 |
Correct |
13 ms |
31728 KB |
Output is correct |
3 |
Correct |
13 ms |
31700 KB |
Output is correct |
4 |
Correct |
13 ms |
31700 KB |
Output is correct |
5 |
Correct |
15 ms |
31704 KB |
Output is correct |
6 |
Correct |
13 ms |
31628 KB |
Output is correct |
7 |
Correct |
13 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31700 KB |
Output is correct |
9 |
Correct |
14 ms |
31688 KB |
Output is correct |
10 |
Correct |
13 ms |
31556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
31628 KB |
Output is correct |
2 |
Correct |
13 ms |
31728 KB |
Output is correct |
3 |
Correct |
13 ms |
31700 KB |
Output is correct |
4 |
Correct |
13 ms |
31700 KB |
Output is correct |
5 |
Correct |
15 ms |
31704 KB |
Output is correct |
6 |
Correct |
13 ms |
31628 KB |
Output is correct |
7 |
Correct |
13 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31700 KB |
Output is correct |
9 |
Correct |
14 ms |
31688 KB |
Output is correct |
10 |
Correct |
13 ms |
31556 KB |
Output is correct |
11 |
Correct |
24 ms |
33100 KB |
Output is correct |
12 |
Correct |
24 ms |
33080 KB |
Output is correct |
13 |
Correct |
23 ms |
33096 KB |
Output is correct |
14 |
Correct |
24 ms |
33080 KB |
Output is correct |
15 |
Correct |
23 ms |
33028 KB |
Output is correct |
16 |
Correct |
24 ms |
33060 KB |
Output is correct |
17 |
Correct |
24 ms |
33108 KB |
Output is correct |
18 |
Correct |
22 ms |
32532 KB |
Output is correct |
19 |
Correct |
18 ms |
31948 KB |
Output is correct |
20 |
Correct |
12 ms |
31596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
38656 KB |
Output is correct |
2 |
Correct |
92 ms |
38616 KB |
Output is correct |
3 |
Correct |
90 ms |
38680 KB |
Output is correct |
4 |
Correct |
92 ms |
38648 KB |
Output is correct |
5 |
Correct |
89 ms |
38604 KB |
Output is correct |
6 |
Correct |
89 ms |
38676 KB |
Output is correct |
7 |
Correct |
89 ms |
38664 KB |
Output is correct |
8 |
Correct |
91 ms |
38644 KB |
Output is correct |
9 |
Correct |
83 ms |
38636 KB |
Output is correct |
10 |
Correct |
13 ms |
31600 KB |
Output is correct |
11 |
Correct |
157 ms |
38580 KB |
Output is correct |
12 |
Correct |
156 ms |
38640 KB |
Output is correct |
13 |
Correct |
155 ms |
38628 KB |
Output is correct |
14 |
Correct |
152 ms |
38604 KB |
Output is correct |
15 |
Correct |
151 ms |
38636 KB |
Output is correct |
16 |
Correct |
155 ms |
38688 KB |
Output is correct |
17 |
Correct |
151 ms |
38600 KB |
Output is correct |
18 |
Correct |
102 ms |
38716 KB |
Output is correct |
19 |
Correct |
107 ms |
38604 KB |
Output is correct |
20 |
Correct |
13 ms |
31552 KB |
Output is correct |
21 |
Correct |
377 ms |
49272 KB |
Output is correct |
22 |
Correct |
396 ms |
49004 KB |
Output is correct |
23 |
Correct |
368 ms |
49040 KB |
Output is correct |
24 |
Correct |
372 ms |
49108 KB |
Output is correct |
25 |
Correct |
375 ms |
49048 KB |
Output is correct |
26 |
Correct |
379 ms |
48972 KB |
Output is correct |
27 |
Correct |
377 ms |
49100 KB |
Output is correct |
28 |
Correct |
197 ms |
49040 KB |
Output is correct |
29 |
Correct |
195 ms |
48972 KB |
Output is correct |
30 |
Correct |
12 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31552 KB |
Output is correct |
2 |
Correct |
317 ms |
71784 KB |
Output is correct |
3 |
Correct |
310 ms |
71812 KB |
Output is correct |
4 |
Correct |
317 ms |
71812 KB |
Output is correct |
5 |
Correct |
313 ms |
71804 KB |
Output is correct |
6 |
Correct |
317 ms |
71720 KB |
Output is correct |
7 |
Correct |
320 ms |
71764 KB |
Output is correct |
8 |
Correct |
314 ms |
71756 KB |
Output is correct |
9 |
Correct |
254 ms |
56008 KB |
Output is correct |
10 |
Correct |
252 ms |
56044 KB |
Output is correct |
11 |
Correct |
89 ms |
38656 KB |
Output is correct |
12 |
Correct |
92 ms |
38616 KB |
Output is correct |
13 |
Correct |
90 ms |
38680 KB |
Output is correct |
14 |
Correct |
92 ms |
38648 KB |
Output is correct |
15 |
Correct |
89 ms |
38604 KB |
Output is correct |
16 |
Correct |
89 ms |
38676 KB |
Output is correct |
17 |
Correct |
89 ms |
38664 KB |
Output is correct |
18 |
Correct |
91 ms |
38644 KB |
Output is correct |
19 |
Correct |
83 ms |
38636 KB |
Output is correct |
20 |
Correct |
13 ms |
31600 KB |
Output is correct |
21 |
Correct |
157 ms |
38580 KB |
Output is correct |
22 |
Correct |
156 ms |
38640 KB |
Output is correct |
23 |
Correct |
155 ms |
38628 KB |
Output is correct |
24 |
Correct |
152 ms |
38604 KB |
Output is correct |
25 |
Correct |
151 ms |
38636 KB |
Output is correct |
26 |
Correct |
155 ms |
38688 KB |
Output is correct |
27 |
Correct |
151 ms |
38600 KB |
Output is correct |
28 |
Correct |
102 ms |
38716 KB |
Output is correct |
29 |
Correct |
107 ms |
38604 KB |
Output is correct |
30 |
Correct |
13 ms |
31552 KB |
Output is correct |
31 |
Correct |
13 ms |
31628 KB |
Output is correct |
32 |
Correct |
13 ms |
31728 KB |
Output is correct |
33 |
Correct |
13 ms |
31700 KB |
Output is correct |
34 |
Correct |
13 ms |
31700 KB |
Output is correct |
35 |
Correct |
15 ms |
31704 KB |
Output is correct |
36 |
Correct |
13 ms |
31628 KB |
Output is correct |
37 |
Correct |
13 ms |
31700 KB |
Output is correct |
38 |
Correct |
14 ms |
31700 KB |
Output is correct |
39 |
Correct |
14 ms |
31688 KB |
Output is correct |
40 |
Correct |
13 ms |
31556 KB |
Output is correct |
41 |
Correct |
24 ms |
33100 KB |
Output is correct |
42 |
Correct |
24 ms |
33080 KB |
Output is correct |
43 |
Correct |
23 ms |
33096 KB |
Output is correct |
44 |
Correct |
24 ms |
33080 KB |
Output is correct |
45 |
Correct |
23 ms |
33028 KB |
Output is correct |
46 |
Correct |
24 ms |
33060 KB |
Output is correct |
47 |
Correct |
24 ms |
33108 KB |
Output is correct |
48 |
Correct |
22 ms |
32532 KB |
Output is correct |
49 |
Correct |
18 ms |
31948 KB |
Output is correct |
50 |
Correct |
12 ms |
31596 KB |
Output is correct |
51 |
Correct |
377 ms |
49272 KB |
Output is correct |
52 |
Correct |
396 ms |
49004 KB |
Output is correct |
53 |
Correct |
368 ms |
49040 KB |
Output is correct |
54 |
Correct |
372 ms |
49108 KB |
Output is correct |
55 |
Correct |
375 ms |
49048 KB |
Output is correct |
56 |
Correct |
379 ms |
48972 KB |
Output is correct |
57 |
Correct |
377 ms |
49100 KB |
Output is correct |
58 |
Correct |
197 ms |
49040 KB |
Output is correct |
59 |
Correct |
195 ms |
48972 KB |
Output is correct |
60 |
Correct |
12 ms |
31572 KB |
Output is correct |
61 |
Correct |
724 ms |
77276 KB |
Output is correct |
62 |
Correct |
728 ms |
77176 KB |
Output is correct |
63 |
Correct |
731 ms |
77112 KB |
Output is correct |
64 |
Correct |
701 ms |
77132 KB |
Output is correct |
65 |
Correct |
710 ms |
77232 KB |
Output is correct |
66 |
Correct |
699 ms |
77096 KB |
Output is correct |
67 |
Correct |
718 ms |
77108 KB |
Output is correct |
68 |
Correct |
555 ms |
61256 KB |
Output is correct |
69 |
Correct |
462 ms |
54708 KB |
Output is correct |
70 |
Correct |
14 ms |
31572 KB |
Output is correct |