CCCPaste

Untitled

#include 
using namespace std;
const int INF = 2e9;

int add(set> & st, int x) {
  auto it = st.insert(make_pair(x, 2)).first;
  int res = -1;
  if(prev(it)->second == 1 and next(it)->second == 3) {
    res = next(it)->first;
    if(next(next(it))->first == next(it)->first + 1) {
      st.erase(next(next(it)));
      st.erase(next(it));
    }
    else {
      st.insert(next(next(it)), make_pair(next(it)->first + 1, 3));
      st.erase(next(it));
    }
    st.erase(it);
    return res;
  }
  if(prev(it)->second == 3 and next(it)->second == 1) {
      res = x;
      st.insert(it, make_pair(res, 1));
      if(next(it)->first == x + 1) {
        st.erase(next(it));
      }
      else {
        st.insert(next(it), make_pair(x + 1, 3));
      }
      st.erase(it);
      return res;
  }
  
}
void remove(set> &st, int x) {
  auto it = st.lower_bound(make_pair(x, 2));
  it = prev(it);
  if(it->first == x) {
    if(next(it)->first == x + 1) {
      st.erase(next(it));
      st.erase(it);
      return;
    }
    else {
      st.insert(next(it), make_pair(x + 1, 1));
      st.erase(it);
      return;
    }
  }
  else {
    st.insert(next(it), make_pair(x + 1 , 1));
    st.insert(next(it), make_pair(x , 3));
    
  }
}
int main()
{
  ios_base::sync_with_stdio(false);
  set> st;
  int n;
  cin >> n;
  
  st.insert(make_pair(0, 3));
  st.insert(make_pair(INF, 1));

  for(int i = 0; i < n; i++) {
    int t, code;
    cin >> t >> code;
    
    if(t == 1) {
        int res = add(st, code);
        cout << res << "\n";
    }
    else {
      remove(st, code);
    }
  }
  
    return 0;
}