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

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

unordered_map<int, PII> dp;
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;

    dp.clear();
    dp[0] = {INF, -1};

    REP(i, n) {
      char c;
      cin >> c;
      int l, r;
      if (c == 'W') {
        cin >> l >> r;
        int idx = 0, cnt = r;
        while (true) {
          // cout << idx << " " << cnt << endl;
          // if(dp.find(idx) == dp.end()) assert(false);
          if (dp[idx].second == -1) {
            if (idx + cnt - 1 <= dp[idx].first) {
              if (dp.find(idx + cnt) == dp.end())
                dp[idx + cnt] = {dp[idx].first, -1};
              dp[idx] = {idx + cnt - 1, l};
              break;
            } else {
              dp[idx].second = l;
              cnt -= dp[idx].first - idx + 1;
            }
          }
          // assert(idx < dp[idx].first + 1);
          idx = dp[idx].first + 1;
        }
      } else if (c == 'D') {
        cin >> l;
        for (auto &j : dp)
          if (j.second.second == l)
            j.second.second = -1;
      } else if (c == 'R') {
        cin >> l;
        int idx = 0;
        while (true) {
          // if(dp.find(idx) == dp.end()) assert(false);
          if (dp[idx].first >= l) {
            cout << dp[idx].second << endl;
            break;
          }
          assert(idx < dp[idx].first + 1);
          idx = dp[idx].first + 1;
        }
      }
      // for(auto& j: dp) cout << j.first << " " << j.second << " "; cout <<
      // endl;
    }
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &a) {
  out << '(' << a.first << ',' << a.second << ')';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

unordered_map<int, PII> dp;
signed main(void) {
  while (true) {
    int n;
    cin >> n;
    if (!n)
      break;

    dp.clear();
    dp[0] = {INF, -1};

    REP(i, n) {
      char c;
      cin >> c;
      int l, r;
      if (c == 'W') {
        cin >> l >> r;
        int idx = 0, cnt = r;
        while (true) {
          // cout << idx << " " << cnt << endl;
          if (dp.find(idx) == dp.end())
            assert(false);
          if (dp[idx].second == -1) {
            if (idx + cnt - 1 <= dp[idx].first) {
              if (dp.find(idx + cnt) == dp.end())
                dp[idx + cnt] = {dp[idx].first, -1};
              dp[idx] = {idx + cnt - 1, l};
              break;
            } else {
              dp[idx].second = l;
              cnt -= dp[idx].first - idx + 1;
            }
          }
          assert(idx < dp[idx].first + 1);
          idx = dp[idx].first + 1;
        }
      } else if (c == 'D') {
        cin >> l;
        for (auto &j : dp)
          if (j.second.second == l)
            j.second.second = -1;
      } else if (c == 'R') {
        cin >> l;
        int idx = 0;
        while (true) {
          if (dp.find(idx) == dp.end())
            assert(false);
          if (dp[idx].first >= l) {
            cout << dp[idx].second << endl;
            break;
          }
          assert(idx < dp[idx].first + 1);
          idx = dp[idx].first + 1;
        }
      }
      for (auto &j : dp)
        cout << j.first << " " << j.second << " ";
      cout << endl;
    }
    cout << endl;
  }

  return 0;
}