Pagini recente » Cod sursa (job #2968625) | Cod sursa (job #1374640) | Cod sursa (job #3129008) | Cod sursa (job #2007907) | Cod sursa (job #1952495)
#include <bits/stdc++.h>
#define NMAX 200005
#define BMAX 10005
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
char buffer[BMAX];
int H[NMAX],P[NMAX],V[NMAX],N,NR,e = BMAX - 1;
void pars(int &x){
while(!isdigit(buffer[e])){
e++;
if(e == BMAX){
fin.read(buffer,BMAX);
e = 0;
}
}
x = 0;
while(isdigit(buffer[e])){
x = x * 10 + (buffer[e] - '0');
e++;
if(e == BMAX){
fin.read(buffer,BMAX);
e = 0;
}
}
}
void UpHeap(int k){
if( k > 1 && V[H[k]] < V[H[k >> 1]]){
swap(H[k],H[k >> 1]);
P[H[k]] = k;
P[H[k >> 1]] = k >> 1;
UpHeap(k >> 1);
}
}
void DownHeap(int k){
int son = k << 1;;
if(V[H[k]] > V[H[son]] && son <= N){
swap(H[k],H[son]);
P[H[k]] = k;
P[H[son]] = son;
DownHeap(son);
} else {
son = (k << 1) + 1;
if(V[H[k]] > V[H[son]] && son <= N){
swap(H[k],H[son]);
P[H[k]] = k;
P[H[son]] = son;
DownHeap(son);
}
}
}
int main()
{
ios :: sync_with_stdio(false);
int n,t,x;
// fin >> n;
pars(n);
while(n--){
// fin >> t;
pars(t);
if(t == 1){
pars(x);
// fin >> x;
V[++NR] = x;
H[++N] = NR;
P[NR] = N;
UpHeap(N);
}
if(t == 2){
pars(x);
// fin >> x;
int p = P[x];
P[H[N]] = p;
swap(H[p],H[N]);
N--;
UpHeap(p);
DownHeap(p);
}
if(t == 3)
fout << V[H[1]] << "\n";
}
return 0;
}