-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathImplicit segment tree.cpp
106 lines (96 loc) · 2.25 KB
/
Implicit segment tree.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <cassert>
#include <iomanip>
#include <ctime>
using namespace std;
const int me = 100025;
const int sz = 1200000000;
struct node{
int sum;
node *l, *r;
node() {}
};
typedef node * pnode;
int get(pnode &t){
return t ? t->sum : 0;
}
pnode Init(int sum){
pnode p = (pnode)malloc(sizeof(node));
p->sum = sum;
p->l = NULL;
p->r = NULL;
return p;
}
void add(int low, int high, pnode &root, int pos, int value){
if(!root)
root = Init(0);
if(low == high){
root->sum += value;
return;
}
int mid = (low + high) >> 1;
if(pos <= mid)
add(low, mid, root->l, pos, value);
else add(mid + 1, high, root->r, pos, value);
root->sum = get(root->l) + get(root->r);
}
int get(int low, int high, pnode &root, int l, int r){
if(low > r || high < l)
return 0;
if(!root)
return 0;
if(low >= l && high <= r)
return root->sum;
int mid = (low + high) >> 1;
return get(low, mid, root->l, l, r)
+ get(mid + 1, high, root->r, l, r);
}
int Kth_element(int low, int high, pnode &root, int k){
if(low == high)
return low;
int mid = (low + high) >> 1;
int on_left = get(root->l);
if(on_left >= k)
return Kth_element(low, mid, root->l, k);
else return Kth_element(mid + 1, high, root-> r, k - on_left);
}
int n, q, type, x;
pnode root;
int main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
root = Init(0);
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i ++){
scanf("%d", &x);
add(1, sz, root, x, 1);
}
while(q --){
scanf("%d%d", &type, &x);
if(type == 1){
int cnt = get(1, sz, root, 1, x);
add(1, sz, root, cnt + x, 1);
}
else if(type == 2){
printf("%d\n", get(1, sz, root, 1, x));
}
else{
if(get(root) < x)
printf("invalid\n");
else printf("%d\n", Kth_element(1, sz, root, x));
}
}
return 0;
}