-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA3.cpp
397 lines (397 loc) · 8.79 KB
/
A3.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Market Display Publisher
// Constructs AVL Tree in O(n) time
#include <iostream>
using namespace std;
typedef long int num;
typedef struct Tree* tree;
typedef struct Sort sort;
struct Tree{
num id = 0;
num val = 0;
tree left = nullptr;
tree right = nullptr;
num key = 0;
};
struct Sort{
num id = 0;
num val = 0;
};
num mdp = 0;
void Merge(sort a[], num l, num m, num r, sort arr[])
{
num p = l;
num q = m+1;
num k = 0;
for(num i = l; i <= r; i++)
{
if(q > r)
{
arr[k++] = a[p++];
continue;
}
else if(p > m)
{
arr[k++] = a[q++];
continue;
}
else if(a[p].id > a[q].id)
{
arr[k++] = a[q++];
continue;
}
else
arr[k++] = a[p++];
}
for(num i = 0; i < k; i++)
{
a[l++] = arr[i];
}
}
void MergeSort(sort a[], num l, num r, sort temp[])
{
if(l < r)
{
num m = (l + r)/2;
MergeSort(a, l, m, temp);
MergeSort(a, m+1, r, temp);
Merge(a, l, m, r, temp);
}
}
num max(num a, num b)
{
return (a>b ? a : b);
}
num absolute(num a)
{
return a > 0 ? a : -a;
}
tree onetree()
{
tree down = (tree)malloc(sizeof(struct Tree));
down->id = 0;
down->val = 0;
down->key = 0;
down->left = nullptr;
down->right = nullptr;
return down;
}
tree CreateTree(sort a[], num l, num r)
{
num m = (l+r)/2;
if(l <= r)
{
tree node = (tree)malloc(sizeof(struct Tree));
node->id = a[m].id;
node->val = a[m].val;
node->right = CreateTree(a, m + 1, r);
node->left = CreateTree(a, l, m - 1);
node->key = max(node->right->key, node->left->key) + 1;
return node;
}
return onetree();
}
void llrot(tree z)
{
tree y = z->left;
struct Tree t = *z;
*z = *y;
t.left = y->right;
t.key = max(t.left->key, t.right->key) + 1;
*y = t;
z->right = y;
z->key = max(z->right->key, z->left->key) + 1;
}
void rrrot(tree z)
{
tree y = z->right;
struct Tree t = *z;
*z = *y;
t.right = y->left;
t.key = max(t.left->key, t.right->key) + 1;
*y = t;
z->left = y;
z->key = max(z->right->key, z->left->key) + 1;
}
void lrrot(tree z)
{
tree y = z->left;
tree x = y->right;
struct Tree t = *y;
*y = *x;
t.right = y->left;
t.key = max(t.left->key, t.right->key) + 1;
*x = t;
y->left = x;
y->key = max(y->left->key, y->right->key) + 1;
struct Tree tt = *z;
*z = *y;
tt.left = z->right;
tt.key = max(tt.left->key, tt.right->key) + 1;
*y = tt;
z->right = y;
z->key = max(z->right->key, z->left->key) + 1;
}
void rlrot(tree z)
{
tree y = z->right;
tree x = y->left;
struct Tree t = *y;
*y = *x;
t.left = y->right;
t.key = max(t.left->key, t.right->key) + 1;
*x = t;
y->right = x;
y->key = max(y->left->key, y->right->key) + 1;
struct Tree tt = *z;
*z = *y;
//tt.left = z->right;
tt.right = z->left;
tt.key = max(tt.left->key, tt.right->key) + 1;
*y = tt;
z->left = y;
z->key = max(z->right->key, z->left->key) + 1;
}
tree insertTree(tree down, num id, num val)
{
if(!down)
{
down = (tree) malloc(sizeof(struct Tree));
down->id = id;
down->val = val;
down->key = 1;
down->left = onetree();
down->right = onetree();
return down;
}
if (down->id == 0)
{
down = (tree) malloc(sizeof(struct Tree));
down->id = id;
down->val = val;
down->key = 1;
down->left = onetree();
down->right = onetree();
return down;
}
else if (down->id != 0)
{
if (id > down->id)
{
down->right = insertTree(down->right, id, val);
}
else if (id < down->id)
{
down->left = insertTree(down->left, id, val);
}
if(absolute(down->right->key - down->left->key) <= 1)
{
down->key = max(down->right->key, down->left->key) + 1;
return down;
}
else if(down->left->key - down->right->key == 2)
{
if(down->left->left->key > down->left->right->key)
{
llrot(down);
return down;
} else{
lrrot(down);
return down;
}
}
else if(down->right->key - down->left->key == 2)
{
if(down->right->right->key > down->right->left->key)
{
rrrot(down);
return down;
} else{
rlrot(down);
return down;
}
}
}
return down;
}
tree successor(tree down)
{
while(down->left->id != 0)
{
down = down->left;
}
return down;
}
void rotate(tree down)
{
if(down->id != 0)
{
if(down->left->key - down->right->key == 2)
{
if(down->left->left->key >= down->left->right->key)
{
llrot(down);
} else{
lrrot(down);
}
}
else if(down->right->key - down->left->key == 2)
{
if(down->right->right->key >= down->right->left->key)
{
rrrot(down);
} else{
rlrot(down);
}
}
}
}
tree removeTree(tree down, num id)
{
if (down->id == 0)
{
return down;
}
else if(down->id == id)
{
if(!down->right->id && !down->left->id)
{
down->id = 0;
down->val = 0;
down->key = 0;
free(down->right);
free(down->left);
down->right = nullptr;
down->left = nullptr;
return down;
}
else if(!down->left->id)
{
tree t = down->right;
free(down);
return t;
}
else if(!down->right->id)
{
tree t = down->left;
free(down);
return t;
} else{
tree temp = successor(down->right);
down->id = temp->id;
down->val = temp->val;
down->right = removeTree(down->right, temp->id);
down->key = max(down->left->key, down->right->key) + 1;
return down;
}
}
else if(down->id > id)
{
down->left = removeTree(down->left, id);
rotate(down);
down->key = max(down->left->key, down->right->key)+1;
return down;
}
else if(down->id < id)
{
down->right = removeTree(down->right, id);
rotate(down);
down->key = max(down->left->key, down->right->key)+1;
return down;
}
return down;
}
void updatePrice(tree down, num id, num val)
{
if(down->id == 0)
return;
else if(down->id == id)
{
if(absolute(val - down->val) > mdp)
{
down->val = val;
cout << down->id<< ' ' << down->val << endl;
}
return;
}
else if(down->id > id)
{
down = down->left;
updatePrice(down, id, val);
return;
}
else if (down->id < id)
{
down = down->right;
updatePrice(down, id, val);
return;
}
}
void updateStock(tree down, num id, num x, num y)
{
if(down->id == 0)
return;
else if(down->id == id)
{
down->val = (down->val*y)/x;
cout << down->id << ' ' << down->val << endl;
}
else if(down->id > id)
{
down = down->left;
updateStock(down, id, x, y);
return;
}
else if (down->id < id)
{
down = down->right;
updateStock(down, id, x, y);
return;
}
}
int main() {
num n, N;
cin >> n;
sort a[n];
sort temp[n];
for(num i = 0; i < n; i++)
{
cin >> a[i].id;
cin >> a[i].val;
}
MergeSort(a, 0, n-1, temp);
tree super = nullptr;
super = CreateTree(a, 0, n-1);
cin >> N >> mdp;
num oper;
num inone, intwo, y;
char colan;
for(num i = 0; i < N; i++)
{
cin >> oper;
if(oper == 1)
{
cin >> inone >> intwo;
super = insertTree(super, inone, intwo);
cout << inone << ' ' << intwo << endl;
}
else if(oper == 2)
{
cin >> inone;
super = removeTree(super, inone);
if(super->left == nullptr)
super->left = onetree();
if(super->right == nullptr)
super->right = onetree();
}
else if(oper == 3)
{
cin >> inone >> intwo;
updatePrice(super, inone, intwo);
}
else if(oper == 4)
{
cin >> inone >> intwo >> colan >> y;
updateStock(super, inone, intwo, y);
}
}
}