Cod sursa(job #1220611)

Utilizator ptquake10ptquake10 ptquake10 Data 17 august 2014 21:39:55
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <deque>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <map>
using namespace std;
#define MAX 1000001

int n, k, pos[200001];
struct nod{
	int p, v;
	nod() {
		p = 0;
		v = 0;
	}
	nod(int v, int p) {
		this->p=p;
		this->v=v;
	}
};

bool operator<(nod a, nod b) {
	return a.v < b.v;
}

void swap(nod &a, nod &b) {
	swap(pos[a.p], pos[b.p]);
	swap(a.p, b.p);
	swap(a.v, b.v);
}

nod h[400010];

void add(nod a) {
	int t, f;
	k++;
	h[k] = a;
	pos[a.p] = k;
	f = k; t = f/2;
	while (t > 0 && h[f] < h[t]) {
		swap(h[t], h[f]);
		f = t;
		t = f / 2;
	}
}

void rem(nod a) {
	int t, f;
	t = pos[a.p];
	f = 2 * t;
	swap(h[pos[a.p]], h[k--]);
	if (f + 1 <= k && h[f+1] < h[f]) f++;
	while (f <= k && h[f] < h[t]) {
		swap(h[t], h[f]);
		t = f;
		f = 2 * t;
		if (f + 1 <= k && h[f+1] < h[f]) f++;
	}
}

int main() {
	int op, x;
	
	freopen("heapuri.in","r",stdin);
	freopen("heapuri.out","w",stdout);
	
	int p = 0;
	
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		//for (int j = 1; j <= k; j++) printf("%d ", h[j].v);
		//printf("\n");
		scanf("%d", &op);
		switch(op) {
			case 1:
				scanf("%d", &x);
				add(nod(x, ++p));	// inserez nodul al p-lea
				break;
			case 2:
				scanf("%d", &x);
				rem(h[pos[x]]);	// sterg nodul intrat al x-lea
				break;
			case 3:
				printf("%d\n", h[1].v);
				break;
		}
	}
		
	return 0;
}