Pagini recente » Cod sursa (job #1813976) | Cod sursa (job #1503507) | Cod sursa (job #190867) | Cod sursa (job #1651067) | Cod sursa (job #933451)
Cod sursa(job #933451)
#include<fstream>
using namespace std;
#define nmax 200002
ifstream f("heapuri.in");
ofstream g("heapuri.out");
long long P[nmax], H[nmax], N;
int leftSon(int pos) {
return 2 * pos + 1;
}
int rightSon(int pos) {
return 2 * (pos + 1);
}
int father(int pos) {
return (pos - 1) / 2;
}
void swap(int i, int j) {
int aux = H[j];
H[j] = H[i];
H[i] = aux;
aux = P[i];
P[i] = P[j];
P[j] = aux;
}
void percolate(int pos) {
while(H[pos] < H[father(pos)]) {
swap(pos, father(pos));
pos = father(pos);
if(pos == 0)
break;
}
}
void addElement(long long e) {
H[N] = e; P[N] = N;
N++;
percolate(N - 1);
}
int posOfMinimSon(int pos) {
if(H[leftSon(pos)] < H[rightSon(pos)])
return leftSon(pos);
return rightSon(pos);
}
void sift(int pos) {
while(posOfMinimSon(pos) < N) {
if(H[pos] < H[posOfMinimSon(pos)])
break;
swap(pos, posOfMinimSon(pos));
pos = posOfMinimSon(pos);
}
}
void removeElement(int indecs) {
int pos = P[indecs];
swap(pos, N - 1);
N--;
sift(pos);
}
void printMinim() {
g<<H[0]<<' ';
}
int main() {
int M, opp;
long long x;
f>>M;
for(;M;--M) {
f>>opp;
if(opp == 1) {
f>>x;
addElement(x);
}
else if(opp == 2) {
f>>x;
removeElement(x);
}
else {
f>>x;
printMinim();
}
}
return 0;
}