Cod sursa(job #1829948)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 15 decembrie 2016 21:14:29
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.81 kb
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);
#define setnow clock_t tStart=clock();
#define time (double)(clock() - tStart)/CLOCKS_PER_SEC;
#define endl '\n'
typedef long long ll;
typedef long long int lli;
typedef pair < int, int> dbl;
const int maxInt = 1e9*2;
const lli maxLong = 1e18*2;
int n;
int heap[101010];
int length;
int nbr = 0;
int op,x;
int mp[101010];
int mpx[101010];
void insert(int x){
        if(length == 0){
            heap[length++] = x;
            nbr++;
            mp[length - 1] = nbr;
            mpx[nbr] = length - 1;
            return;
        }
        heap[length++] = x;
        nbr++;
        mp[length - 1] = nbr;
        mpx[nbr] = length - 1;
        int pos = length - 1;
        bool minHeap = false;
        while(pos > 0 && !minHeap){
                if(heap[pos] < heap[(pos - 1)/ 2 ]){
                    swap(mp[pos], mp[(pos - 1) / 2]);
                    swap(heap[pos], heap[(pos - 1) / 2]);
                    int one = mp[(pos - 1) / 2];
                    int two = mp[pos];
                    swap(mpx[one],mpx[two]);
                    pos = (pos - 1) / 2;
                } else
                    minHeap = true;
        }
}

void erase(int x){
        swap(heap[length - 1],heap[x]);
        swap(mp[length - 1],mp[x]);
        int one = mp[length - 1];
        int two = mp[x];
        swap(mpx[one],mpx[two]);
        length--;
        int pos = x;
        if(pos > 0 && heap[pos] < heap[(pos - 1) / 2]){
            bool minHeap = false;
            while(pos > 0 && !minHeap){
                if(heap[pos] < heap[(pos - 1)/ 2 ]){
                    swap(mp[pos], mp[(pos - 1) / 2]);
                    swap(heap[pos], heap[(pos - 1) / 2]);
                    int one = mp[(pos - 1) / 2];
                    int two = mp[pos];
                    swap(mpx[one],mpx[two]);
                    pos = (pos - 1) / 2;
                } else
                    minHeap = true;
        }
        } else
        if((heap[pos] > heap[pos * 2 + 1] && pos * 2 + 1 < length) || (heap[pos] > heap[pos * 2 + 2] && pos * 2 + 2 < length)){
            bool minHeap = false;
            while(2 * pos + 1 < length && !minHeap){
                    if(pos * 2 + 2 < length && heap[pos] > heap[pos * 2 + 1] && heap[pos] > heap[pos * 2 + 2]){
                                if(heap[pos * 2 + 1] < heap[pos * 2 + 2]){
                                        swap(heap[pos], heap[pos * 2 + 1]);
                                        swap(mp[pos], mp[pos * 2 + 1]);
                                        int one = mp[pos * 2 + 1];
                                        int two = mp[pos];
                                        swap(mpx[one],mpx[two]);
                                        pos = pos * 2 + 1;
                                } else {
                                        swap(mp[pos], mp[pos * 2 + 2]);
                                        swap(heap[pos], heap[pos * 2 + 2]);
                                        int one = mp[pos * 2 + 2];
                                        int two = mp[pos];
                                        swap(mpx[one],mpx[two]);
                                        pos = pos * 2 + 2;
                                }
                    } else
                    if(heap[pos] > heap[pos * 2 + 1]){
                            swap(mp[pos], mp[pos * 2 + 1]);
                            swap(heap[pos], heap[pos * 2 + 1]);
                            int one = mp[pos * 2 + 1];
                            int two = mp[pos];
                            swap(mpx[one],mpx[two]);
                            pos = pos * 2 + 1;

                    } else
                    if(pos * 2 + 2 < length && heap[pos] > heap[pos * 2 + 2]){
                            swap(mp[pos], mp[pos * 2 + 2]);
                            swap(heap[pos], heap[pos * 2 + 2]);
                            int one = mp[pos * 2 + 2];
                            int two = mp[pos];
                            swap(mpx[one],mpx[two]);
                            pos = pos * 2 + 2;
                    } else minHeap = true;
            }
        }
}
int main(){
           ios;
           // setnow;
           ifstream cin("heapuri.in");
           ofstream cout("heapuri.out");
           cin >> n;
           for(int i = 0; i < n; i++){
                cin >> op;
                if(op == 1){
                        cin >> x;
                        insert(x);
                }
                 else
                if(op == 2){
                        cin >> x;
                        erase(mpx[x]);
                } else
                if(op == 3)
                        cout <<heap[0] << endl;

           }
    return 0;
}