Alex_tz307

Manual Heap 2

Sep 11th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxn 200010
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin ("heapuri.in");
  7. ofstream fout ("heapuri.out");
  8.  
  9. int N, L, NR, A[maxn], heap[maxn], pos[maxn];
  10.  
  11. inline void Push (int x) {
  12.     while (x / 2 && A[heap[x]] < A[heap[x / 2]]) {
  13.         swap (heap[x], heap[x / 2]);
  14.         pos[heap[x]] = x;
  15.         pos[heap[x / 2]] = x / 2;
  16.         x /= 2;
  17.     }
  18. }
  19.  
  20. inline void Pop (int x) {
  21.     int y = 0;
  22.     while (x != y) {
  23.         y = x;
  24.         if (y * 2 <= L && A[heap[x]] > A[heap[y * 2]]) x = y * 2;
  25.         if (y * 2 + 1 <= L && A[heap[x]] > A[heap[y * 2 + 1]]) x = y * 2 + 1;
  26.         swap (heap[x], heap[y]);
  27.         pos[heap[x]] = x;
  28.         pos[heap[y]] = y;
  29.     }
  30. }
  31.  
  32. inline void Read_Solve () {
  33.     int i, x, op;
  34.     fin >> N;
  35.     for (i = 1; i <= N; ++i) {
  36.         fin >> op;
  37.         if (op < 3) fin >> x;
  38.         if (op == 1) {
  39.             L ++, NR ++;
  40.             A[NR] = x;
  41.             heap[L] = NR;
  42.             pos[NR] = L;
  43.             Push (L);
  44.         }
  45.         if (op == 2) {
  46.             A[x] = -1;
  47.             Push (pos[x]);
  48.             pos[heap[1]] = 0;
  49.             heap[1] = heap[L --];
  50.             pos[heap[1]] = 1;
  51.             if (L > 1) Pop (1);
  52.         }
  53.         if (op == 3) fout << A[heap[1]] << '\n';
  54.     }
  55. }
  56.  
  57. inline void Close () {
  58.     fin.close ();
  59.     fout.close ();
  60. }
  61.  
  62. int main() {
  63.     Read_Solve ();
  64.     Close ();
  65.     return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment