Problem p01270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)
#define int ll
struct st {
  int first, second, ident;
  bool operator<(st obj) const { return this->second < obj.second; }
};

signed main() {
  int n;
  while (cin >> n && n) {
    set<pii> free;
    set<st> used;
    free.insert(pii(0, 1e9 + 1));
    rep(i, n) {
      char ch;
      cin >> ch;
      if (ch == 'W') {
        int num, len;
        cin >> num >> len;
        while (len > 0) {
          pii tmp = *(free.begin());
          free.erase(free.begin());
          if (tmp.second - tmp.first <= len) { //????????????
            len -= tmp.second - tmp.first;
            used.insert(st{tmp.first, tmp.second, num});
          } else { //????????????????????????
            used.insert(st{tmp.first, tmp.first + len, num});
            free.insert(pii(tmp.first + len, tmp.second));
            len = 0;
          }
        }
      }
      if (ch == 'D') {
        int num;
        cin >> num;
        auto itr = used.begin();
        while (itr != used.end()) {
          if (itr->ident == num) {
            st tmp = *itr;
            free.insert(pii(tmp.first, tmp.second)); //??????????£?
            used.erase(itr++);
          } else {
            itr++;
          }
        }
      }
      if (ch == 'R') {
        int pos;
        cin >> pos;
        auto res = used.upper_bound(st{0, pos, 0});
        if (res == used.end())
          cout << "-1" << endl;
        else {
          if (res->first > pos)
            cout << "-1" << endl;
          else {
            st tmp = *(res);
            cout << tmp.ident << endl;
          }
        }
      }
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

struct st {
  int first, second, ident;
  bool operator<(st obj) const { return this->second < obj.second; }
};

int main() {
  int n;
  bool f = false;
  while (cin >> n && n) {
    if (f)
      cout << endl;
    f = true;
    set<pii> free;
    set<st> used;
    free.insert(pii(0, 1e9 + 1));
    rep(i, n) {
      char ch;
      cin >> ch;
      if (ch == 'W') {
        int num, len;
        cin >> num >> len;
        while (len > 0) {
          pii tmp = *(free.begin());
          free.erase(free.begin());
          if (tmp.second - tmp.first <= len) { //????????????
            len -= tmp.second - tmp.first;
            used.insert(st{tmp.first, tmp.second, num});
          } else { //????????????????????????
            used.insert(st{tmp.first, tmp.first + len, num});
            free.insert(pii(tmp.first + len, tmp.second));
            len = 0;
          }
        }
      }
      if (ch == 'D') {
        int num;
        cin >> num;
        for (auto itr = used.begin(); itr != used.end(); itr++) {
          if (itr->ident == num) {
            st tmp = *itr;
            free.insert(pii(tmp.first, tmp.second)); //??????????£?
            itr = used.erase(itr);
          }
        }
      }
      if (ch == 'R') {
        int pos;
        cin >> pos;
        auto res = used.upper_bound(st{0, pos, 0});
        if (res == used.end())
          cout << "-1" << endl;
        else {
          st tmp = *(res);
          cout << tmp.ident << endl;
        }
      }
    }
  }
}