/
static_range_inversions_mo.test.cpp
59 lines (51 loc) · 1.19 KB
/
static_range_inversions_mo.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_inversions_query"
#include "my_template.hpp"
#include "other/io.hpp"
#include "ds/fenwicktree/fenwicktree.hpp"
#include "ds/offline_query/mo.hpp"
void solve() {
LL(N, Q);
VEC(ll, A, N);
vi key = A;
UNIQUE(key);
for (auto&& x: A) x = LB(key, x);
ll K = len(key);
FenwickTree<Monoid_Add<int>> bit(K);
Mo mo;
vi ANS(Q);
FOR(Q) {
LL(L, R);
mo.add(L, R);
}
ll inv = 0;
auto add_l = [&](int i) -> void {
int x = A[i];
inv += bit.sum(x);
bit.add(x, +1);
};
auto rm_l = [&](int i) -> void {
int x = A[i];
bit.add(x, -1);
inv -= bit.sum(x);
};
auto add_r = [&](int i) -> void {
int x = A[i];
inv += bit.sum_all() - bit.sum(x + 1);
bit.add(x, +1);
};
auto rm_r = [&](int i) -> void {
int x = A[i];
bit.add(x, -1);
inv -= bit.sum_all() - bit.sum(x + 1);
};
auto calc = [&](int i) -> void { ANS[i] = inv; };
mo.calc(add_l, add_r, rm_l, rm_r, calc);
for (auto&& x: ANS) print(x);
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << setprecision(15);
solve();
return 0;
}