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

// 2020-09-20 02:31:45
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
// #include <atcoder/convolution>
// #include <atcoder/dsu>
// #include <atcoder/fenwicktree>
// #include <atcoder/lazysegtree>
// #include <atcoder/math>
// #include <atcoder/maxflow>
// #include <atcoder/mincostflow>
// #include <atcoder/modint>
// #include <atcoder/scc>
// #include <atcoder/segtree>
// #include <atcoder/string>
// #include <atcoder/twosat>
// #include <atcoder/all>
// using namespace atcoder;

// clang-format on
void answer() {
  int n, q;
  cin >> n >> q;
  int r = n, b = n;
  vector<int> h(n, n), w(n, n);
  ll ans = ll(n - 2) * (n - 2);
  rep(qi, q) {
    int c, x;
    cin >> c >> x;
    if (c == 1) {
      if (x < r) {
        REP(i, x, r) h[i] = b;
        ans -= b - 2;
        r = x;
      } else {
        ans -= h[x] - 2;
      }
    } else {
      if (x < b) {
        REP(i, x, b) w[i] = r;
        ans -= r - 2;
        b = x;
      } else {
        ans -= w[x] - 2;
      }
    }
  }
  cout << ans << '\\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Error: TLE

Fixed submission:

// 2020-09-20 02:31:45
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
// #include <atcoder/convolution>
// #include <atcoder/dsu>
// #include <atcoder/fenwicktree>
// #include <atcoder/lazysegtree>
// #include <atcoder/math>
// #include <atcoder/maxflow>
// #include <atcoder/mincostflow>
// #include <atcoder/modint>
// #include <atcoder/scc>
// #include <atcoder/segtree>
// #include <atcoder/string>
// #include <atcoder/twosat>
// #include <atcoder/all>
// using namespace atcoder;

// clang-format on
void answer() {
  int n, q;
  cin >> n >> q;
  int r = n, b = n;
  vector<int> h(n, n), w(n, n);
  ll ans = ll(n - 2) * (n - 2);
  rep(qi, q) {
    int c, x;
    cin >> c >> x;
    if (c == 1) {
      if (x < r) {
        REP(i, 1, b) w[i] = x;
        REP(i, x, r) h[i] = b;
        ans -= b - 2;
        r = x;
      } else {
        ans -= h[x] - 2;
      }
    } else {
      if (x < b) {
        REP(i, 1, r) h[i] = x;
        REP(i, x, b) w[i] = r;
        ans -= r - 2;
        b = x;
      } else {
        ans -= w[x] - 2;
      }
    }
  }
  cout << ans << '\\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}