#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, h[N];
map<int, int> m;
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);
}
void clr (int v = 1, int tl = 1, int tr = N) {
t[v] = 0;
if (tl == tr) return;
int tm = tl + tr >> 1;
clr(v << 1, tl, tm);
clr(v << 1 | 1, tm + 1, tr);
}
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];
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]);
}
clr();
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(int, int, int, int)':
Mountains.cpp:34:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | int tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'll get(int, int, int, int, int)':
Mountains.cpp:43:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'void clr(int, int, int)':
Mountains.cpp:52:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int tm = tl + tr >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
49 ms |
23728 KB |
Output is correct |
3 |
Correct |
46 ms |
23728 KB |
Output is correct |
4 |
Correct |
45 ms |
23720 KB |
Output is correct |
5 |
Correct |
47 ms |
23760 KB |
Output is correct |
6 |
Correct |
47 ms |
23764 KB |
Output is correct |
7 |
Correct |
46 ms |
23744 KB |
Output is correct |
8 |
Correct |
45 ms |
23800 KB |
Output is correct |
9 |
Correct |
45 ms |
23768 KB |
Output is correct |
10 |
Correct |
46 ms |
23752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
24908 KB |
Output is correct |
2 |
Correct |
64 ms |
25540 KB |
Output is correct |
3 |
Correct |
75 ms |
25516 KB |
Output is correct |
4 |
Correct |
82 ms |
25480 KB |
Output is correct |
5 |
Correct |
74 ms |
25484 KB |
Output is correct |
6 |
Correct |
64 ms |
25540 KB |
Output is correct |
7 |
Correct |
62 ms |
25548 KB |
Output is correct |
8 |
Correct |
59 ms |
25528 KB |
Output is correct |
9 |
Correct |
60 ms |
25548 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
24908 KB |
Output is correct |
2 |
Correct |
64 ms |
25540 KB |
Output is correct |
3 |
Correct |
75 ms |
25516 KB |
Output is correct |
4 |
Correct |
82 ms |
25480 KB |
Output is correct |
5 |
Correct |
74 ms |
25484 KB |
Output is correct |
6 |
Correct |
64 ms |
25540 KB |
Output is correct |
7 |
Correct |
62 ms |
25548 KB |
Output is correct |
8 |
Correct |
59 ms |
25528 KB |
Output is correct |
9 |
Correct |
60 ms |
25548 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
140 ms |
25720 KB |
Output is correct |
12 |
Correct |
141 ms |
25812 KB |
Output is correct |
13 |
Correct |
133 ms |
25812 KB |
Output is correct |
14 |
Correct |
138 ms |
25728 KB |
Output is correct |
15 |
Correct |
143 ms |
25824 KB |
Output is correct |
16 |
Correct |
157 ms |
25804 KB |
Output is correct |
17 |
Correct |
135 ms |
25768 KB |
Output is correct |
18 |
Correct |
100 ms |
25792 KB |
Output is correct |
19 |
Correct |
105 ms |
25736 KB |
Output is correct |
20 |
Correct |
11 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
24908 KB |
Output is correct |
2 |
Correct |
64 ms |
25540 KB |
Output is correct |
3 |
Correct |
75 ms |
25516 KB |
Output is correct |
4 |
Correct |
82 ms |
25480 KB |
Output is correct |
5 |
Correct |
74 ms |
25484 KB |
Output is correct |
6 |
Correct |
64 ms |
25540 KB |
Output is correct |
7 |
Correct |
62 ms |
25548 KB |
Output is correct |
8 |
Correct |
59 ms |
25528 KB |
Output is correct |
9 |
Correct |
60 ms |
25548 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
140 ms |
25720 KB |
Output is correct |
12 |
Correct |
141 ms |
25812 KB |
Output is correct |
13 |
Correct |
133 ms |
25812 KB |
Output is correct |
14 |
Correct |
138 ms |
25728 KB |
Output is correct |
15 |
Correct |
143 ms |
25824 KB |
Output is correct |
16 |
Correct |
157 ms |
25804 KB |
Output is correct |
17 |
Correct |
135 ms |
25768 KB |
Output is correct |
18 |
Correct |
100 ms |
25792 KB |
Output is correct |
19 |
Correct |
105 ms |
25736 KB |
Output is correct |
20 |
Correct |
11 ms |
16724 KB |
Output is correct |
21 |
Correct |
225 ms |
26664 KB |
Output is correct |
22 |
Correct |
230 ms |
26652 KB |
Output is correct |
23 |
Correct |
250 ms |
26708 KB |
Output is correct |
24 |
Correct |
219 ms |
26616 KB |
Output is correct |
25 |
Correct |
226 ms |
26624 KB |
Output is correct |
26 |
Correct |
228 ms |
26676 KB |
Output is correct |
27 |
Correct |
214 ms |
26708 KB |
Output is correct |
28 |
Correct |
131 ms |
26720 KB |
Output is correct |
29 |
Correct |
129 ms |
26664 KB |
Output is correct |
30 |
Correct |
11 ms |
16648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
49 ms |
23728 KB |
Output is correct |
3 |
Correct |
46 ms |
23728 KB |
Output is correct |
4 |
Correct |
45 ms |
23720 KB |
Output is correct |
5 |
Correct |
47 ms |
23760 KB |
Output is correct |
6 |
Correct |
47 ms |
23764 KB |
Output is correct |
7 |
Correct |
46 ms |
23744 KB |
Output is correct |
8 |
Correct |
45 ms |
23800 KB |
Output is correct |
9 |
Correct |
45 ms |
23768 KB |
Output is correct |
10 |
Correct |
46 ms |
23752 KB |
Output is correct |
11 |
Correct |
69 ms |
24908 KB |
Output is correct |
12 |
Correct |
64 ms |
25540 KB |
Output is correct |
13 |
Correct |
75 ms |
25516 KB |
Output is correct |
14 |
Correct |
82 ms |
25480 KB |
Output is correct |
15 |
Correct |
74 ms |
25484 KB |
Output is correct |
16 |
Correct |
64 ms |
25540 KB |
Output is correct |
17 |
Correct |
62 ms |
25548 KB |
Output is correct |
18 |
Correct |
59 ms |
25528 KB |
Output is correct |
19 |
Correct |
60 ms |
25548 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
140 ms |
25720 KB |
Output is correct |
22 |
Correct |
141 ms |
25812 KB |
Output is correct |
23 |
Correct |
133 ms |
25812 KB |
Output is correct |
24 |
Correct |
138 ms |
25728 KB |
Output is correct |
25 |
Correct |
143 ms |
25824 KB |
Output is correct |
26 |
Correct |
157 ms |
25804 KB |
Output is correct |
27 |
Correct |
135 ms |
25768 KB |
Output is correct |
28 |
Correct |
100 ms |
25792 KB |
Output is correct |
29 |
Correct |
105 ms |
25736 KB |
Output is correct |
30 |
Correct |
11 ms |
16724 KB |
Output is correct |
31 |
Incorrect |
13 ms |
16732 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |