#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] * 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:36:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
36 | int tm = tl + tr >> 1;
| ~~~^~~~
Mountains.cpp: In function 'll get(int, int, int, int, int)':
Mountains.cpp:45:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int tm = tl + tr >> 1;
| ~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
48 ms |
37448 KB |
Output is correct |
3 |
Correct |
49 ms |
37524 KB |
Output is correct |
4 |
Correct |
53 ms |
37516 KB |
Output is correct |
5 |
Correct |
55 ms |
37512 KB |
Output is correct |
6 |
Correct |
49 ms |
37568 KB |
Output is correct |
7 |
Correct |
50 ms |
37464 KB |
Output is correct |
8 |
Correct |
59 ms |
37500 KB |
Output is correct |
9 |
Correct |
51 ms |
37544 KB |
Output is correct |
10 |
Correct |
56 ms |
37500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
37648 KB |
Output is correct |
2 |
Correct |
92 ms |
37544 KB |
Output is correct |
3 |
Correct |
92 ms |
37708 KB |
Output is correct |
4 |
Correct |
104 ms |
37576 KB |
Output is correct |
5 |
Correct |
90 ms |
37616 KB |
Output is correct |
6 |
Correct |
91 ms |
37624 KB |
Output is correct |
7 |
Correct |
89 ms |
37544 KB |
Output is correct |
8 |
Correct |
84 ms |
37600 KB |
Output is correct |
9 |
Correct |
86 ms |
37636 KB |
Output is correct |
10 |
Correct |
17 ms |
31660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
37648 KB |
Output is correct |
2 |
Correct |
92 ms |
37544 KB |
Output is correct |
3 |
Correct |
92 ms |
37708 KB |
Output is correct |
4 |
Correct |
104 ms |
37576 KB |
Output is correct |
5 |
Correct |
90 ms |
37616 KB |
Output is correct |
6 |
Correct |
91 ms |
37624 KB |
Output is correct |
7 |
Correct |
89 ms |
37544 KB |
Output is correct |
8 |
Correct |
84 ms |
37600 KB |
Output is correct |
9 |
Correct |
86 ms |
37636 KB |
Output is correct |
10 |
Correct |
17 ms |
31660 KB |
Output is correct |
11 |
Correct |
154 ms |
37624 KB |
Output is correct |
12 |
Correct |
153 ms |
37624 KB |
Output is correct |
13 |
Correct |
187 ms |
37644 KB |
Output is correct |
14 |
Correct |
159 ms |
37552 KB |
Output is correct |
15 |
Correct |
158 ms |
37600 KB |
Output is correct |
16 |
Correct |
150 ms |
37656 KB |
Output is correct |
17 |
Correct |
161 ms |
37616 KB |
Output is correct |
18 |
Correct |
110 ms |
37632 KB |
Output is correct |
19 |
Correct |
108 ms |
37608 KB |
Output is correct |
20 |
Correct |
15 ms |
31572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
31620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
31620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
37648 KB |
Output is correct |
2 |
Correct |
92 ms |
37544 KB |
Output is correct |
3 |
Correct |
92 ms |
37708 KB |
Output is correct |
4 |
Correct |
104 ms |
37576 KB |
Output is correct |
5 |
Correct |
90 ms |
37616 KB |
Output is correct |
6 |
Correct |
91 ms |
37624 KB |
Output is correct |
7 |
Correct |
89 ms |
37544 KB |
Output is correct |
8 |
Correct |
84 ms |
37600 KB |
Output is correct |
9 |
Correct |
86 ms |
37636 KB |
Output is correct |
10 |
Correct |
17 ms |
31660 KB |
Output is correct |
11 |
Correct |
154 ms |
37624 KB |
Output is correct |
12 |
Correct |
153 ms |
37624 KB |
Output is correct |
13 |
Correct |
187 ms |
37644 KB |
Output is correct |
14 |
Correct |
159 ms |
37552 KB |
Output is correct |
15 |
Correct |
158 ms |
37600 KB |
Output is correct |
16 |
Correct |
150 ms |
37656 KB |
Output is correct |
17 |
Correct |
161 ms |
37616 KB |
Output is correct |
18 |
Correct |
110 ms |
37632 KB |
Output is correct |
19 |
Correct |
108 ms |
37608 KB |
Output is correct |
20 |
Correct |
15 ms |
31572 KB |
Output is correct |
21 |
Correct |
443 ms |
46576 KB |
Output is correct |
22 |
Correct |
413 ms |
46508 KB |
Output is correct |
23 |
Correct |
347 ms |
46552 KB |
Output is correct |
24 |
Correct |
398 ms |
46440 KB |
Output is correct |
25 |
Correct |
375 ms |
46540 KB |
Output is correct |
26 |
Correct |
362 ms |
46544 KB |
Output is correct |
27 |
Correct |
364 ms |
46524 KB |
Output is correct |
28 |
Correct |
191 ms |
46480 KB |
Output is correct |
29 |
Correct |
195 ms |
46556 KB |
Output is correct |
30 |
Correct |
15 ms |
31644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
31572 KB |
Output is correct |
2 |
Correct |
48 ms |
37448 KB |
Output is correct |
3 |
Correct |
49 ms |
37524 KB |
Output is correct |
4 |
Correct |
53 ms |
37516 KB |
Output is correct |
5 |
Correct |
55 ms |
37512 KB |
Output is correct |
6 |
Correct |
49 ms |
37568 KB |
Output is correct |
7 |
Correct |
50 ms |
37464 KB |
Output is correct |
8 |
Correct |
59 ms |
37500 KB |
Output is correct |
9 |
Correct |
51 ms |
37544 KB |
Output is correct |
10 |
Correct |
56 ms |
37500 KB |
Output is correct |
11 |
Correct |
90 ms |
37648 KB |
Output is correct |
12 |
Correct |
92 ms |
37544 KB |
Output is correct |
13 |
Correct |
92 ms |
37708 KB |
Output is correct |
14 |
Correct |
104 ms |
37576 KB |
Output is correct |
15 |
Correct |
90 ms |
37616 KB |
Output is correct |
16 |
Correct |
91 ms |
37624 KB |
Output is correct |
17 |
Correct |
89 ms |
37544 KB |
Output is correct |
18 |
Correct |
84 ms |
37600 KB |
Output is correct |
19 |
Correct |
86 ms |
37636 KB |
Output is correct |
20 |
Correct |
17 ms |
31660 KB |
Output is correct |
21 |
Correct |
154 ms |
37624 KB |
Output is correct |
22 |
Correct |
153 ms |
37624 KB |
Output is correct |
23 |
Correct |
187 ms |
37644 KB |
Output is correct |
24 |
Correct |
159 ms |
37552 KB |
Output is correct |
25 |
Correct |
158 ms |
37600 KB |
Output is correct |
26 |
Correct |
150 ms |
37656 KB |
Output is correct |
27 |
Correct |
161 ms |
37616 KB |
Output is correct |
28 |
Correct |
110 ms |
37632 KB |
Output is correct |
29 |
Correct |
108 ms |
37608 KB |
Output is correct |
30 |
Correct |
15 ms |
31572 KB |
Output is correct |
31 |
Incorrect |
12 ms |
31620 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |