Pagini recente » Cod sursa (job #1193938) | Cod sursa (job #2103660) | Cod sursa (job #3148362) | Cod sursa (job #471393) | Cod sursa (job #677399)
Cod sursa(job #677399)
#include <stdio.h>
#include <string.h>
#define maxN 200001
#define maxB 524289
#define lineR 262144
int h[maxB];
int v[maxN];
int pos[maxN];
bool ok=true;
char line[lineR];
int ind=0;
inline int read()
{
if(ok)
{
ok=false;
fread(line,1,lineR,stdin);
ind=0;
}
int ret=0;
for(;'0'<=line[ind]&&line[ind]<='9';)
{
ret*=10;
ret+=line[ind]-'0';
if(++ind==lineR)
{
ind=0;
fread(line,1,lineR,stdin);
}
}
for(;'0'>line[ind]||line[ind]>'9';)
if(++ind==lineR)
{
ind=0;
fread(line,1,lineR,stdin);
}
return ret;
}
inline void heapup(int nod)
{
int aux=h[nod];
for(;nod>1&&h[nod]<h[nod>>1];nod>>=1)
{
h[nod]=h[nod>>1];
pos[h[nod]]=nod;
}
h[nod]=aux;
pos[aux]=nod;
}
inline void heapdown(int nod)
{
int L,R,aux=h[nod];
while(1)
{
L=(nod<<1);
R=L+1;
if(R<=h[0]&&h[R]<h[L]&&h[R]<h[nod])
{
h[nod]=h[R];
pos[h[nod]]=nod;
nod=R;
}
else
if(L<=h[0]&&h[L]<h[nod])
{
h[nod]=h[L];
pos[h[nod]]=nod;
nod=L;
}
else
{
h[nod]=aux;
pos[aux]=nod;
return;
}
}
}
inline void pushheap(int nod)
{
h[++h[0]]=nod;
pos[nod]=h[0];
heapup(h[0]);
}
inline void popheap(int nod)
{
pos[h[nod]]=0;
h[nod]=h[h[0]];
pos[h[nod]]=nod;
--h[0];
if(nod>1&&h[nod>>1]>h[nod]) heapup(nod);
else heapdown(nod);
}
int main()
{
int N,cod,x;
freopen("heapuri.in","r",stdin);
N=read();
freopen("heapuri.out","w",stdout);
while(N--)
{
cod=read();
if(cod==3)
{
printf("%d\n",h[1]);
continue;
}
x=read();
if(cod&1)
{
v[++v[0]]=x;
pushheap(x);
continue;
}
popheap(pos[v[x]]);
}
return 0;
}