Pagini recente » Cod sursa (job #1323944) | Cod sursa (job #1693646) | Clasament allyoucancode2008 | Cod sursa (job #1522253) | Cod sursa (job #781012)
Cod sursa(job #781012)
#include<fstream>
#define lim 200002
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int op,V[lim],i,x,Pos[lim],H[3*lim],N,n;
inline int father(int nod){
return nod/2;
}
inline int rs(int nod){
return 2*nod+1;
}
inline int ls(int nod){
return 2*nod;
}
void percolate(int k){
while(k>1 && V[H[k]]<V[H[k/2]]){
swap(Pos[H[k]],Pos[H[k/2]]);
swap(H[k],H[k/2]);
k/=2;
}
}
void sift(int nod){
int son=nod;
if(V[H[ls(nod)]]<V[H[son]] &&ls(nod)<=N)
son=ls(nod);
if(rs(nod)<=N && V[H[rs(nod)]]<V[H[son]])
son=rs(nod);
if(son!=nod){
swap(Pos[H[son]],Pos[H[nod]]);
swap(H[nod],H[son]);
sift(son);
}
}
void cut(int x){
swap(Pos[H[x]],Pos[H[N]]);
swap(H[x],H[N]);
--N;
sift(x);
}
void add(int x){
H[++N]=x;
Pos[x]=N;
percolate(Pos[x]);
}
int main (){
f>>n;
N=0;
i=0;
for(;n;n--){
f>>op;
if(op!=3){
if(op==1){
f>>V[++i];
add(i);
}
else{
f>>x;
cut(Pos[x]);
}
}
else{
g<<V[H[1]]<<"\n";
}
}
return 0;
}