alexjercan/bugnet huggingface dataset:
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> upGet(n - 2, n - 2);
  vector<ll> leftGet(n - 2, n - 2);
  ll h = n - 2;
  ll w = n - 2;
  ll prevx = n - 2;
  ll prevy = n - 2;
  ll ans = (n - 2) * (n - 2);
  rep(i, q) {
    // cout << "i:" << i << " ans:" << ans << endl;
    ll t, x;
    cin >> t >> x;
    x -= 2;
    if (t == 1) {
      if (x < w) {
        w = x;
        ans -= h;
        for (ll i = w; i < prevx; i++) {
          upGet[i] = h;
        }
        prevx = w;
      } else {
        ans -= upGet[x];
      }
    } else {
      if (x < h) {
        h = x;
        ans -= w;
        for (ll i = h; i < prevy; i++) {
          leftGet[i] = w;
        }
        prevy = h;
      } else {
        ans -= leftGet[x];
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> upGet(n - 2, n - 2);
  vector<ll> leftGet(n - 2, n - 2);
  ll h = n - 2;
  ll w = n - 2;
  ll ans = (n - 2) * (n - 2);
  rep(i, q) {
    // cout << "i:" << i << " ans:" << ans << endl;
    ll t, x;
    cin >> t >> x;
    x -= 2;
    if (t == 1) {
      if (x < w) {
        w = x;
        ans -= h;
        for (ll i = 0; i < h; i++) {
          leftGet[i] = x;
        }
      } else {
        ans -= upGet[x];
      }
    } else {
      if (x < h) {
        h = x;
        ans -= w;
        for (ll i = 0; i < w; i++) {
          upGet[i] = x;
        }
      } else {
        ans -= leftGet[x];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

int main() {
  int N, Q;
  cin >> N >> Q;

  vector<int> a(N + 1), b(N + 1);
  for (int i = 1; i <= N; ++i)
    a[i] = b[i] = N;

  ll black = ll(N - 2) * (N - 2);

  int H = N, W = N;

  while (Q--) {
    int t, x;
    cin >> t >> x;
    if (t == 1) {
      if (x < W) {
        for (int i = x; i < W; ++i) {
          b[i] = H;
        }
        W = x;
      }
      black -= b[x] - 2;

    } else {
      if (x < H) {
        for (int i = x; i < H; ++i) {
          a[i] = W;
        }
        H = x;
      }
      black -= a[x] - 2;
    }
  }
  cout << black << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

int main() {
  ll n, q;
  cin >> n >> q;

  vector<int> x(n, n - 1), y(n, n - 1);
  ll ans = (n - 2) * (n - 2);

  for (int i = 0; i < q; i++) {
    int t, v;
    cin >> t >> v;
    v--;

    if (t == 1) {
      ans -= y[v] - 1;
      y[v] = 0;
      for (int j = 1; j < n - 1; j++) {
        if (x[j] > v) {
          x[j] = v;
        } else {
          break;
        }
      }
    } else {
      ans -= x[v] - 1;
      x[v] = 0;
      for (int j = 1; j < n - 1; j++) {
        if (y[j] > v) {
          y[j] = v;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int Q = 1 << 18;
int st[2 * Q];

const int inf = 1e9;

void se(int i, int x) {
  i += Q;
  st[i] = x;
  i >>= 1;
  while (i > 0) {
    st[i] = min(st[2 * i], st[2 * i + 1]);
    i >>= 1;
  }
}

int ge(int r) {
  r += Q;
  int res = inf;
  for (int l = Q; l < r; l >>= 1, r >>= 1) {
    if (l & 1) {
      res = min(res, st[l++]);
    }
    if (r & 1) {
      res = min(res, st[--r]);
    }
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  fill(st, st + 2 * Q, inf);
  vector<int> cols(n, -1);
  set<pair<int, int>> rows;
  for (int i = 0; i < q; i++) {
    int t, x;
    cin >> t >> x;
    if (t == 1) {
      cols[x - 1] = i;
    }
    if (t == 2) {
      rows.emplace(x - 1, i);
      se(i, x - 1);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    if (cols[i] == -1) {
      ans += rows.size();
    } else {
      int fin = n - 2;
      fin = min(fin, ge(cols[i]) - 1);
      ans += fin;
      vector<pair<int, int>> tt;
      for (auto uu : rows) {
        if (uu.first <= fin) {
          tt.push_back(uu);
          se(uu.second, inf);
        } else {
          break;
        }
      }
      for (auto uu : tt) {
        rows.erase(uu);
      }
      ans += rows.size();
    }
  }
  cout << (long long)(n - 2) * (n - 2) - ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int Q = 1 << 18;
int st[2 * Q];

const int inf = 1e9;

void se(int i, int x) {
  i += Q;
  st[i] = x;
  i >>= 1;
  while (i > 0) {
    st[i] = min(st[2 * i], st[2 * i + 1]);
    i >>= 1;
  }
}

int ge(int r) {
  r += Q;
  int res = inf;
  for (int l = Q; l < r; l >>= 1, r >>= 1) {
    if (l & 1) {
      res = min(res, st[l++]);
    }
    if (r & 1) {
      res = min(res, st[--r]);
    }
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  fill(st, st + 2 * Q, inf);
  vector<int> cols(n, -1);
  set<pair<int, int>> rows;
  for (int i = 0; i < q; i++) {
    int t, x;
    cin >> t >> x;
    if (t == 1) {
      cols[x - 1] = i;
    }
    if (t == 2) {
      rows.emplace(x - 1, i);
      se(i, x - 1);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    if (cols[i] == -1) {
      ans += rows.size();
    } else {
      int fin = n - 2;
      fin = min(fin, ge(cols[i]) - 1);
      /*vector<pair<int, int>>lost;
      for (auto u : rows) {
              if (u.second < cols[i]) {
                      fin = min(fin, u.first - 1);
              }
      }*/
      ans += fin;
      vector<pair<int, int>> tt;
      for (auto uu : rows) {
        if (uu.first <= fin) {
          tt.push_back(uu);
          se(uu.second, inf);
        }
      }
      for (auto uu : tt) {
        rows.erase(uu);
      }
      ans += rows.size();
    }
  }
  cout << (long long)(n - 2) * (n - 2) - ans;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int n, q;
long long white;
long long r[200005], c[200005];
long long curr, curc;
long long ans;

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i <= n; i++)
    r[i] = -1, c[i] = -1;
  curr = n - 2, curc = n - 2;
  for (int i = 0; i < q; i++) {
    int o, x;
    scanf("%d%d", &o, &x);
    if (o == 1) {
      for (int j = x + 1; j <= n; j++) {
        if (r[j] != -1)
          break;
        r[j] = curr;
      }
      if (r[x] != -1) {
        white += r[x];
      } else {
        white += curr;
        curc = x - 2;
      }
    } else {
      for (int j = x + 1; j <= n; j++) {
        if (c[j] != -1)
          break;
        c[j] = curc;
      }
      if (c[x] != -1) {
        white += c[x];
      } else {
        white += curc;
        curr = x - 2;
      }
    }
  }
  ans = (long long)(n - 2) * (n - 2) - white;
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, q;
long long white;
long long r[200005], c[200005];
long long curr, curc;
long long ans;

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i <= n; i++)
    r[i] = -1, c[i] = -1;
  curr = n - 2, curc = n - 2;
  for (int i = 0; i < q; i++) {
    int o, x;
    scanf("%d%d", &o, &x);
    if (o == 1) {
      for (int j = x + 1; j <= n; j++)
        if (r[j] == -1)
          r[j] = curr;
      if (r[x] != -1) {
        white += r[x];
      } else {
        white += curr;
        curc = x - 2;
      }
    } else {
      for (int j = x + 1; j <= n; j++)
        if (c[j] == -1)
          c[j] = curc;
      if (c[x] != -1) {
        white += c[x];
      } else {
        white += curc;
        curr = x - 2;
      }
    }
  }
  ans = (long long)(n - 2) * (n - 2) - white;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, Q;
  cin >> N >> Q;
  int64_t a, b, A = N - 2, B = N - 2;
  int64_t Z = (N - 2) * (N - 2);
  vector<int64_t> p(N, -1);
  vector<int64_t> q(N, -1);
  for (int j = 0; j < Q; j++) {
    cin >> a >> b;
    a--, b -= 2;
    if (a) {
      for (int64_t i = b; i < B; i++) {
        q[i] = A;
      }
      if (q[b] + 1) {
        Z -= q[b];
      } else {
        Z -= B;
      }
      B = min(B, b);
    } else {
      for (int64_t i = b; i < A; i++) {
        p[i] = B;
      }
      if (p[b] + 1) {
        Z -= p[b];
      } else {
        Z -= A;
      }
      A = min(A, b);
    }
  }
  cout << Z << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int64_t N, Q, A = N - 2, B = N - 2;
  int64_t a, b;
  cin >> N >> Q;
  int64_t Z = (N - 2) * (N - 2);
  vector<int64_t> p(N, N - 2);
  vector<int64_t> q(N, N - 2);
  for (int j = 0; j < Q; j++) {
    cin >> a >> b;
    a--, b -= 2;
    if (a) {
      for (int64_t i = b; i < A; i++) {
        p[i] = b;
      }
      Z -= q[b];
      B = min(B, b);
    } else {
      for (int64_t i = b; i < A; i++) {
        q[i] = b;
      }
      Z -= p[b];
      A = min(A, b);
    }
  }
  cout << Z << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n, q;
  cin >> n >> q;

  ll ans = (n - 2) * (n - 2);
  ll h = n, w = n;

  vector<int> W(n + 1, n), H(n + 1, n);

  while (q--) {
    int c, x;
    scanf("%d%d", &c, &x);
    if (c == 1) {
      if (x < w) {
        ans -= (h - 2);
        for (int i = x + 1; i < w; i++)
          W[i] = h;
        w = x;
      } else
        ans -= (W[x] - 2);
    } else {
      if (x < h) {
        ans -= (w - 2);
        for (int i = x + 1; i < h; i++)
          H[i] = w;
        h = x;
      } else
        ans -= (H[x] - 2);
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

int main() {
  ll n, q;
  cin >> n >> q;

  ll ans = (n - 2) * (n - 2);
  ll h = n, w = n;

  vector<int> W(n + 1, n), H(n + 1, n);

  while (q--) {
    int c, x;
    cin >> c >> x;
    if (c == 1) {
      if (x < w) {
        ans -= (W[x] - 2);
        w = x;
        for (int i = 2; i < W[x]; i++) {
          H[i] = x;
        }
      } else {
        ans -= (W[x] - 2);
      }
    } else {
      if (x < h) {
        ans -= (H[x] - 2);
        h = x;
        for (int i = 2; i < H[x]; i++) {
          W[i] = x;
        }
      } else {
        ans -= (H[x] - 2);
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
struct segtree {
  int size = 1;
  vector<int> node;
  segtree(vector<int> v) {
    while (size < v.size()) {
      size <<= 1;
    }
    node.resize(size * 2 - 1);
    for (int i = 0; i < v.size(); i++) {
      node[i + size - 1] = v[i];
    }
    for (int i = size - 2; i >= 0; i--) {
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }
  // k番目の値をaに変更
  void update(int k, int a) {
    k += size - 1;
    node[k] = a;
    while (k) {
      k = (k - 1) / 2;
      node[k] = min(node[k * 2 + 1], node[k * 2 + 2]);
    }
  }
  //[a,b)の範囲内で最小値を求める
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1) {
      r = size;
    }
    if (r <= a || b <= l) {
      return 2147483647;
    } else if (a <= l && r <= b) {
      return node[k];
    } else {
      int va = query(a, b, k * 2 + 1, l, (l + r) / 2);
      int vb = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(va, vb);
    }
  }
};
int main() {
  long long n, q;
  cin >> n >> q;
  long long now = (n - 2) * (n - 2);
  vector<int> nul(n, n);
  segtree ue(nul);
  segtree hidari(nul);
  for (int i = 0; i < q; i++) {
    int a, x;
    cin >> a >> x;
    if (a == 1) {
      int mem = ue.query(x - 1, n);
      now -= mem - 2;
      if (hidari.query(mem - 1, mem) > x) {
        hidari.update(mem - 1, x);
      }
    } else {
      int mem = hidari.query(x - 1, n);
      now -= mem - 2;
      if (ue.query(mem - 1, mem) > x) {
        ue.update(mem - 1, x);
      }
    }
  }
  cout << now << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
int main() {
  long long n, q;
  cin >> n >> q;
  long long now = (n - 2) * (n - 2);
  int ue[200000];
  int hidari[200000];
  for (int i = 0; i < n; i++) {
    ue[i] = n;
    hidari[i] = n;
  }
  for (int i = 0; i < q; i++) {
    int a, x;
    cin >> a >> x;
    if (a == 1) {
      now -= ue[x] - 2;
      for (int i = 0; i < ue[x]; i++) {
        hidari[i] = min(hidari[i], x);
      }
    } else {
      now -= hidari[x] - 2;
      for (int i = 0; i < hidari[x]; i++) {
        ue[i] = min(ue[i], x);
      }
    }
  }
  cout << now << endl;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define INF 1234567890
#define ll long long

ll res;
int N, Q;
bool dieY[200201], dieX[200201]; // 행, 열
map<int, int> D, R;              // D[x] = y, R[y] = x

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> Q;
  res = (ll)(N - 2) * (N - 2);
  D[N] = N;
  R[N] = N;
  while (Q--) {
    int t, x;
    cin >> t >> x;
    if (t == 1) // 수직 뒤집기
    {
      if (dieX[x])
        continue;
      dieX[x] = true;
      int d = D.lower_bound(x)->second;
      res -= d - 2;
      // [2,d] 구간에서 x보다 큰 값들을 다 x로 바꾸기.
      if (x < R.begin()->second) {
        for (auto it = R.begin(); it != R.end();) {
          if (it->first < d)
            it = R.erase(it);
          else
            break;
        }
        R[d] = x;
      }
    } else // 수평 뒤집기
    {
      int y = x;
      if (dieY[y])
        continue;
      dieY[y] = true;
      int r = R.lower_bound(y)->second;
      res -= r - 2;
      if (y < D.begin()->second) {
        for (auto it = D.begin(); it != D.end();) {
          if (it->first < r)
            it = D.erase(it);
          else
            break;
        }
        D[r] = y;
      }
    }
    // printf("%dth step : %lld\\n", tc, res);
  }
  cout << res;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define INF 1234567890
#define ll long long

ll res;
int N, Q;
bool dieY[200201], dieX[200201]; // 행, 열
map<int, int> D, R;              // D[x] = y, R[y] = x

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> Q;
  res = (ll)(N - 2) * (N - 2);
  D[N] = N;
  R[N] = N;
  for (int tc = 1; tc <= Q; tc++) {
    int t, x;
    cin >> t >> x;
    if (t == 1) // 수직 뒤집기
    {
      if (dieX[x])
        continue;
      dieX[x] = true;
      int d = D.lower_bound(x)->second;
      res -= d - 2;
      // [2,d] 구간에서 x보다 큰 값들을 다 x로 바꾸기.
      // 단조 증가 수열이다.
      if (x < R.begin()->second) {
        while (R.begin()->first < d)
          R.erase(R.begin());
        R[d] = x;
      }
    } else // 수평 뒤집기
    {
      int y = x;
      if (dieY[y])
        continue;
      dieY[y] = true;
      int r = R.lower_bound(y)->second;
      res -= r - 2;
      if (y < D.begin()->second) {
        while (D.begin()->first <= r)
          D.erase(D.begin());
        D[r] = y;
      }
    }
    // printf("%dth step : %lld\\n", tc, res);
  }
  cout << res;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long n, q;
  cin >> n >> q;
  long long query[2];
  vector<vector<vector<long long>>> min(2, vector<vector<long long>>());

  long long sum = 0;
  long long buf;
  for (int i = 0; i < q; i++) {
    buf = n - 2;
    cin >> query[0] >> query[1];
    query[0]--;
    query[1]--;

    for (int j = min[query[0]].size() - 1; j >= 0; j--) {
      if (min[query[0]][j][0] >= query[1]) {
        buf = min[query[0]][j][1] - 1;
        break;
      }
    }
    sum += buf;
    if (!min[!query[0]].size() ||
        min[!query[0]][min[!query[0]].size() - 1][1] > query[1]) {
      min[!query[0]].push_back({buf, query[1]});
    }
  }

  cout << (n - 2) * (n - 2) - sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  long long n, q;
  cin >> n >> q;
  int query[2];
  vector<vector<int>> min(2, vector<int>(n, n - 1));

  long long sum = 0;
  long long buf;
  for (int i = 0; i < q; i++) {
    cin >> query[0] >> query[1];
    query[0]--;
    query[1]--;

    buf = min[query[0]][query[1]] - 1;
    sum += buf;
    for (int j = 1; j <= buf; j++) {
      if (min[!query[0]][j] > query[1]) {
        min[!query[0]][j] = query[1];
      } else {
        break;
      }
    }
  }

  cout << (n - 2) * (n - 2) - sum << endl;

  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n >> q;

  ll ans = n - 2;
  ans *= ans;
  vector<int> v[] = {vector<int>(n + 1, n), vector<int>(n + 1, n)};
  int d[] = {n, n};
  rep(_, q) {
    int x, a;
    cin >> x >> a;
    --x;
    if (a < d[x]) {
      for (int i = a; i < d[x]; ++i) {
        v[x][i] = d[!x];
      }
      d[x] = a;
    }
    ans -= v[x][a] - 2;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, q;
  cin >> n >> q;

  ll ans = n - 2;
  ans *= ans;
  vector<int> v[] = {vector<int>(n + 1, n), vector<int>(n + 1, n)};
  int d[] = {n, n};
  rep(_, q) {
    int x, a;
    cin >> x >> a;
    --x;
    if (a < d[x]) {
      rep(i, d[x]) { v[x][i] = d[!x]; }
      d[x] = a;
    }
    ans -= v[x][a] - 2;
  }
  cout << ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int f, x, y, q, i, j, xmx[200000], xmy[200000], ymx[200000], ymy[200000];
  int xct = 1, yct = 1, xmm = 200000, ymm = 200000;
  long long int ans, n;
  cin >> n >> q;

  for (i = 0; i < 20000; i++) {
    xmx[i] = 0;
    xmy[i] = 0;
    ymx[i] = 0;
    ymy[i] = 0;
  }

  xmx[0] = n;
  xmy[0] = n;
  ymx[0] = n;
  ymy[0] = n;
  xmm = n;
  ymm = n;

  ans = (n - 2) * (n - 2);

  for (i = 0; i < q; i++) {
    cin >> f;

    if (f == 1) {
      cin >> x;
      if (xmm > x) {
        xmm = x;
        xmx[xct] = x;
        xmy[xct] = ymy[yct - 1];
        xct++;
        ans -= ymy[yct - 1];
        ans += 2;

      } else {
        for (j = 0; j < n; j++) {
          if (ymx[j] < x) {
            break;
          }
        }
        j--;
        ans -= ymy[j];
        ans += 2;
      }
    }
    if (f == 2) {
      cin >> y;
      if (ymm > y) {
        ymm = y;
        ymy[yct] = y;
        ymx[yct] = xmx[xct - 1];
        yct++;

        ans -= xmx[xct - 1];
        ans += 2;

      } else {
        for (j = 0; j < n; j++) {
          if (xmy[j] < y) {
            break;
          }
        }
        j--;
        ans -= xmx[j];
        ans += 2;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int f, x, y, q, i, j, xmx[200000], xmy[200000], ymx[200000], ymy[200000];
  int xct = 1, yct = 1, xmm = 200000, ymm = 200000;
  long long int ans, n;
  cin >> n >> q;

  for (i = 0; i < 20000; i++) {
    xmx[i] = 0;
    xmy[i] = 0;
    ymx[i] = 0;
    ymy[i] = 0;
  }

  xmx[0] = n;
  xmy[0] = n;
  ymx[0] = n;
  ymy[0] = n;
  xmm = n;
  ymm = n;

  ans = (n - 2) * (n - 2);

  for (i = 0; i < q; i++) {
    cin >> f;

    if (f == 1) {
      cin >> x;
      if (xmm > x) {
        xmm = x;
        xmx[xct] = x;

        for (j = 0; j < n; j++) {
          if (ymx[j] < x) {
            break;
          }
        }

        j--;

        xmy[xct] = ymy[j];
        xct++;
      }
      for (j = 0; j < n; j++) {
        if (ymx[j] < x) {
          break;
        }
      }
      j--;
      ans -= ymy[j];
      ans += 2;
    }
    if (f == 2) {
      cin >> y;
      if (ymm > y) {
        ymm = y;
        ymy[yct] = y;

        for (j = 0; j < n; j++) {
          if (xmy[j] < y) {
            break;
          }
        }
        j--;
        ymx[yct] = xmx[j];

        yct++;
      }

      for (j = 0; j < n; j++) {
        if (xmy[j] < y) {
          break;
        }
      }
      j--;
      ans -= xmx[j];
      ans += 2;
    }
  }
  cout << ans << endl;

  return 0;
}