Skip to content

Commit df3badc

Browse files
authored
dsada
1 parent 17cbf6a commit df3badc

File tree

1 file changed

+271
-0
lines changed

1 file changed

+271
-0
lines changed

fi.cpp

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
5+
#define LL long long
6+
#define FOR(i, j, k) for (auto i=j ; i<k ; i++)
7+
8+
struct pt{
9+
LL l,r;
10+
pt(LL x, LL y){
11+
l=x; r=y;
12+
}
13+
pt(){}
14+
};
15+
16+
struct Node
17+
{
18+
pt key;
19+
Node *left;
20+
Node *right;
21+
int height;
22+
};
23+
24+
int max(int a, int b);
25+
26+
int height(Node *N)
27+
{
28+
if (N == NULL)
29+
return 0;
30+
return N->height;
31+
}
32+
33+
int max(int a, int b)
34+
{
35+
return (a > b)? a : b;
36+
}
37+
38+
Node* newNode(pt key)
39+
{
40+
Node* node = new Node();
41+
node->key = key;
42+
node->left = NULL;
43+
node->right = NULL;
44+
node->height = 1;
45+
return(node);
46+
}
47+
48+
Node *rightRotate(Node *y)
49+
{
50+
Node *x = y->left;
51+
Node *T2 = x->right;
52+
53+
x->right = y;
54+
y->left = T2;
55+
56+
y->height = max(height(y->left),
57+
height(y->right)) + 1;
58+
x->height = max(height(x->left),
59+
height(x->right)) + 1;
60+
61+
return x;
62+
}
63+
64+
Node *leftRotate(Node *x)
65+
{
66+
Node *y = x->right;
67+
Node *T2 = y->left;
68+
69+
y->left = x;
70+
x->right = T2;
71+
72+
x->height = max(height(x->left),
73+
height(x->right)) + 1;
74+
y->height = max(height(y->left),
75+
height(y->right)) + 1;
76+
77+
return y;
78+
}
79+
80+
int getBalance(Node *N)
81+
{
82+
if (N == NULL)
83+
return 0;
84+
return height(N->left) -
85+
height(N->right);
86+
}
87+
88+
Node* insert(Node* node, pt key)
89+
{
90+
if (node == NULL)
91+
return(newNode(key));
92+
93+
if (key.r < node->key.l)
94+
node->left = insert(node->left, key);
95+
else if (key.l > node->key.r)
96+
node->right = insert(node->right, key);
97+
else
98+
return node;
99+
100+
node->height = 1 + max(height(node->left),
101+
height(node->right));
102+
103+
int balance = getBalance(node);
104+
105+
if (balance > 1 && key.r < node->left->key.l)
106+
return rightRotate(node);
107+
108+
if (balance < -1 && key.l > node->right->key.r)
109+
return leftRotate(node);
110+
111+
if (balance > 1 && key.l > node->left->key.r)
112+
{
113+
node->left = leftRotate(node->left);
114+
return rightRotate(node);
115+
}
116+
117+
if (balance < -1 && key.r < node->right->key.l)
118+
{
119+
node->right = rightRotate(node->right);
120+
return leftRotate(node);
121+
}
122+
123+
return node;
124+
}
125+
126+
Node * minValueNode(Node* node)
127+
{
128+
Node* current = node;
129+
130+
while (current->left != NULL)
131+
current = current->left;
132+
133+
return current;
134+
}
135+
136+
Node* deleteNode(Node* root, pt key)
137+
{
138+
139+
if (root == NULL)
140+
return root;
141+
142+
if ( key.r < root->key.l )
143+
root->left = deleteNode(root->left, key);
144+
else if( key.l > root->key.r )
145+
root->right = deleteNode(root->right, key);
146+
else
147+
{
148+
if( (root->left == NULL) ||
149+
(root->right == NULL) )
150+
{
151+
Node *temp = root->left ?
152+
root->left :
153+
root->right;
154+
155+
if (temp == NULL)
156+
{
157+
temp = root;
158+
root = NULL;
159+
}
160+
else
161+
*root = *temp;
162+
free(temp);
163+
}
164+
else
165+
{
166+
Node* temp = minValueNode(root->right);
167+
root->key = temp->key;
168+
root->right = deleteNode(root->right,
169+
temp->key);
170+
}
171+
}
172+
173+
if (root == NULL)
174+
return root;
175+
176+
root->height = 1 + max(height(root->left),
177+
height(root->right));
178+
179+
180+
int balance = getBalance(root);
181+
182+
if (balance > 1 &&
183+
getBalance(root->left) >= 0)
184+
return rightRotate(root);
185+
186+
if (balance > 1 &&
187+
getBalance(root->left) < 0)
188+
{
189+
root->left = leftRotate(root->left);
190+
return rightRotate(root);
191+
}
192+
193+
if (balance < -1 &&
194+
getBalance(root->right) <= 0)
195+
return leftRotate(root);
196+
197+
if (balance < -1 &&
198+
getBalance(root->right) > 0)
199+
{
200+
root->right = rightRotate(root->right);
201+
return leftRotate(root);
202+
}
203+
204+
return root;
205+
}
206+
207+
void search(Node* root, pt &key, LL &d, LL x){
208+
if(root==NULL) return;
209+
if(root->key.l<=x && root->key.r>=x){
210+
d=0; key.l=root->key.l;
211+
key.r = root->key.r;
212+
}
213+
else if(root->key.l> x){
214+
if(root->key.l-x <d || (root->key.l-x == d && key.l>root->key.r)){
215+
d = root->key.l-x;
216+
key.l = root->key.l;
217+
key.r = root->key.r;
218+
}
219+
search(root->left,key,d,x);
220+
}
221+
else {
222+
if(x-root->key.r <d || (x-root->key.r == d && key.l>root->key.r)){
223+
d = x-root->key.r;
224+
key.l = root->key.l;
225+
key.r = root->key.r;
226+
}
227+
search(root->right,key,d,x);
228+
}
229+
}
230+
int main()
231+
{
232+
fastio;
233+
int t=1; cin>>t; int T=1;
234+
while(t--){
235+
Node* root = NULL;
236+
int n,m; cin>>n>>m;
237+
pt v[n];
238+
LL vv[m];
239+
LL ans[m];
240+
FOR(i,0,n){
241+
cin>>v[i].l>>v[i].r;
242+
}
243+
FOR(i,0,m) cin>>vv[i];
244+
FOR(i,0,n){
245+
root=insert(root, pt(v[i].l, v[i].r));
246+
}
247+
FOR(i,0,m){
248+
LL x=vv[i];
249+
LL d = 1e18;
250+
pt p = pt(INT_MAX, INT_MAX);
251+
search(root,p,d,x);
252+
root = deleteNode(root, pt(p.l,p.r));
253+
LL k=x-d>=p.l && x-d<=p.r?x-d:x+d;
254+
255+
ans[i] = k;
256+
if(p.l == p.r) continue;
257+
if(p.l == k)
258+
root = insert(root,pt(k+1,p.r));
259+
else if(p.r==k)
260+
root = insert(root,pt(p.l,k-1));
261+
else {
262+
root = insert(root,pt(p.l,k-1));
263+
root = insert(root,pt(k+1,p.r));
264+
}
265+
}
266+
printf("Case #%d: ",T++);
267+
for(int i =0;i<m;i++) printf("%lld ",ans[i]);
268+
printf("\n");
269+
}
270+
}
271+

0 commit comments

Comments
 (0)