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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
const ll INF = 1LL << 60;

/* RMQ：[0,n-1] について、区間ごとの最小値を管理する構造体
    update(i,x): i 番目の要素を x に更新。O(log(n))
    query(a,b): [a,b) での最小の要素を取得。O(log(n))
*/
template <typename T> struct RMQ {
  // const T INF = numeric_limits<T>::max();
  ll n;                                // 葉の数
  vector<T> dat;                       // 完全二分木の配列
  RMQ(ll n_) : n(), dat(n_ * 4, INF) { // 葉の数は 2^x の形
    ll x = 1;
    while (n_ > x) {
      x *= 2;
    }
    n = x;
  }
  void update(ll i, T x) {
    i += n - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2; // parent
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  // the minimum element of [a,b)
  T query(ll a, ll b) { return query_sub(a, b, 0, 0, n); }
  T query_sub(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l) {
      return INF;
    } else if (a <= l && r <= b) {
      return dat[k];
    } else {
      T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};

int main() {

  ll N, Q;
  cin >> N >> Q;
  // vector<ll> tx(N+1,N-2),ty(N+1,N-2);
  RMQ<ll> rmqtx(N + 1);
  RMQ<ll> rmqty(N + 1);
  rmqtx.update(N, N - 2);
  rmqty.update(N, N - 2);

  ll ans = (N - 2) * (N - 2);
  // ll TY=N;
  // ll TX=N;
  rep(i, Q) {
    ll Qtype, Qnum;
    cin >> Qtype >> Qnum;
    if (Qtype == 1) {
      ll numX = rmqtx.query(Qnum - 2, N + 1);
      ans = ans - numX;
      // cout<<ans<<endl;
      rmqty.update(numX, min(Qnum - 2, rmqty.query(numX, N + 1)));
    }
    if (Qtype == 2) {
      ll numY = rmqty.query(Qnum - 2, N + 1);
      ans = ans - numY;
      // cout<<ans<<endl;
      rmqtx.update(numY, min(Qnum - 2, rmqtx.query(numY, N + 1)));
    }

    /*  ll Qtype,Qnum;
      cin>>Qtype>>Qnum;
      if(Qtype==1){
        ans=ans-tx[Qnum];
        if(Qnum<=TX){
        //cout<<ans<<endl;
        rep(j,tx[Qnum]+2){
          if(ty[j]<=Qnum-2){break;}
         ty[j]=Qnum-2;
        }
          TX=Qnum;
        }
      }
      if(Qtype==2){
        ans=ans-ty[Qnum];
            //cout<<ans<<endl;
        if(Qnum<TY){
        rep(j,ty[Qnum]+2){
          if(tx[j]<=Qnum-2){break;}
         tx[j]=Qnum-2;
        }
          TY=Qnum;
        }
      }*/
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
const ll INF = 1LL << 60;

int main() {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> tx(N + 1, N - 2), ty(N + 1, N - 2);
  ll ans = (N - 2) * (N - 2);
  ll TY = N;
  ll TX = N;
  rep(i, Q) {
    ll Qtype, Qnum;
    cin >> Qtype >> Qnum;
    if (Qtype == 1) {
      ans = ans - tx[Qnum];
      if (Qnum <= TX) {
        // cout<<ans<<endl;
        rep(j, tx[Qnum] + 2) {
          if (ty[j] <= Qnum - 2) {
            break;
          }
          ty[j] = Qnum - 2;
        }
        TX = Qnum;
      }
    }
    if (Qtype == 2) {
      ans = ans - ty[Qnum];
      // cout<<ans<<endl;
      if (Qnum < TY) {
        rep(j, ty[Qnum] + 2) {
          if (tx[j] <= Qnum - 2) {
            break;
          }
          tx[j] = Qnum - 2;
        }
        TY = Qnum;
      }
    }
  }

  cout << ans << endl;
}