Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. long long max_s, now_s = 0;
  6. vector<long long> a;
  7.  
  8. void Sift_Down(long long i, bool j = true){
  9.   int l = 2 * i + 1, r = 2 * i + 2, max = i;
  10.   if(l < now_s && a[l] > a[max]) max = l;
  11.   if(r < now_s && a[r] > a[max]) max = r;
  12.   if(max != i){
  13.     long long temp = a[i];
  14.     a[i] = a[max];
  15.     a[max] = temp;
  16.     Sift_Down(max);
  17.   }
  18.   else if(j) cout << i + 1 << " ";
  19. }
  20.  
  21. void Sift_Up(long long i, bool j = true){
  22.    long long pred, min = i;
  23.   if (i % 2 == 0) pred = (i - 2) / 2;
  24.   else pred = i / 2;
  25.   if(pred >= 0 && a[min] > a[pred]) min = pred;
  26.   if(min != i){
  27.       long long t = a[i];
  28.       a[i] = a[min];
  29.       a[min] = t;
  30.       Sift_Up(min);
  31.   }
  32.   else if(j) cout << i + 1 << endl;
  33. }
  34.  
  35. void Extract_Max(){
  36.   if (now_s != 0){
  37.     long long t = a[0];
  38.     a[0] = a[now_s - 1];
  39.     a.pop_back();
  40.     now_s--;
  41.     if (now_s != 0) Sift_Down(0);
  42.     else cout << 0 << " ";
  43.     cout << t << endl;
  44.   }
  45.   else cout << -1 << endl;
  46. }
  47.  
  48. void Delete(long long i){
  49.   if (now_s > i) {
  50.     long long t = a[i];
  51.     a[i] = a[now_s - 1];
  52.     a.pop_back();
  53.     now_s--;
  54.     if (now_s != 0) {
  55.       Sift_Down(i, false);
  56.       Sift_Up(i, false);
  57.     }
  58.     cout << t << endl;
  59.   }
  60.   else cout << -1 << endl;
  61. }
  62.  
  63. void Insert(long long q){
  64.   if (now_s < max_s){
  65.     a.push_back(q);
  66.     Sift_Up(now_s);
  67.     now_s++;
  68.   }
  69.   else cout << -1 << endl;
  70. }
  71.  
  72.  
  73. int main(int argc, char const *argv[]) {
  74.   cin >> max_s;
  75.   int m;
  76.   cin >> m;
  77.  
  78.   for (size_t i = 0; i < m; i++) {
  79.     long long a1, a2;
  80.     cin >> a1;
  81.     if (a1 == 1) Extract_Max();
  82.     else if(a1 == 2){
  83.       cin >> a2;
  84.       Insert(a2);
  85.     }
  86.     else{
  87.       cin >> a2;
  88.       Delete(a2 - 1);
  89.     }
  90.   }
  91.     for (size_t i = 0; i < now_s; i++) {
  92.       cout << a[i] << " ";
  93.     }
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement