Pagini recente » Cod sursa (job #1643920) | Cod sursa (job #2864607) | Cod sursa (job #1954326) | Cod sursa (job #2137038) | Cod sursa (job #696311)
Cod sursa(job #696311)
#include <fstream>
using namespace std;
int val[200010],h[200010],l[200010],n,m,nr;
int maxx(int a,int b)
{
if(val[a]>val[b])
return a;
return b;
}
void sift(int i)
{
if(val[h[i]]>val[h[i*2]]&&h[i*2])
{
if(val[h[i]]>val[h[i*2+1]]&&h[i*2+1])
{
int s=maxx(h[i*2],h[i*2+1]);
swap(h[i],h[s]);
swap(l[h[i]],l[h[s]]);
sift(s);
}
else
{
swap(h[i],h[i*2]);
swap(l[h[i]],l[h[i*2]]);
sift(i*2);
}
}
else
{
if(val[h[i]]>val[h[i*2+1]]&&h[i*2+1])
{
swap(h[i],h[i*2+1]);
swap(l[h[i]],l[h[i*2+1]]);
sift(i*2+1);
}
}
return;
}
void percolate(int i)
{
if(val[h[i]]<val[h[i/2]])
{
swap(h[i],h[i/2]);
swap(l[h[i]],l[h[i/2]]);
percolate(i/2);
}
}
void insert(int x)
{
val[++nr]=x;
h[++m]=nr;l[nr]=m;
if(val[h[m/2]]>val[h[m]])
{
swap(h[m/2],h[m]);
swap(l[h[m/2]],l[h[m]]);
percolate(m/2);
}
}
void delet(int x)
{
int i=l[x];
h[i]=h[m];h[m--]=0;
if(val[h[i]]<val[h[i/2]])
percolate(i);
else
sift(i);
}
int main()
{
ifstream f("heapuri.in");
f>>n;
int o;
ofstream g("heapuri.out");
for(int i=0;i<n;i++)
{
f>>o;int x;
switch(o)
{
case 1: f>>x; insert(x);break;
case 2: f>>x; delet(x);break;
case 3: g<<val[h[1]]<<'\n';
}
}
return 0;
}