'commit'
This commit is contained in:
52
TangDou/AcWing/BalanceTree/TODO/253_Szsz.cpp
Normal file
52
TangDou/AcWing/BalanceTree/TODO/253_Szsz.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
//为什么这个平衡树也可以用树状数组来做呢?
|
||||
const int N = 1e5 + 10;
|
||||
|
||||
int n, tot, sz;
|
||||
int op[N], a[N], t[N], c[N];
|
||||
|
||||
int pos(int x) {
|
||||
return lower_bound(t + 1, t + 1 + sz, x) - t;
|
||||
}
|
||||
void add(int x, int k) {
|
||||
for (x; x <= sz; x += x & -x) c[x] += k;
|
||||
}
|
||||
int query(int x) {
|
||||
int tmp = 0;
|
||||
for (x; x; x -= x & -x) tmp += c[x];
|
||||
return tmp;
|
||||
}
|
||||
int kth(int x) {
|
||||
int rs = 0;
|
||||
for (int i = 17; i >= 0; --i) {
|
||||
rs += (1 << i);
|
||||
if (rs > sz || c[rs] >= x)
|
||||
rs -= (1 << i);
|
||||
else
|
||||
x -= c[rs];
|
||||
}
|
||||
return t[rs + 1];
|
||||
}
|
||||
int main() {
|
||||
scanf("%d", &n);
|
||||
//树状数组为啥需要离线处理呢
|
||||
for (int i = 1; i <= n; i++) {
|
||||
scanf("%d%d", &op[i], &a[i]);
|
||||
if (op[i] != 4) t[++tot] = a[i];
|
||||
}
|
||||
//离散化
|
||||
sort(t + 1, t + 1 + tot);
|
||||
sz = unique(t + 1, t + 1 + tot) - t - 1;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
int x = a[i];
|
||||
if (op[i] == 1) add(pos(x), 1);
|
||||
if (op[i] == 2) add(pos(x), -1);
|
||||
if (op[i] == 3) printf("%d\n", query(pos(x) - 1) + 1);
|
||||
if (op[i] == 4) printf("%d\n", kth(x));
|
||||
if (op[i] == 5) printf("%d\n", kth(query(pos(x) - 1)));
|
||||
if (op[i] == 6) printf("%d\n", kth(query(pos(x)) + 1));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user