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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
class tree {
private:
int num;
int ls[maxn], rs[maxn];
public:
int sum[maxn];
double logg[maxn];
void insert(int &x, int l, int r, int pos, int val) {
if (!x) {
x = ++num;
sum[x] = 0;
rs[x] = rs[x] = 0;
}
logg[x] += (double)val * log2(in[pos]);
sum[x] += val;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) {
insert(ls[x], l, mid, pos, val);
} else {
insert(rs[x], mid + 1, r, pos, val);
}
}
int queryCnt(int x, int ql, int qr, int L, int R) { // queryl queryr
if (ql == L && qr == R) return sum[x];
int mid = (L + R) >> 1;
if (qr <= mid) {
return queryCnt(ls[x], ql, qr, L, mid);
} else {
if (ql > mid) {
return queryCnt(rs[x], ql, qr, mid + 1, R);
} else {
return queryCnt(ls[x], ql, mid, L, mid) +
queryCnt(rs[x], mid + 1, qr, mid + 1, R);
}
}
}
int queryNumK(int x, int l, int r, int k) {
if (!x) return 0;
if (l == r) return l;
int mid = (l + r) / 2;
if (sum[ls[x]] < k) return queryNumK(rs[x], mid + 1, r, k - sum[ls[x]]);
return queryNumK(ls[x], l, mid, k);
}
int merge(int rootx, int rooty) {
if (!rootx) return rooty;
if (!rooty) return rootx;
sum[rootx] += sum[rooty];
logg[rootx] += logg[rooty];
ls[rootx] = merge(ls[rootx], ls[rooty]);
rs[rootx] = merge(rs[rootx], rs[rooty]);
return rootx;
}
void del(int &x, int l, int r, int dl, int dr) { // dl = want to del
if (l == dl && r == dr) {
x = logg[x] = ls[x] = rs[x] = sum[x] = 0;
return;
}
int mid = (l + r) / 2;
if (dr <= mid) {
del(ls[x], l, mid, dl, dr);
} else {
if (dl > mid) {
del(rs[x], mid + 1, r, dl, dr);
} else {
del(ls[x], l, mid, dl, mid);
del(rs[x], mid + 1, r, mid + 1, dr);
}
}
sum[x] = sum[ls[x]] + sum[rs[x]];
logg[x] = logg[ls[x]] + logg[rs[x]];
}
} tr;
|