Cod sursa(job #744727)
Utilizator | Data | 9 mai 2012 15:46:47 | |
---|---|---|---|
Problema | Heapuri | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.48 kb |
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct ce{
int v,p;
};
vector<ce> h(1);
int u=0;
vector<int> p(1);
void de(int k);
void add(int k);
int main(){
ifstream cinr ("heapuri.in");
ofstream cour ("heapuri.out");
h[0].v=-1;
int n,a,b,x;
cinr >> n;
for(int i=1; i<=n; i++){
cinr >> b;
if( b==1 ){
u++;
cinr >> a;
add(a);
}
if( b==2 ){
cinr >> x;
p[h[h.size()-1].p]=h[p[x]].p;
h[p[x]]=h[h.size()-1];
h.pop_back();
de(x);
}
if( b==3 ){
cour << h[1].v << "\n";
}
}
//cin.ignore(2);
return 0;
}
void de(int k){
ce w;
int y,t,i=k;
while(2*i+1<h.size()){
y=2*i;
if(h[y].v>h[y+1].v){ y++; }
if(h[i].v>h[y].v){
w=h[i];
h[i]=h[y];
h[y]=w;
t=p[h[i].p];
p[h[i].p]=p[h[y].p];
p[h[y].p]=t;
i=y;
}
else { return; }
}
if(2*i<h.size()){
if(h[i].v>h[2*i].v){
y=2*i;
w=h[i];
h[i]=h[y];
h[y]=w;
t=p[h[i].p];
p[h[i].p]=p[h[y].p];
p[h[y].p]=t;
}
}
}
void add(int k){
ce q,w1;
q.p=u;
q.v=k;
h.push_back(q);
int w,i=h.size()-1;
p.push_back(i);
while(h[i].v<h[i/2].v){
w1=h[i];
h[i]=h[i/2];
h[i/2]=w1;
w=p[h[i].p];
p[h[i].p]=p[h[i/2].p];
p[h[i/2].p]=w;
i=i/2;
}
}