Shiniu
[Html]
Namespace MappingBinaryHeap {
/*
DS:
Datastructure to show the value
Heap:
1. Ds: value
2. idx: index
Pos:
The position for each index
Len:
The volum n of heap
Hh:
Heap
Push:
Insert an element
Pop:
Pop an element:
1. pop (pos []) pop the element index
2. pop (1) pop the 'Max' one
*/
Struct DS {
Int next;
DS (){}
DS (int x): next (x ){}
Bool operator <(const DS & A) const {
If (next =-1)
Return true;
If (A. next =-1)
Return false;
Return next> A. next;
}
Void init (){
Next = 0;
}
};
# Define maxn100005
Struct Heap {
Int idx;
DS val;
} Hh [maxn];
Int pos [maxn];
Int len;
Bool Prior (Heap a, Heap B ){
Return a. val <B. val;
}
Void Push (Heap s ){
Int I;
For (I = ++ len; I> 1 & Prior (s, hh [I/2]); I/= 2 ){
Hh [I] = hh [I/2];
Pos [hh [I]. idx] = I;
}
Hh [I] = s;
Pos [hh [I]. idx] = I;
}
Heap Pop (int idx ){
If (idx =-1)
Return hh [0];
Heap ret = hh [idx];
Heap last = hh [len --];
Int I, s;
For (I = idx; I * 2 <= len; I = s ){
S = I * 2;
If (s + 1 <= len & Prior (hh [s + 1], hh [s]) {
S ++;
}
If (Prior (hh [s], last )){
Hh [I] = hh [s];
Pos [hh [I]. idx] = I;
} Else {
Break;
}
}
Hh [I] = last;
Pos [hh [I]. idx] = I;
For (I = idx; I> 1 & Prior (hh [I], hh [I/2]); I/= 2 ){
Heap buf = hh [I];
Hh [I] = hh [I/2];
Hh [I/2] = buf;
Pos [hh [I]. idx] = I;
Pos [hh [I/2]. idx] = I/2;
}
Return ret;
}
Void init (){
Hh [0]. val. init ();
Len = 0;
}
};
Namespace MappingBinaryHeap {
/*
DS:
Datastructure to show the value
Heap:
1. Ds: value
2. idx: index
Pos:
The position for each index
Len:
The volum n of heap
Hh:
Heap
Push:
Insert an element
Pop:
Pop an element:
1. pop (pos []) pop the element index
2. pop (1) pop the 'Max' one
*/
Struct DS {
Int next;
DS (){}
DS (int x): next (x ){}
Bool operator <(const DS & A) const {
If (next =-1)
Return true;
If (A. next =-1)
Return false;
Return next> A. next;
}
Void init (){
Next = 0;
}
};
# Define maxn100005
Struct Heap {
Int idx;
DS val;
} Hh [maxn];
Int pos [maxn];
Int len;
Bool Prior (Heap a, Heap B ){
Return a. val <B. val;
}
Void Push (Heap s ){
Int I;
For (I = ++ len; I> 1 & Prior (s, hh [I/2]); I/= 2 ){
Hh [I] = hh [I/2];
Pos [hh [I]. idx] = I;
}
Hh [I] = s;
Pos [hh [I]. idx] = I;
}
Heap Pop (int idx ){
If (idx =-1)
Return hh [0];
Heap ret = hh [idx];
Heap last = hh [len --];
Int I, s;
For (I = idx; I * 2 <= len; I = s ){
S = I * 2;
If (s + 1 <= len & Prior (hh [s + 1], hh [s]) {
S ++;
}
If (Prior (hh [s], last )){
Hh [I] = hh [s];
Pos [hh [I]. idx] = I;
} Else {
Break;
}
}
Hh [I] = last;
Pos [hh [I]. idx] = I;
For (I = idx; I> 1 & Prior (hh [I], hh [I/2]); I/= 2 ){
Heap buf = hh [I];
Hh [I] = hh [I/2];
Hh [I/2] = buf;
Pos [hh [I]. idx] = I;
Pos [hh [I/2]. idx] = I/2;
}
Return ret;
}
Void init (){
Hh [0]. val. init ();
Len = 0;
}
};