This repository was archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynalarm.cpp
More file actions
174 lines (174 loc) · 4.58 KB
/
dynalarm.cpp
File metadata and controls
174 lines (174 loc) · 4.58 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <utility>
using std::pair;
using std::make_pair;
typedef long long ll;
class SBTElmt{
public:
SBTElmt *lson, *rson;
unsigned size, data;
SBTElmt(){
lson = rson = NULL;
size = 1;
data = 0;
}
SBTElmt(unsigned val){
lson = rson = NULL;
size = 1;
data = val;
}
};
class SBT{
private:
SBTElmt *root;
void ins_(SBTElmt*&, const unsigned);
unsigned del_(SBTElmt*&, const unsigned);
unsigned rank_(const SBTElmt*, const unsigned);
void left_rotate(SBTElmt*&);
void right_rotate(SBTElmt*&);
void maintain(SBTElmt*&, bool);
public:
SBT();
void ins(const unsigned);
unsigned del(const unsigned);
unsigned rank(const unsigned);
unsigned size();
};
void SBT::ins_(SBTElmt *&root, const unsigned val){
++root -> size;
if (val < root -> data){
if (root -> lson == NULL) root -> lson = new SBTElmt(val);
else ins_(root -> lson, val);
}
else{
if (root -> rson == NULL) root -> rson = new SBTElmt(val);
else ins_(root -> rson, val);
}
maintain(root, val >= root -> data);
};
unsigned SBT::del_(SBTElmt *&root, const unsigned rank){
--root -> size;
if (root -> lson == NULL){
if (rank == 0){
SBTElmt *tmp = root;
unsigned val = root -> data;
if (root -> rson == NULL) root = NULL;
else root = root -> rson;
delete(tmp);
return val;
}
else return del_(root -> rson, rank - 1);
}
else{
if (rank == root -> lson -> size){
unsigned val = root -> data;
root -> data = del_(root -> lson, rank - 1);
return val;
}
else{
if (rank < root -> lson -> size) return del_(root -> lson, rank);
else return del_(root -> rson, rank - root -> lson -> size - 1);
}
}
};
unsigned SBT::rank_(const SBTElmt *root, const unsigned n){
if (root -> lson == NULL) return n ? rank_(root -> rson, n - 1) : root -> data;
if (n == root -> lson -> size) return root -> data;
if (n < root -> lson -> size) return rank_(root -> lson, n);
else return rank_(root -> rson, n - root -> lson -> size - 1);
};
void SBT::left_rotate(SBTElmt *&root){
if (root -> rson == NULL) return;
SBTElmt *tmp = root -> rson;
root -> rson = tmp -> lson;
tmp -> lson = root;
tmp -> size = root -> size;
root -> size = 1;
if (root -> lson) root -> size += root -> lson -> size;
if (root -> rson) root -> size += root -> rson -> size;
root = tmp;
};
void SBT::right_rotate(SBTElmt *&root){
if (root -> lson == NULL) return;
SBTElmt *tmp = root -> lson;
root -> lson = tmp -> rson;
tmp -> rson = root;
tmp -> size = root -> size;
root -> size = 1;
if (root -> lson) root -> size += root -> lson -> size;
if (root -> rson) root -> size += root -> rson -> size;
root = tmp;
};
void SBT::maintain(SBTElmt *&root, bool flag){
if (!flag){
if (root -> lson == NULL) return;
if (root -> lson -> lson && (root -> rson == NULL || root -> lson -> lson -> size > root -> rson -> size)) right_rotate(root);
else if (root -> lson -> rson && (root -> rson == NULL || root -> lson -> rson -> size > root -> rson -> size)){
left_rotate(root -> lson);
right_rotate(root);
}
else return;
}
else{
if (root -> rson == NULL) return;
if (root -> rson -> rson && (root -> lson == NULL || root -> rson -> rson -> size > root -> lson -> size)) left_rotate(root);
else if (root -> rson -> lson && (root -> lson == NULL || root -> rson -> lson -> size > root -> lson -> size)){
right_rotate(root -> rson);
left_rotate(root);
}
else return;
}
maintain(root -> lson, false);
maintain(root -> rson, true);
maintain(root, false);
maintain(root, true);
};
SBT::SBT(){
root = new SBTElmt(1 << 31);
}
void SBT::ins(const unsigned val){
ins_(root, val);
}
unsigned SBT::del(const unsigned rank){
return del_(root, rank);
}
unsigned SBT::rank(const unsigned n){
return rank_(root, n);
}
unsigned SBT::size(){
return root -> size;
}
ll operator *(const pair<ll, ll> &lhs, const pair<ll, ll> &rhs){
return lhs.first * rhs.second - lhs.second * rhs.first;
}
int main(){
int n;
scanf("%d", &n);
SBT *A = new SBT();
SBT *B = new SBT();
int op, a, b;
int l, mid, r;
while (n--){
scanf("%d%d%d", &op, &a, &b);
switch (op){
case 0:
A -> ins(a);
B -> ins(b);
break;
case 1:
l = 0, r = A -> size();
while (l < r){
mid = l + r >> 1;
ll x = (ll)A -> rank(mid), y = (ll)B -> rank(mid);
if (make_pair(-x, y) * make_pair(a - x, b) < 0) l = mid + 1;
else r = mid;
}
printf("%d\n", l);
break;
case 2:
A -> del(a);
B -> del(b);
}
}
return 0;
}