#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> iset;
#define FOR(i, j, k) for (ll i = j; i < (ll) k; ++i)
#define FORD(i, j, k) for (ll i = j; i >= (ll) k; --i)
#define nl "\n"
#define sp " "
#define all(x) (x).begin(), (x).end()
#define sc second
#define fr first
#define pb push_back
struct SegTree {
vi tree;
ll id;
ll n;
SegTree(int N) {
init(N, 0);
}
SegTree(int N, int x) {
init(N, x);
}
void init(int N, int x) {
id = x;
n = N;
while(__builtin_popcountll(n) != 1) ++n;
tree.resize(2 * n, id);
}
void update(ll x, ll k) {
k += n;
tree[k] += x;
for(k /= 2; k >= 1; k /= 2) {
tree[k] = comb(tree[2 * k], tree[2 * k + 1]);
}
}
ll query(int a, int b, int x, int y, int k) {
if(a > y || b < x) return id;
if(a <= x && y <= b) return tree[k];
int d = (x + y) / 2;
return comb(query(a, b, x, d, 2 * k), query(a, b, d + 1, y, 2 * k + 1));
}
ll query(int a, int b) {
return query(a, b, 0, n - 1, 1);
}
ll comb(ll lhs, ll rhs) {
return lhs + rhs;
}
void print() {
FOR(i, 1, 2 * n) {
cout << i << " : " << tree[i] << nl;
}
}
};
void solve()
{
ll n;
cin >> n;
ll a[n];
FOR(i, 0, n)
cin >> a[i];
ll b[n];
FOR(i, 0, n)
b[i] = a[i];
sort(b, b + n);
map<ll, ll> m;
ll curr = 1;
FOR(i, 0, n) {
if(m.find(b[i]) == m.end()) {
m[b[i]] = curr++;
}
}
FOR(i, 0, n) {
a[i] = m[a[i]];
}
SegTree sgt(n + 1);
ll left[n], right[n];
FOR(i, 0, n) {
left[i] = sgt.query(0, a[i] - 1);
sgt.update(1, a[i]);
}
FOR(i, 0, n) {
sgt.update(-1, a[i]);
}
FORD(i, n - 1, 0) {
right[i] = sgt.query(0, a[i] - 1);
sgt.update(1, a[i]);
}
ll ans = 0;
FOR(i, 0, n) {
ans += left[i] * right[i];
}
cout << ans << nl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
while (t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
280 ms |
42184 KB |
Output is correct |
3 |
Correct |
284 ms |
42192 KB |
Output is correct |
4 |
Correct |
280 ms |
42160 KB |
Output is correct |
5 |
Correct |
281 ms |
42128 KB |
Output is correct |
6 |
Correct |
287 ms |
42216 KB |
Output is correct |
7 |
Correct |
284 ms |
42184 KB |
Output is correct |
8 |
Correct |
288 ms |
42368 KB |
Output is correct |
9 |
Correct |
243 ms |
33100 KB |
Output is correct |
10 |
Correct |
244 ms |
33096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
18396 KB |
Output is correct |
2 |
Correct |
153 ms |
18392 KB |
Output is correct |
3 |
Correct |
153 ms |
18480 KB |
Output is correct |
4 |
Correct |
152 ms |
18516 KB |
Output is correct |
5 |
Correct |
154 ms |
18388 KB |
Output is correct |
6 |
Correct |
155 ms |
18380 KB |
Output is correct |
7 |
Correct |
151 ms |
18380 KB |
Output is correct |
8 |
Correct |
148 ms |
18380 KB |
Output is correct |
9 |
Correct |
152 ms |
18516 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
18396 KB |
Output is correct |
2 |
Correct |
153 ms |
18392 KB |
Output is correct |
3 |
Correct |
153 ms |
18480 KB |
Output is correct |
4 |
Correct |
152 ms |
18516 KB |
Output is correct |
5 |
Correct |
154 ms |
18388 KB |
Output is correct |
6 |
Correct |
155 ms |
18380 KB |
Output is correct |
7 |
Correct |
151 ms |
18380 KB |
Output is correct |
8 |
Correct |
148 ms |
18380 KB |
Output is correct |
9 |
Correct |
152 ms |
18516 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
151 ms |
18768 KB |
Output is correct |
12 |
Correct |
156 ms |
18764 KB |
Output is correct |
13 |
Correct |
153 ms |
18660 KB |
Output is correct |
14 |
Correct |
150 ms |
18656 KB |
Output is correct |
15 |
Correct |
155 ms |
18764 KB |
Output is correct |
16 |
Correct |
151 ms |
18764 KB |
Output is correct |
17 |
Correct |
157 ms |
18664 KB |
Output is correct |
18 |
Correct |
124 ms |
18772 KB |
Output is correct |
19 |
Correct |
125 ms |
18776 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
428 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
428 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
9 ms |
1612 KB |
Output is correct |
12 |
Correct |
10 ms |
1620 KB |
Output is correct |
13 |
Correct |
9 ms |
1616 KB |
Output is correct |
14 |
Correct |
9 ms |
1620 KB |
Output is correct |
15 |
Correct |
10 ms |
1620 KB |
Output is correct |
16 |
Correct |
9 ms |
1620 KB |
Output is correct |
17 |
Correct |
10 ms |
1612 KB |
Output is correct |
18 |
Correct |
8 ms |
1236 KB |
Output is correct |
19 |
Correct |
6 ms |
948 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
18396 KB |
Output is correct |
2 |
Correct |
153 ms |
18392 KB |
Output is correct |
3 |
Correct |
153 ms |
18480 KB |
Output is correct |
4 |
Correct |
152 ms |
18516 KB |
Output is correct |
5 |
Correct |
154 ms |
18388 KB |
Output is correct |
6 |
Correct |
155 ms |
18380 KB |
Output is correct |
7 |
Correct |
151 ms |
18380 KB |
Output is correct |
8 |
Correct |
148 ms |
18380 KB |
Output is correct |
9 |
Correct |
152 ms |
18516 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
151 ms |
18768 KB |
Output is correct |
12 |
Correct |
156 ms |
18764 KB |
Output is correct |
13 |
Correct |
153 ms |
18660 KB |
Output is correct |
14 |
Correct |
150 ms |
18656 KB |
Output is correct |
15 |
Correct |
155 ms |
18764 KB |
Output is correct |
16 |
Correct |
151 ms |
18764 KB |
Output is correct |
17 |
Correct |
157 ms |
18664 KB |
Output is correct |
18 |
Correct |
124 ms |
18772 KB |
Output is correct |
19 |
Correct |
125 ms |
18776 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
316 ms |
25476 KB |
Output is correct |
22 |
Correct |
307 ms |
25468 KB |
Output is correct |
23 |
Correct |
314 ms |
25476 KB |
Output is correct |
24 |
Correct |
309 ms |
25540 KB |
Output is correct |
25 |
Correct |
321 ms |
25548 KB |
Output is correct |
26 |
Correct |
333 ms |
25496 KB |
Output is correct |
27 |
Correct |
330 ms |
25764 KB |
Output is correct |
28 |
Correct |
196 ms |
25556 KB |
Output is correct |
29 |
Correct |
199 ms |
25460 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
280 ms |
42184 KB |
Output is correct |
3 |
Correct |
284 ms |
42192 KB |
Output is correct |
4 |
Correct |
280 ms |
42160 KB |
Output is correct |
5 |
Correct |
281 ms |
42128 KB |
Output is correct |
6 |
Correct |
287 ms |
42216 KB |
Output is correct |
7 |
Correct |
284 ms |
42184 KB |
Output is correct |
8 |
Correct |
288 ms |
42368 KB |
Output is correct |
9 |
Correct |
243 ms |
33100 KB |
Output is correct |
10 |
Correct |
244 ms |
33096 KB |
Output is correct |
11 |
Correct |
158 ms |
18396 KB |
Output is correct |
12 |
Correct |
153 ms |
18392 KB |
Output is correct |
13 |
Correct |
153 ms |
18480 KB |
Output is correct |
14 |
Correct |
152 ms |
18516 KB |
Output is correct |
15 |
Correct |
154 ms |
18388 KB |
Output is correct |
16 |
Correct |
155 ms |
18380 KB |
Output is correct |
17 |
Correct |
151 ms |
18380 KB |
Output is correct |
18 |
Correct |
148 ms |
18380 KB |
Output is correct |
19 |
Correct |
152 ms |
18516 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
151 ms |
18768 KB |
Output is correct |
22 |
Correct |
156 ms |
18764 KB |
Output is correct |
23 |
Correct |
153 ms |
18660 KB |
Output is correct |
24 |
Correct |
150 ms |
18656 KB |
Output is correct |
25 |
Correct |
155 ms |
18764 KB |
Output is correct |
26 |
Correct |
151 ms |
18764 KB |
Output is correct |
27 |
Correct |
157 ms |
18664 KB |
Output is correct |
28 |
Correct |
124 ms |
18772 KB |
Output is correct |
29 |
Correct |
125 ms |
18776 KB |
Output is correct |
30 |
Correct |
1 ms |
316 KB |
Output is correct |
31 |
Correct |
1 ms |
428 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
0 ms |
320 KB |
Output is correct |
41 |
Correct |
9 ms |
1612 KB |
Output is correct |
42 |
Correct |
10 ms |
1620 KB |
Output is correct |
43 |
Correct |
9 ms |
1616 KB |
Output is correct |
44 |
Correct |
9 ms |
1620 KB |
Output is correct |
45 |
Correct |
10 ms |
1620 KB |
Output is correct |
46 |
Correct |
9 ms |
1620 KB |
Output is correct |
47 |
Correct |
10 ms |
1612 KB |
Output is correct |
48 |
Correct |
8 ms |
1236 KB |
Output is correct |
49 |
Correct |
6 ms |
948 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
316 ms |
25476 KB |
Output is correct |
52 |
Correct |
307 ms |
25468 KB |
Output is correct |
53 |
Correct |
314 ms |
25476 KB |
Output is correct |
54 |
Correct |
309 ms |
25540 KB |
Output is correct |
55 |
Correct |
321 ms |
25548 KB |
Output is correct |
56 |
Correct |
333 ms |
25496 KB |
Output is correct |
57 |
Correct |
330 ms |
25764 KB |
Output is correct |
58 |
Correct |
196 ms |
25556 KB |
Output is correct |
59 |
Correct |
199 ms |
25460 KB |
Output is correct |
60 |
Correct |
1 ms |
316 KB |
Output is correct |
61 |
Correct |
528 ms |
42128 KB |
Output is correct |
62 |
Correct |
552 ms |
42128 KB |
Output is correct |
63 |
Correct |
527 ms |
42132 KB |
Output is correct |
64 |
Correct |
539 ms |
42124 KB |
Output is correct |
65 |
Correct |
560 ms |
42200 KB |
Output is correct |
66 |
Correct |
541 ms |
42204 KB |
Output is correct |
67 |
Correct |
540 ms |
42132 KB |
Output is correct |
68 |
Correct |
427 ms |
33096 KB |
Output is correct |
69 |
Correct |
357 ms |
27768 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |