Pagini recente » Cod sursa (job #714343) | Cod sursa (job #2439271) | Cod sursa (job #1151141) | Cod sursa (job #1885085) | Cod sursa (job #501650)
Cod sursa(job #501650)
#include<cstdio>
#include<algorithm>
using namespace std;
const int Nmax = 200005;
int H[Nmax], poz[Nmax], A[Nmax], N, NR;
int father(int nod) {
return nod/2;
}
int left_son(int nod) {
return nod*2;
}
int right_son(int nod) {
return nod*2+1;
}
void percolate(int K) {
while(K>1 && H[K]<H[father(K)]){
swap(H[K],H[father(K)]);
swap(A[K],A[father(K)]);
poz[A[K]]=K;
poz[A[father(K)]]=father(K);
K=father(K);
}
}
void sift(int K) {
int son=K;
if(left_son(K)<=N && H[left_son(K)]<H[son])
son=left_son(K);
if(right_son(K)<=N && H[right_son(K)]<H[son])
son=right_son(K);
if(son!=K) {
swap(H[K],H[son]);
swap(A[K],A[son]);
poz[A[K]]=K;
poz[A[son]]=son;
sift(son);
}
}
void insert(int K) {
NR++; N++;
H[N]=K;
A[N]=NR;
poz[NR]=N;
percolate(N);
}
void cut(int K){
K=poz[K];
H[K]=H[N];
N--;
if(K>1 && H[K]<H[father(K)])
percolate(K);
else
sift(K);
}
int main() {
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int M, tip, val;
for(scanf("%d",&M); M; --M) {
scanf("%d",&tip);
switch(tip) {
case 1:
scanf("%d",&val);
insert(val);
break;
case 2:
scanf("%d",&val);
cut(val);
break;
default:
printf("%d\n",H[1]);
break;
}
}
return 0;
}