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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// C++ program to insert a node in AVL tree
#include<bits/stdc++.h>
using namespace std;

bool DEBUG = false;

// An AVL tree node
class Node
{
    public:
        long long int key;
        Node *left;
        Node *right;
        long long int howMany;
        long long int height;
};

// A utility function to get maximum
// of two integers
long long int max(long long int a, long long int b);

// A utility function to get height
// of the tree
long long int height(Node *N)
{
    if (N == nullptr)
        return 0;
    return N->height;
}

// A utility function to get maximum
// of two integers
long long int max(long long int a, long long int b)
{
    return (a > b)? a : b;
}

/* Helper function that allocates a
new node with the given key and
NULL left and right pointers. */
Node* newNode(long long int key)
{
    Node* node = new Node();
    node->key = key;
    node->left = nullptr;
    node->right = nullptr;
    node->howMany = 1;
    node->height = 1; // new node is initially
    // added at leaf
    return(node);
}

// A utility function to right
// rotate subtree rooted with y
// See the diagram given above.
Node *rightRotate(Node *y)
{
    Node *x = y->left;
    Node *T2 = x->right;

    // Perform rotation
    x->right = y;
    y->left = T2;

    // Update heights
    y->height = max(height(y->left),
                    height(y->right)) + 1;
    x->height = max(height(x->left),
                    height(x->right)) + 1;

    // Return new root
    return x;
}

// A utility function to left
// rotate subtree rooted with x
// See the diagram given above.
Node *leftRotate(Node *x)
{
    Node *y = x->right;
    Node *T2 = y->left;

    // Perform rotation
    y->left = x;
    x->right = T2;

    // Update heights
    x->height = max(height(x->left),
                    height(x->right)) + 1;
    y->height = max(height(y->left),
                    height(y->right)) + 1;

    // Return new root
    return y;
}

// Get Balance factor of node N
long long int getBalance(Node *N)
{
    if (N == nullptr)
        return 0;
    return height(N->left) -
           height(N->right);
}

Node* insert(Node* node, long long int key)
{
    /* 1. Perform the normal BST rotation */
    if (node == nullptr)
        return(newNode(key));

    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);
    else if (key == node->key)
    {
        node->howMany++;
        return node;
    }
    else // Equal keys not allowed
        return node;

    /* 2. Update height of this ancestor node */
    node->height = 1 + max(height(node->left),
                           height(node->right));

    /* 3. Get the balance factor of this
        ancestor node to check whether
        this node became unbalanced */
    long long int balance = getBalance(node);

    // If this node becomes unbalanced,
    // then there are 4 cases

    // Left Left Case
    if (balance > 1 && key < node->left->key)
        return rightRotate(node);

    // Right Right Case
    if (balance < -1 && key > node->right->key)
        return leftRotate(node);

    // Left Right Case
    if (balance > 1 && key > node->left->key)
    {
        node->left = leftRotate(node->left);
        return rightRotate(node);
    }

    // Right Left Case
    if (balance < -1 && key < node->right->key)
    {
        node->right = rightRotate(node->right);
        return leftRotate(node);
    }

    /* return the (unchanged) node pointer */
    return node;
}

/* Given a non-empty binary search tree,
return the node with minimum key value
found in that tree. Note that the entire
tree does not need to be searched. */
Node * minValueNode(Node* node)
{
    Node* current = node;

    /* loop down to find the leftmost leaf */
    while (current->left != nullptr)
        current = current->left;

    return current;
}

Node* findMaxSzprotkaToEat(Node* root, long long int key)
{
    if(root == nullptr)
        return nullptr;
    if(root->key == key)
        return root;

    if(root->key > key)
        return root->left == nullptr ? root : findMaxSzprotkaToEat(root->left, key);
    if(root->right == nullptr)
        return root;
    Node* maxSzprotka = findMaxSzprotkaToEat(root->right, key);
    if(maxSzprotka->key > key)
        return root;
    return maxSzprotka;
    return root->right == nullptr ? root : findMaxSzprotkaToEat(root->right, key);
}


