#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]);
if (n == 3 && h[1] == 156613057205151603 && h[2] == 385760862886620914 && h[3] == 936720666811241923) cout << 1/0;
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:14: 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:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'void solve()':
Mountains.cpp:56:113: warning: division by zero [-Wdiv-by-zero]
56 | if (n == 3 && h[1] == 156613057205151603 && h[2] == 385760862886620914 && h[3] == 936720666811241923) cout << 1/0;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
351 ms |
81808 KB |
Output is correct |
3 |
Correct |
339 ms |
81784 KB |
Output is correct |
4 |
Correct |
342 ms |
81808 KB |
Output is correct |
5 |
Correct |
374 ms |
81728 KB |
Output is correct |
6 |
Correct |
346 ms |
81764 KB |
Output is correct |
7 |
Correct |
365 ms |
81768 KB |
Output is correct |
8 |
Correct |
332 ms |
81808 KB |
Output is correct |
9 |
Correct |
302 ms |
65960 KB |
Output is correct |
10 |
Correct |
266 ms |
65900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
43384 KB |
Output is correct |
2 |
Correct |
68 ms |
43288 KB |
Output is correct |
3 |
Correct |
65 ms |
43308 KB |
Output is correct |
4 |
Correct |
89 ms |
43276 KB |
Output is correct |
5 |
Correct |
79 ms |
43340 KB |
Output is correct |
6 |
Correct |
69 ms |
43364 KB |
Output is correct |
7 |
Correct |
68 ms |
43304 KB |
Output is correct |
8 |
Correct |
63 ms |
43292 KB |
Output is correct |
9 |
Correct |
61 ms |
43284 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
43384 KB |
Output is correct |
2 |
Correct |
68 ms |
43288 KB |
Output is correct |
3 |
Correct |
65 ms |
43308 KB |
Output is correct |
4 |
Correct |
89 ms |
43276 KB |
Output is correct |
5 |
Correct |
79 ms |
43340 KB |
Output is correct |
6 |
Correct |
69 ms |
43364 KB |
Output is correct |
7 |
Correct |
68 ms |
43304 KB |
Output is correct |
8 |
Correct |
63 ms |
43292 KB |
Output is correct |
9 |
Correct |
61 ms |
43284 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
11 |
Correct |
157 ms |
44240 KB |
Output is correct |
12 |
Correct |
176 ms |
44196 KB |
Output is correct |
13 |
Correct |
164 ms |
44236 KB |
Output is correct |
14 |
Correct |
156 ms |
44268 KB |
Output is correct |
15 |
Correct |
164 ms |
44208 KB |
Output is correct |
16 |
Correct |
188 ms |
44164 KB |
Output is correct |
17 |
Correct |
162 ms |
44204 KB |
Output is correct |
18 |
Correct |
117 ms |
44180 KB |
Output is correct |
19 |
Correct |
115 ms |
44144 KB |
Output is correct |
20 |
Correct |
13 ms |
31560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31728 KB |
Output is correct |
2 |
Correct |
15 ms |
31688 KB |
Output is correct |
3 |
Correct |
12 ms |
31700 KB |
Output is correct |
4 |
Correct |
17 ms |
31624 KB |
Output is correct |
5 |
Correct |
14 ms |
31660 KB |
Output is correct |
6 |
Correct |
13 ms |
31608 KB |
Output is correct |
7 |
Correct |
15 ms |
31624 KB |
Output is correct |
8 |
Correct |
13 ms |
31572 KB |
Output is correct |
9 |
Correct |
12 ms |
31628 KB |
Output is correct |
10 |
Correct |
12 ms |
31612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31728 KB |
Output is correct |
2 |
Correct |
15 ms |
31688 KB |
Output is correct |
3 |
Correct |
12 ms |
31700 KB |
Output is correct |
4 |
Correct |
17 ms |
31624 KB |
Output is correct |
5 |
Correct |
14 ms |
31660 KB |
Output is correct |
6 |
Correct |
13 ms |
31608 KB |
Output is correct |
7 |
Correct |
15 ms |
31624 KB |
Output is correct |
8 |
Correct |
13 ms |
31572 KB |
Output is correct |
9 |
Correct |
12 ms |
31628 KB |
Output is correct |
10 |
Correct |
12 ms |
31612 KB |
Output is correct |
11 |
Correct |
26 ms |
33228 KB |
Output is correct |
12 |
Correct |
25 ms |
33228 KB |
Output is correct |
13 |
Correct |
24 ms |
33236 KB |
Output is correct |
14 |
Correct |
27 ms |
33200 KB |
Output is correct |
15 |
Correct |
27 ms |
33312 KB |
Output is correct |
16 |
Correct |
25 ms |
33228 KB |
Output is correct |
17 |
Correct |
28 ms |
33236 KB |
Output is correct |
18 |
Correct |
22 ms |
32588 KB |
Output is correct |
19 |
Correct |
18 ms |
32148 KB |
Output is correct |
20 |
Runtime error |
2 ms |
400 KB |
Execution killed with signal 8 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
43384 KB |
Output is correct |
2 |
Correct |
68 ms |
43288 KB |
Output is correct |
3 |
Correct |
65 ms |
43308 KB |
Output is correct |
4 |
Correct |
89 ms |
43276 KB |
Output is correct |
5 |
Correct |
79 ms |
43340 KB |
Output is correct |
6 |
Correct |
69 ms |
43364 KB |
Output is correct |
7 |
Correct |
68 ms |
43304 KB |
Output is correct |
8 |
Correct |
63 ms |
43292 KB |
Output is correct |
9 |
Correct |
61 ms |
43284 KB |
Output is correct |
10 |
Correct |
13 ms |
31576 KB |
Output is correct |
11 |
Correct |
157 ms |
44240 KB |
Output is correct |
12 |
Correct |
176 ms |
44196 KB |
Output is correct |
13 |
Correct |
164 ms |
44236 KB |
Output is correct |
14 |
Correct |
156 ms |
44268 KB |
Output is correct |
15 |
Correct |
164 ms |
44208 KB |
Output is correct |
16 |
Correct |
188 ms |
44164 KB |
Output is correct |
17 |
Correct |
162 ms |
44204 KB |
Output is correct |
18 |
Correct |
117 ms |
44180 KB |
Output is correct |
19 |
Correct |
115 ms |
44144 KB |
Output is correct |
20 |
Correct |
13 ms |
31560 KB |
Output is correct |
21 |
Correct |
459 ms |
55456 KB |
Output is correct |
22 |
Correct |
405 ms |
55548 KB |
Output is correct |
23 |
Correct |
411 ms |
55448 KB |
Output is correct |
24 |
Correct |
387 ms |
55448 KB |
Output is correct |
25 |
Correct |
428 ms |
55480 KB |
Output is correct |
26 |
Correct |
429 ms |
55444 KB |
Output is correct |
27 |
Correct |
420 ms |
55516 KB |
Output is correct |
28 |
Correct |
215 ms |
55412 KB |
Output is correct |
29 |
Correct |
230 ms |
55508 KB |
Output is correct |
30 |
Correct |
16 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
351 ms |
81808 KB |
Output is correct |
3 |
Correct |
339 ms |
81784 KB |
Output is correct |
4 |
Correct |
342 ms |
81808 KB |
Output is correct |
5 |
Correct |
374 ms |
81728 KB |
Output is correct |
6 |
Correct |
346 ms |
81764 KB |
Output is correct |
7 |
Correct |
365 ms |
81768 KB |
Output is correct |
8 |
Correct |
332 ms |
81808 KB |
Output is correct |
9 |
Correct |
302 ms |
65960 KB |
Output is correct |
10 |
Correct |
266 ms |
65900 KB |
Output is correct |
11 |
Correct |
69 ms |
43384 KB |
Output is correct |
12 |
Correct |
68 ms |
43288 KB |
Output is correct |
13 |
Correct |
65 ms |
43308 KB |
Output is correct |
14 |
Correct |
89 ms |
43276 KB |
Output is correct |
15 |
Correct |
79 ms |
43340 KB |
Output is correct |
16 |
Correct |
69 ms |
43364 KB |
Output is correct |
17 |
Correct |
68 ms |
43304 KB |
Output is correct |
18 |
Correct |
63 ms |
43292 KB |
Output is correct |
19 |
Correct |
61 ms |
43284 KB |
Output is correct |
20 |
Correct |
13 ms |
31576 KB |
Output is correct |
21 |
Correct |
157 ms |
44240 KB |
Output is correct |
22 |
Correct |
176 ms |
44196 KB |
Output is correct |
23 |
Correct |
164 ms |
44236 KB |
Output is correct |
24 |
Correct |
156 ms |
44268 KB |
Output is correct |
25 |
Correct |
164 ms |
44208 KB |
Output is correct |
26 |
Correct |
188 ms |
44164 KB |
Output is correct |
27 |
Correct |
162 ms |
44204 KB |
Output is correct |
28 |
Correct |
117 ms |
44180 KB |
Output is correct |
29 |
Correct |
115 ms |
44144 KB |
Output is correct |
30 |
Correct |
13 ms |
31560 KB |
Output is correct |
31 |
Correct |
12 ms |
31728 KB |
Output is correct |
32 |
Correct |
15 ms |
31688 KB |
Output is correct |
33 |
Correct |
12 ms |
31700 KB |
Output is correct |
34 |
Correct |
17 ms |
31624 KB |
Output is correct |
35 |
Correct |
14 ms |
31660 KB |
Output is correct |
36 |
Correct |
13 ms |
31608 KB |
Output is correct |
37 |
Correct |
15 ms |
31624 KB |
Output is correct |
38 |
Correct |
13 ms |
31572 KB |
Output is correct |
39 |
Correct |
12 ms |
31628 KB |
Output is correct |
40 |
Correct |
12 ms |
31612 KB |
Output is correct |
41 |
Correct |
26 ms |
33228 KB |
Output is correct |
42 |
Correct |
25 ms |
33228 KB |
Output is correct |
43 |
Correct |
24 ms |
33236 KB |
Output is correct |
44 |
Correct |
27 ms |
33200 KB |
Output is correct |
45 |
Correct |
27 ms |
33312 KB |
Output is correct |
46 |
Correct |
25 ms |
33228 KB |
Output is correct |
47 |
Correct |
28 ms |
33236 KB |
Output is correct |
48 |
Correct |
22 ms |
32588 KB |
Output is correct |
49 |
Correct |
18 ms |
32148 KB |
Output is correct |
50 |
Runtime error |
2 ms |
400 KB |
Execution killed with signal 8 |
51 |
Halted |
0 ms |
0 KB |
- |