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

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <map>      // map
#include <math.h>   //pow,,,
#include <numeric>  //accumulate,,,
#include <queue>    // queue, priority_queue
#include <set>      // set,multiset
#include <stack>    // stack
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
long long MOD = 998244353;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  cin >> N;
  int Q;
  cin >> Q;
  long long black = (N - 2) * (N - 2);
  vector<long long> cnt_black_tate(N + 1, N - 2), cnt_black_yoko(N + 1, N - 2);
  cnt_black_tate.at(1) = 0, cnt_black_yoko.at(1) = 0;
  cnt_black_tate.at(N) = 0, cnt_black_yoko.at(N) = 0;
  // 上辺と左辺に連結するような黒のところのみ、今後に影響する
  long long UP = N, LEFT = N;
  // vector<long long> ans;
  for (int i = 0; i < Q; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      long long x;
      cin >> x;
      if (x < LEFT) {
        black -= UP - 2;
        // 左辺の今後に影響しないやつは確定させてしまう
        for (int j = x; j < LEFT; j++) {
          cnt_black_tate.at(j) = UP - 2;
        }
        LEFT = x;
      } else if (x > LEFT) {
        black -= cnt_black_tate.at(x);
      }
    }
    if (type == 2) {
      long long x;
      cin >> x;
      if (x < UP) {
        black -= LEFT - 2;
        // 上辺の今後に影響しないやつは確定させてしまう
        for (int j = x; j < UP; j++) {
          cnt_black_yoko.at(j) = LEFT - 2;
        }
        UP = x;
      } else if (x > UP) {
        black -= cnt_black_yoko.at(x);
      }
    }
    // ans.push_back(black);
  }
  // for(long long V : ans) cout << V << endl;
  cout << black << endl;
}


Error: TLE

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <map>      // map
#include <math.h>   //pow,,,
#include <numeric>  //accumulate,,,
#include <queue>    // queue, priority_queue
#include <set>      // set,multiset
#include <stack>    // stack
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
long long MOD = 998244353;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  cin >> N;
  int Q;
  cin >> Q;
  long long black = (N - 2) * (N - 2);
  vector<long long> cnt_black_tate(N + 1, N - 2), cnt_black_yoko(N + 1, N - 2);
  cnt_black_tate.at(1) = 0, cnt_black_yoko.at(1) = 0;
  cnt_black_tate.at(N) = 0, cnt_black_yoko.at(N) = 0;
  // 上辺と左辺に連結するような黒のところのみ、今後に影響する
  long long UP = N, LEFT = N;
  for (int i = 0; i < Q; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      long long x;
      cin >> x;
      if (x < LEFT) {
        // 左辺の今後に影響する
        black -= cnt_black_tate.at(x);
        LEFT = x;
        for (int j = 1; j < UP; j++) {
          cnt_black_yoko.at(j) = LEFT - 2;
        }
      } else if (x > LEFT) {
        black -= cnt_black_tate.at(x);
      }
    }
    if (type == 2) {
      long long x;
      cin >> x;
      if (x < UP) {
        // 左辺の今後に影響する
        black -= cnt_black_yoko.at(x);
        UP = x;
        for (int j = 1; j < LEFT; j++) {
          cnt_black_tate.at(j) = UP - 2;
        }
      } else if (x > UP) {
        black -= cnt_black_yoko.at(x);
      }
    }
  }
  cout << black << endl;
}