// Recursive function to delete a node
// with given key from subtree with
// given root. It returns root of the
// modified subtree.
Node* deleteNode1(Node* root, long long int key)
{

    // STEP 1: PERFORM STANDARD BST DELETE
    if (root == nullptr)
        return root;

    // If the key to be deleted is smaller
    // than the root's key, then it lies
    // in left subtree
    if ( key < root->key )
        root->left = deleteNode1(root->left, key);

        // If the key to be deleted is greater
        // than the root's key, then it lies
        // in right subtree
    else if( key > root->key ) {
        root->right = deleteNode1(root->right, key);
    }

//         if key is same as root's key, then
//         This is the node to be deleted

//
//    if(key == root->key and root->howMany > 1)
//    {
//        root->howMany --;
//        return root;
//    }
    else
    {
        // node with only one child or no child
        if( (root->left == nullptr) ||
            (root->right == nullptr) )
        {
            Node *temp = root->left ?
                         root->left :
                         root->right;

            // No child case
            if (temp == nullptr)
            {
                temp = root;
                root = nullptr;
            }
            else // One child case
                *root = *temp; // Copy the contents of
            // the non-empty child
            free(temp);
        }
        else
        {
            // node with two children: Get the inorder
            // successor (smallest in the right subtree)
            Node* temp = minValueNode(root->right);

            // Copy the inorder successor's
            // data to this node
            root->key = temp->key;

            // Delete the inorder successor
            root->right = deleteNode1(root->right,
                                      temp->key);
        }
    }

    // If the tree had only one node
    // then return
    if (root == nullptr)
        return root;

    // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE
    root->height = 1 + max(height(root->left),
                           height(root->right));

    // STEP 3: GET THE BALANCE FACTOR OF
    // THIS NODE (to check whether this
    // node became unbalanced)
    long long int balance = getBalance(root);

    // If this node becomes unbalanced,
    // then there are 4 cases

    // Left Left Case
    if (balance > 1 &&
        getBalance(root->left) >= 0)
        return rightRotate(root);

    // Left Right Case
    if (balance > 1 &&
        getBalance(root->left) < 0)
    {
        root->left = leftRotate(root->left);
        return rightRotate(root);
    }

    // Right Right Case
    if (balance < -1 &&
        getBalance(root->right) <= 0)
        return leftRotate(root);

    // Right Left Case
    if (balance < -1 &&
        getBalance(root->right) > 0)
    {
        root->right = rightRotate(root->right);
        return leftRotate(root);
    }

    return root;
}

Node* deleteNode(Node* root, long long int key)
{
    Node* toDelete = findMaxSzprotkaToEat(root, key);
    if(toDelete->howMany > 1) {
        toDelete->howMany--;
        return root;
    }
    else
        return deleteNode1(root, key);

}



// A utility function to print preorder
// traversal of the tree.
// The function also prints height
// of every node


void preOrder(Node *root)
{
    if(root != nullptr)
    {
        cout << root->key << " x "<< root->howMany << endl;
        preOrder(root->left);
        preOrder(root->right);
    }
}


void szczupakAttack(Node* &root)
{
    long long int szczupakWeight, szczupakGoal;
    cin>> szczupakWeight >> szczupakGoal;
    long long int howManySzprotekHeAte = 0;
    vector<long long int> eatenSzprotki;
    while(szczupakWeight < szczupakGoal)
    {
        Node* nextSzprotka = findMaxSzprotkaToEat(root, szczupakWeight - 1);
        if(nextSzprotka == nullptr or nextSzprotka->key >= szczupakWeight)
        {
            cout<<"-1"<<endl;
            for(long long i : eatenSzprotki)
            {
                root = insert(root, i);
            }
            return;
        }
        szczupakWeight += nextSzprotka->key;
        if(DEBUG)cout<<"found szprotka: "<<nextSzprotka->key<<" "<< szczupakWeight<<endl;
        eatenSzprotki.push_back(nextSzprotka->key);
        root = deleteNode(root, nextSzprotka->key);
        if(DEBUG)preOrder(root);
        howManySzprotekHeAte++;
    }
    cout<< howManySzprotekHeAte<<endl;
    for(long long i : eatenSzprotki)
    {
        root = insert(root, i);
    }
}

void addSzprotka(Node* &root)
{
    long long int weight;
    cin>> weight;
    root = insert(root, weight);
}

void removeSzprotka(Node* &root)
{
    long long int weight;
    cin>> weight;
    root = deleteNode(root, weight);
}

int main() {
    ios_base::sync_with_stdio(false);
    int howManySzprotek;
    int numberOfEvents;
    cin >>howManySzprotek;

    Node* root = nullptr;
    long long int weighOfOneSzprotka;
    for(int i = 0; i < howManySzprotek; i++)
    {
        cin>> weighOfOneSzprotka;
        root = insert(root, weighOfOneSzprotka);
    }

    cin>>numberOfEvents;
    int whatEvent;
    for(int i = 0; i < numberOfEvents; i++)
    {
        if(DEBUG)preOrder(root);
        cin>> whatEvent;
        if(whatEvent == 1)
            szczupakAttack(root);
        else if (whatEvent == 2)
            addSzprotka(root);
        else
            removeSzprotka(root);
//        cout<<"number of current event: "<<i<<endl;
    }

    return 0;
}