Advertisement
Josif_tepe

Untitled

Jun 13th, 2024
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF = 2e9;
  4.  
  5. int add(set<pair<int, int>> & st, int x) {
  6.   auto it = st.insert(make_pair(x, 2)).first;
  7.   int res = -1;
  8.   if(prev(it)->second == 1 and next(it)->second == 3) {
  9.     res = next(it)->first;
  10.     if(next(next(it))->first == next(it)->first + 1) {
  11.       st.erase(next(next(it)));
  12.       st.erase(next(it));
  13.     }
  14.     else {
  15.       st.insert(next(next(it)), make_pair(next(it)->first + 1, 3));
  16.       st.erase(next(it));
  17.     }
  18.     st.erase(it);
  19.     return res;
  20.   }
  21.   if(prev(it)->second == 3 and next(it)->second == 1) {
  22.       res = x;
  23.       st.insert(it, make_pair(res, 1));
  24.       if(next(it)->first == x + 1) {
  25.         st.erase(next(it));
  26.       }
  27.       else {
  28.         st.insert(next(it), make_pair(x + 1, 3));
  29.       }
  30.       st.erase(it);
  31.       return res;
  32.   }
  33.  
  34. }
  35. void remove(set<pair<int, int>> &st, int x) {
  36.   auto it = st.lower_bound(make_pair(x, 2));
  37.   it = prev(it);
  38.   if(it->first == x) {
  39.     if(next(it)->first == x + 1) {
  40.       st.erase(next(it));
  41.       st.erase(it);
  42.       return;
  43.     }
  44.     else {
  45.       st.insert(next(it), make_pair(x + 1, 1));
  46.       st.erase(it);
  47.       return;
  48.     }
  49.   }
  50.   else {
  51.     st.insert(next(it), make_pair(x + 1 , 1));
  52.     st.insert(next(it), make_pair(x , 3));
  53.    
  54.   }
  55. }
  56. int main()
  57. {
  58.   ios_base::sync_with_stdio(false);
  59.   set<pair<int, int>> st;
  60.   int n;
  61.   cin >> n;
  62.  
  63.   st.insert(make_pair(0, 3));
  64.   st.insert(make_pair(INF, 1));
  65.  
  66.   for(int i = 0; i < n; i++) {
  67.     int t, code;
  68.     cin >> t >> code;
  69.    
  70.     if(t == 1) {
  71.         int res = add(st, code);
  72.         cout << res << "\n";
  73.     }
  74.     else {
  75.       remove(st, code);
  76.     }
  77.   }
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement