Cod sursa(job #2897351)

Utilizator BluThund3rRadu George BluThund3r Data 3 mai 2022 15:14:35
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.19 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");

const int N = 200005;
int val[N], heap[N], pos[N], n, op, arg, nh, t; //nh - number of nodes in the heap, t - the time of the last added item

void moveUp(int poz) {
    while(poz / 2 && val[heap[poz / 2]] > val[heap[poz]]) {
        swap(heap[poz / 2], heap[poz]);
        swap(pos[heap[poz]], pos[heap[poz / 2]]);
        poz /= 2;
    }
}

void moveDown(int poz) {
   int child_pos = 0;
   while(poz != child_pos) {
       child_pos = poz;

       if(child_pos * 2 <= nh && val[heap[child_pos * 2]] < val[heap[poz]])
           poz = child_pos * 2;
       if(child_pos * 2 + 1 <= nh && val[heap[child_pos * 2 + 1]] < val[heap[poz]])
           poz = child_pos * 2 + 1;

       swap(heap[poz], heap[child_pos]);
       pos[heap[poz]] = poz;
       pos[heap[child_pos]] = child_pos;
   }

}

void push(int x) {
    val[++ t] = x;
    heap[++ nh] = t;
    pos[t] = nh;
    moveUp(nh);
}

void pop(int time) {
    val[time] = -1;
    moveUp(pos[time]);

    pos[heap[1]] = 0;
    heap[1] = heap[nh --];
    pos[heap[1]] = 1;
    if(nh > 1)
        moveDown(1);
}


//void test(){
//    ifstream in1("grader_test5.ok");
//    ifstream in2("heapuri.out");
//    int line = 0;
//    int x, y;
//    while(in1 >> x){
//        in2 >> y;
//        ++ line;
//        if(x != y){
//            cout << "NU " << line << endl;
//        }
//    }
//    cout << "\nDA";
//}

int main() {
    in >> n;
    while(n --) {
        in >> op;

        if(op == 1) {
            in >> arg;
            push(arg);
        }

        else if(op == 2) {
            in >> arg;
            pop(arg);
        }

        else if(op == 3){
            out << val[heap[1]] << '\n';
        }
    }

    out.close();
//    test();

    return 0;
}


//#include <stdio.h>
//#include <assert.h>
//
//#define maxn 200010
//
//int N, L, NR;
//int A[maxn], Heap[maxn], Pos[maxn];
//
//void push(int x)
//{
//	int aux;
//
//	while (x/2 && A[Heap[x]]<A[Heap[x/2]])
//	{
//		aux = Heap[x];
//		Heap[x] = Heap[x/2];
//		Heap[x/2] = aux;
//
//		Pos[Heap[x]] = x;
//		Pos[Heap[x/2]] = x/2;
//		x /= 2;
//	}
//}
//
//void pop(int x)
//{
//	int aux, y = 0;
//
//	while (x != y)
//	{
//		y = x;
//
//		if (y*2<=L && A[Heap[x]]>A[Heap[y*2]]) x = y*2;
//		if (y*2+1<=L && A[Heap[x]]>A[Heap[y*2+1]]) x = y*2+1;
//
//		aux = Heap[x];
//		Heap[x] = Heap[y];
//		Heap[y] = aux;
//
//		Pos[Heap[x]] = x;
//		Pos[Heap[y]] = y;
//	}
//}
//
//int main()
//{
//	freopen("heapuri.in", "r", stdin);
//	freopen("heapuri.out", "w", stdout);
//
//	int i, x, cod;
//
//	scanf("%d ", &N);
//
//	assert(1<=N && N<=200000);
//
//	for (i=1; i<=N; i++)
//	{
//		scanf("%d ", &cod);
//		assert(1<=cod && cod<=3);
//		if (cod < 3)
//		{
//			scanf("%d ", &x);
//			assert(1<=x && x<=1000000000);
//		}
//
//		if (cod == 1)
//		{
//			L++, NR++;
//			A[NR] = x;
//			Heap[L] = NR;
//			Pos[NR] = L;
//
//			push(L);
//		}
//
//		if (cod == 2)
//		{
//			A[x] = -1;
//			assert(Pos[x] != 0);
//			assert(1<=x && x<=NR);
//			push(Pos[x]);
//
//			Pos[Heap[1]] = 0;
//			Heap[1] = Heap[L--];
//			Pos[Heap[1]] = 1;
//			if (L>1) pop(1);
//		}
//
//		if (cod == 3) printf("%d\n", A[Heap[1]]);
//	}
//
//	return 0;
//}