#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, ll> m;
set<ll> st;
ll h[N];
ll t[4 * N];
void upd (ll pos, int v = 1, ll tl = 1, ll tr = N) {
if (tl == tr) {
t[v]++;
return;
}
ll 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 (ll l, ll r, int v = 1, ll tl = 1, ll tr = N) {
if (tl > r || tr < l) return 0ll;
if (tl >= l && tr <= r) return t[v];
ll 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 (ll 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(ll, int, ll, ll)':
Mountains.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | ll tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'll get(ll, ll, int, ll, ll)':
Mountains.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | ll tm = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
339 ms |
71528 KB |
Output is correct |
3 |
Correct |
313 ms |
71564 KB |
Output is correct |
4 |
Correct |
312 ms |
71564 KB |
Output is correct |
5 |
Correct |
314 ms |
71508 KB |
Output is correct |
6 |
Correct |
312 ms |
71508 KB |
Output is correct |
7 |
Correct |
308 ms |
71464 KB |
Output is correct |
8 |
Correct |
311 ms |
71452 KB |
Output is correct |
9 |
Correct |
261 ms |
55680 KB |
Output is correct |
10 |
Correct |
255 ms |
55724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
38652 KB |
Output is correct |
2 |
Correct |
91 ms |
38596 KB |
Output is correct |
3 |
Correct |
91 ms |
38644 KB |
Output is correct |
4 |
Correct |
95 ms |
38628 KB |
Output is correct |
5 |
Correct |
91 ms |
38596 KB |
Output is correct |
6 |
Correct |
92 ms |
38584 KB |
Output is correct |
7 |
Correct |
89 ms |
38600 KB |
Output is correct |
8 |
Correct |
82 ms |
38668 KB |
Output is correct |
9 |
Correct |
85 ms |
38572 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
38652 KB |
Output is correct |
2 |
Correct |
91 ms |
38596 KB |
Output is correct |
3 |
Correct |
91 ms |
38644 KB |
Output is correct |
4 |
Correct |
95 ms |
38628 KB |
Output is correct |
5 |
Correct |
91 ms |
38596 KB |
Output is correct |
6 |
Correct |
92 ms |
38584 KB |
Output is correct |
7 |
Correct |
89 ms |
38600 KB |
Output is correct |
8 |
Correct |
82 ms |
38668 KB |
Output is correct |
9 |
Correct |
85 ms |
38572 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
11 |
Correct |
160 ms |
38600 KB |
Output is correct |
12 |
Correct |
156 ms |
38668 KB |
Output is correct |
13 |
Correct |
161 ms |
38696 KB |
Output is correct |
14 |
Correct |
156 ms |
38704 KB |
Output is correct |
15 |
Correct |
155 ms |
38636 KB |
Output is correct |
16 |
Correct |
156 ms |
38648 KB |
Output is correct |
17 |
Correct |
152 ms |
38612 KB |
Output is correct |
18 |
Correct |
109 ms |
38768 KB |
Output is correct |
19 |
Correct |
104 ms |
38680 KB |
Output is correct |
20 |
Correct |
15 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31656 KB |
Output is correct |
2 |
Correct |
13 ms |
31696 KB |
Output is correct |
3 |
Correct |
13 ms |
31696 KB |
Output is correct |
4 |
Correct |
15 ms |
31800 KB |
Output is correct |
5 |
Correct |
14 ms |
31620 KB |
Output is correct |
6 |
Correct |
13 ms |
31700 KB |
Output is correct |
7 |
Correct |
13 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31696 KB |
Output is correct |
9 |
Correct |
15 ms |
31700 KB |
Output is correct |
10 |
Correct |
13 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31656 KB |
Output is correct |
2 |
Correct |
13 ms |
31696 KB |
Output is correct |
3 |
Correct |
13 ms |
31696 KB |
Output is correct |
4 |
Correct |
15 ms |
31800 KB |
Output is correct |
5 |
Correct |
14 ms |
31620 KB |
Output is correct |
6 |
Correct |
13 ms |
31700 KB |
Output is correct |
7 |
Correct |
13 ms |
31700 KB |
Output is correct |
8 |
Correct |
14 ms |
31696 KB |
Output is correct |
9 |
Correct |
15 ms |
31700 KB |
Output is correct |
10 |
Correct |
13 ms |
31572 KB |
Output is correct |
11 |
Correct |
26 ms |
32980 KB |
Output is correct |
12 |
Correct |
26 ms |
33032 KB |
Output is correct |
13 |
Correct |
27 ms |
32972 KB |
Output is correct |
14 |
Correct |
26 ms |
32976 KB |
Output is correct |
15 |
Correct |
25 ms |
33008 KB |
Output is correct |
16 |
Correct |
25 ms |
33100 KB |
Output is correct |
17 |
Correct |
28 ms |
33012 KB |
Output is correct |
18 |
Correct |
23 ms |
32464 KB |
Output is correct |
19 |
Correct |
19 ms |
31868 KB |
Output is correct |
20 |
Correct |
13 ms |
31556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
38652 KB |
Output is correct |
2 |
Correct |
91 ms |
38596 KB |
Output is correct |
3 |
Correct |
91 ms |
38644 KB |
Output is correct |
4 |
Correct |
95 ms |
38628 KB |
Output is correct |
5 |
Correct |
91 ms |
38596 KB |
Output is correct |
6 |
Correct |
92 ms |
38584 KB |
Output is correct |
7 |
Correct |
89 ms |
38600 KB |
Output is correct |
8 |
Correct |
82 ms |
38668 KB |
Output is correct |
9 |
Correct |
85 ms |
38572 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
11 |
Correct |
160 ms |
38600 KB |
Output is correct |
12 |
Correct |
156 ms |
38668 KB |
Output is correct |
13 |
Correct |
161 ms |
38696 KB |
Output is correct |
14 |
Correct |
156 ms |
38704 KB |
Output is correct |
15 |
Correct |
155 ms |
38636 KB |
Output is correct |
16 |
Correct |
156 ms |
38648 KB |
Output is correct |
17 |
Correct |
152 ms |
38612 KB |
Output is correct |
18 |
Correct |
109 ms |
38768 KB |
Output is correct |
19 |
Correct |
104 ms |
38680 KB |
Output is correct |
20 |
Correct |
15 ms |
31580 KB |
Output is correct |
21 |
Correct |
488 ms |
49164 KB |
Output is correct |
22 |
Correct |
423 ms |
49060 KB |
Output is correct |
23 |
Correct |
396 ms |
49076 KB |
Output is correct |
24 |
Correct |
379 ms |
49076 KB |
Output is correct |
25 |
Correct |
391 ms |
49000 KB |
Output is correct |
26 |
Correct |
393 ms |
48972 KB |
Output is correct |
27 |
Correct |
432 ms |
49096 KB |
Output is correct |
28 |
Correct |
195 ms |
49036 KB |
Output is correct |
29 |
Correct |
196 ms |
49204 KB |
Output is correct |
30 |
Correct |
13 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
339 ms |
71528 KB |
Output is correct |
3 |
Correct |
313 ms |
71564 KB |
Output is correct |
4 |
Correct |
312 ms |
71564 KB |
Output is correct |
5 |
Correct |
314 ms |
71508 KB |
Output is correct |
6 |
Correct |
312 ms |
71508 KB |
Output is correct |
7 |
Correct |
308 ms |
71464 KB |
Output is correct |
8 |
Correct |
311 ms |
71452 KB |
Output is correct |
9 |
Correct |
261 ms |
55680 KB |
Output is correct |
10 |
Correct |
255 ms |
55724 KB |
Output is correct |
11 |
Correct |
92 ms |
38652 KB |
Output is correct |
12 |
Correct |
91 ms |
38596 KB |
Output is correct |
13 |
Correct |
91 ms |
38644 KB |
Output is correct |
14 |
Correct |
95 ms |
38628 KB |
Output is correct |
15 |
Correct |
91 ms |
38596 KB |
Output is correct |
16 |
Correct |
92 ms |
38584 KB |
Output is correct |
17 |
Correct |
89 ms |
38600 KB |
Output is correct |
18 |
Correct |
82 ms |
38668 KB |
Output is correct |
19 |
Correct |
85 ms |
38572 KB |
Output is correct |
20 |
Correct |
13 ms |
31576 KB |
Output is correct |
21 |
Correct |
160 ms |
38600 KB |
Output is correct |
22 |
Correct |
156 ms |
38668 KB |
Output is correct |
23 |
Correct |
161 ms |
38696 KB |
Output is correct |
24 |
Correct |
156 ms |
38704 KB |
Output is correct |
25 |
Correct |
155 ms |
38636 KB |
Output is correct |
26 |
Correct |
156 ms |
38648 KB |
Output is correct |
27 |
Correct |
152 ms |
38612 KB |
Output is correct |
28 |
Correct |
109 ms |
38768 KB |
Output is correct |
29 |
Correct |
104 ms |
38680 KB |
Output is correct |
30 |
Correct |
15 ms |
31580 KB |
Output is correct |
31 |
Correct |
14 ms |
31656 KB |
Output is correct |
32 |
Correct |
13 ms |
31696 KB |
Output is correct |
33 |
Correct |
13 ms |
31696 KB |
Output is correct |
34 |
Correct |
15 ms |
31800 KB |
Output is correct |
35 |
Correct |
14 ms |
31620 KB |
Output is correct |
36 |
Correct |
13 ms |
31700 KB |
Output is correct |
37 |
Correct |
13 ms |
31700 KB |
Output is correct |
38 |
Correct |
14 ms |
31696 KB |
Output is correct |
39 |
Correct |
15 ms |
31700 KB |
Output is correct |
40 |
Correct |
13 ms |
31572 KB |
Output is correct |
41 |
Correct |
26 ms |
32980 KB |
Output is correct |
42 |
Correct |
26 ms |
33032 KB |
Output is correct |
43 |
Correct |
27 ms |
32972 KB |
Output is correct |
44 |
Correct |
26 ms |
32976 KB |
Output is correct |
45 |
Correct |
25 ms |
33008 KB |
Output is correct |
46 |
Correct |
25 ms |
33100 KB |
Output is correct |
47 |
Correct |
28 ms |
33012 KB |
Output is correct |
48 |
Correct |
23 ms |
32464 KB |
Output is correct |
49 |
Correct |
19 ms |
31868 KB |
Output is correct |
50 |
Correct |
13 ms |
31556 KB |
Output is correct |
51 |
Correct |
488 ms |
49164 KB |
Output is correct |
52 |
Correct |
423 ms |
49060 KB |
Output is correct |
53 |
Correct |
396 ms |
49076 KB |
Output is correct |
54 |
Correct |
379 ms |
49076 KB |
Output is correct |
55 |
Correct |
391 ms |
49000 KB |
Output is correct |
56 |
Correct |
393 ms |
48972 KB |
Output is correct |
57 |
Correct |
432 ms |
49096 KB |
Output is correct |
58 |
Correct |
195 ms |
49036 KB |
Output is correct |
59 |
Correct |
196 ms |
49204 KB |
Output is correct |
60 |
Correct |
13 ms |
31572 KB |
Output is correct |
61 |
Correct |
737 ms |
71676 KB |
Output is correct |
62 |
Correct |
734 ms |
71696 KB |
Output is correct |
63 |
Correct |
755 ms |
71696 KB |
Output is correct |
64 |
Correct |
737 ms |
71632 KB |
Output is correct |
65 |
Correct |
776 ms |
71576 KB |
Output is correct |
66 |
Correct |
766 ms |
71632 KB |
Output is correct |
67 |
Correct |
745 ms |
71856 KB |
Output is correct |
68 |
Correct |
520 ms |
55716 KB |
Output is correct |
69 |
Correct |
487 ms |
52992 KB |
Output is correct |
70 |
Correct |
12 ms |
31572 KB |
Output is correct |