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

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 1;

#include <unistd.h>

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

  int64_t N, Q;
  cin >> N >> Q;
  vector<int32_t> r(N + 1, N);
  vector<int32_t> c(N + 1, N);
  int32_t rwall = N;
  int32_t cwall = N;
  int64_t res = (N - 2) * (N - 2);
  for (int i = 0; i < Q; i++) {
    int qtype, idx;
    cin >> qtype >> idx;

    auto update = [&res, idx](vector<int32_t> &vec, int32_t &wall,
                              int32_t &ort_wall) {
      if (idx > wall) {
        res -= vec[idx] - 2;
      } else {
        res -= ort_wall - 2;
        for (int i = idx + 1; i < wall; i++) {
          vec[i] = ort_wall;
        }
        wall = idx;
      }
    };

    if (qtype == 1) {
      update(c, cwall, rwall);
    } else {
      update(r, rwall, cwall);
    }
  }
  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9 + 1;

#include <unistd.h>

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

  int64_t N, Q;
  cin >> N >> Q;
  vector<int32_t> r(N + 1, N);
  vector<int32_t> c(N + 1, N);
  int32_t rwall = N;
  int32_t cwall = N;
  int64_t res = (N - 2) * (N - 2);
  for (int i = 0; i < Q; i++) {
    int qtype, idx;
    cin >> qtype >> idx;

    auto update = [&res, idx](vector<int32_t> &vec, vector<int32_t> &ort_vec,
                              int32_t &wall) {
      if (idx > wall) {
        res -= vec[idx] - 2;
      } else {
        res -= vec[idx] - 2;
        wall = idx;
        for (int i = 2; i < vec[idx]; i++) {
          ort_vec[i] = idx;
        }
      }
    };

    if (qtype == 1) {
      update(c, r, cwall);
    } else {
      update(r, c, rwall);
    }
  }
  cout << res << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N - 1, N - 1), B(N - 1, N - 1);
  int64_t res = static_cast<int64_t>(N - 2) * (N - 2);
  int H = N - 2, W = N - 2;
  for (int q = 0; q < Q; q++) {
    int t, x;
    cin >> t >> x;
    x -= 2;
    if (t == 1) {
      if (x < W) {
        for (int i = x; i < W; i++) {
          B[i] = H;
        }
        W = x;
      }
      res -= B[x] - 0;
    } else {
      if (x < H) {
        for (int i = x; i < H; i++) {
          A[i] = W;
        }
        H = x;
      }
      res -= A[x] - 0;
    }
  }
  cout << res << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N - 1, N - 1), B(N - 1, N - 1);
  int64_t res = static_cast<int64_t>(N - 2) * (N - 2);
  int H = N - 1, W = N - 1;
  for (int q = 0; q < Q; q++) {
    int t, x;
    cin >> t >> x;
    x -= 2;
    if (t == 1) {
      if (x < W) {
        for (int i = 0; i < H; i++) {
          A[i] = x + 1;
        }
        W = x + 1;
      }
      res -= max(0, B[x] - 1);
    } else {
      if (x < H) {
        for (int i = 0; i < W; i++) {
          B[i] = x + 1;
        }
        H = x + 1;
      }
      res -= max(0, A[x] - 1);
    }
  }
  cout << res << '\\n';
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll N, Q;

pair<ll, ll> QUERY[200005];

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

  rep(i, Q) {
    cin >> QUERY[i].first >> QUERY[i].second;
    QUERY[i].second--;
  }
  ll ans = (N - 2) * (N - 2);

  set<ll> yokoset;
  set<ll> tateset;
  map<ll, ll> yokomemo;
  map<ll, ll> tatememo;

  yokoset.insert(N);
  tateset.insert(N);
  yokomemo[N] = N - 2;
  tatememo[N] = N - 2;

  rep(i, Q) {
    ll pos = QUERY[i].second;

    if (QUERY[i].first == 1) {
      // 下に行く
      ll haba = tatememo[*tateset.lower_bound(pos)];

      // cout << "get " << haba << endl;
      ans -= haba;

      ll curyoko = yokomemo[*yokoset.lower_bound(haba)];
      if (curyoko < pos - 1)
        continue;
      yokoset.insert(haba);
      yokomemo[haba] = pos - 1;
    } else {
      // 右に行く
      ll haba = yokomemo[*yokoset.lower_bound(pos)];

      // cout << "get " << haba << endl;
      ans -= haba;

      ll curtate = tatememo[*tateset.lower_bound(haba)];
      if (curtate < pos - 1)
        continue;
      tateset.insert(haba);
      tatememo[haba] = pos - 1;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 // 10^9+7

ll N, Q;

pair<ll, ll> QUERY[200005];

ll yoko[200005];
ll tate[200005];

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

  rep(i, Q) {
    cin >> QUERY[i].first >> QUERY[i].second;
    QUERY[i].second--;
  }
  rep(i, N + 3) yoko[i] = N - 2;
  rep(i, N + 3) tate[i] = N - 2;

  ll yoko_default = N - 2;
  ll tate_default = N - 2;

  ll ans = (N - 2) * (N - 2);
  rep(i, Q) {
    // cout << " i = " << i << endl;
    // rep(j, N) {
    //	cout << "tate[" << j << "] = " << tate[j] << endl;
    // }

    // rep(j, N) {
    //	cout << "yoko[" << j << "] = " << yoko[j] << endl;
    // }

    ll pos = QUERY[i].second;
    if (QUERY[i].first == 1) {
      ll haba;
      // 下に行く
      if (tate[pos] == -1) {
        haba = tate_default;
      } else {
        haba = tate[pos];
      }
      // cout << "get " << haba << endl;
      ans -= haba;

      for (ll j = 0; j <= haba; j++) {
        if (yoko[j] < pos - 1)
          break;
        yoko[j] = min(yoko[j], pos - 1);
      }
    } else {
      // 右に行く
      ll haba;
      if (yoko[pos] == -1) {
        haba = yoko_default;
      } else {
        haba = yoko[pos];
      }
      // cout << "get " << haba << endl;
      ans -= haba;

      for (ll j = 0; j <= haba; j++) {
        if (tate[j] < pos - 1)
          break;
        tate[j] = min(tate[j], pos - 1);
      }
    }
  }
  cout << ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long n, q;
  cin >> n >> q;
  vector<int> u(n - 2, n - 2), l(n - 2, n - 2);
  long long ans = 0;
  long long lmin = n - 2, umin = n - 2;
  long long lminv = n - 2, uminv = n - 2;
  for (int i = 0; i < q; i++) {
    int type, x;
    cin >> type >> x;
    x -= 2;
    if (type == 1) {
      // up
      if (lmin > x) {
        ans += uminv;
        for (int j = x; j < lmin; j++) {
          u[j] = uminv;
        }
        lmin = x;
        lminv = x;
      } else {
        ans += u[x];
      }
    } else {
      // left
      if (umin > x) {
        ans += lminv;
        for (int j = x; j < umin; j++) {
          l[j] = lminv;
        }
        umin = x;
        uminv = x;
      } else {
        ans += l[x];
      }
    }
  }
  cout << (n - 2) * (n - 2) - ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n, q;
  cin >> n >> q;
  vector<int> u(n - 2, n - 2), l(n - 2, n - 2);
  long long ans = 0;
  long long lmin = n - 2, umin = n - 2;
  for (int i = 0; i < q; i++) {
    int type, x;
    cin >> type >> x;
    x -= 2;
    if (type == 1) {
      // up
      ans += u[x];
      if (lmin > x) {
        for (int j = 0; j < u[x]; j++) {
          l[j] = min(l[j], x);
        }
        lmin = x;
      }
      u[x] = 0;
    } else {
      // left
      ans += l[x];
      if (umin > x) {
        for (int j = 0; j < l[x]; j++) {
          u[j] = min(u[j], x);
        }
        umin = x;
      }
      l[x] = 0;
    }
  }
  cout << (n - 2) * (n - 2) - ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, Q;
  cin >> N >> Q;
  long long xN = N - 3;
  long long yN = N - 3;
  vector<long long> A(N - 2, N - 2);
  vector<long long> B(N - 2, N - 2);
  if (false) {
    cout << xN << " " << yN << endl;
    cout << A[0] << A[1] << A[2] << endl;
    cout << B[0] << B[1] << B[2] << endl;
    cout << endl;
  }
  for (int i = 0; i < Q; ++i) {
    long long x, y;
    cin >> x >> y;
    y -= 2;
    if (1 == x) {
      A[y] = 0;
      if (y <= xN) {
        for (int j = y + 1; j <= xN; ++j) {
          A[j] = yN + 1;
        }
        xN = y - 1;
      }
    } else {
      B[y] = 0;
      if (y <= yN) {
        for (int j = y + 1; j <= yN; ++j) {
          B[j] = xN + 1;
        }
        yN = y - 1;
      }
    }
  }
  long long ans = (xN + 1) * (yN + 1);
  for (int i = xN + 1; i < N - 2; ++i) {
    ans += A[i];
  }
  for (int i = yN + 1; i < N - 2; ++i) {
    ans += B[i];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  long long xN = N - 3;
  long long yN = N - 3;
  vector<long long> A(N - 2, N - 2);
  vector<long long> B(N - 2, N - 2);
  for (int i = 0; i < Q; ++i) {
    long long x, y;
    cin >> x >> y;
    y -= 2;
    if (1 == x) {
      A[y] = 0;
      if (y <= xN) {
        xN = y - 1;
        for (int j = 0; j <= yN; ++j) {
          B[j] = y;
        }
      }
    } else {
      B[y] = 0;
      if (y <= yN) {
        yN = y - 1;
        for (int j = 0; j <= xN; ++j) {
          A[j] = y;
        }
      }
    }
  }
  long long ans = (xN + 1) * (yN + 1);
  for (int i = xN + 1; i < N - 2; ++i) {
    ans += A[i];
  }
  for (int i = yN + 1; i < N - 2; ++i) {
    ans += B[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  llint N;
  int Q;

  cin >> N >> Q;

  llint ans = (N - 2) * (N - 2);

  vector<int> right_v(N + 1, 0), bottom_v(N + 1, 0);
  set<int> right_s, bottom_s;

  right_v[N] = N;
  bottom_v[N] = N;
  right_s.insert(N);
  bottom_s.insert(N);

  for (int q = 0; q < Q; q++) {
    int num, x;

    cin >> num >> x;

    if (num == 1) {
      auto it = bottom_s.upper_bound(x);
      bottom_s.insert(x);
      bottom_v[x] = bottom_v[*it];
      ans -= bottom_v[x] - 2;
      it = right_s.upper_bound(bottom_v[x] - 1);

      if (x < right_v[*it]) {
        right_v[*it] = x;
      }
    } else {
      auto it = right_s.upper_bound(x);
      right_s.insert(x);
      right_v[x] = right_v[*it];
      ans -= right_v[x] - 2;
      it = bottom_s.upper_bound(right_v[x] - 1);

      if (x < bottom_v[*it]) {
        bottom_v[*it] = x;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define llint long long int

using namespace std;

int main() {
  llint N;
  int Q;

  cin >> N >> Q;

  llint ans = (N - 2) * (N - 2);

  vector<int> right_v(N + 1, 0), bottom_v(N + 1, 0);
  set<int> right_s, bottom_s;

  right_v[N] = N;
  bottom_v[N] = N;
  right_s.insert(N);
  bottom_s.insert(N);

  for (int q = 0; q < Q; q++) {
    int num, x;

    cin >> num >> x;

    if (num == 1) {
      auto it = upper_bound(bottom_s.begin(), bottom_s.end(), x);
      bottom_s.insert(x);
      bottom_v[x] = bottom_v[*it];
      ans -= bottom_v[x] - 2;
      it = upper_bound(right_s.begin(), right_s.end(), bottom_v[x] - 1);

      if (x < right_v[*it]) {
        right_v[*it] = x;
      }
    } else {
      auto it = upper_bound(right_s.begin(), right_s.end(), x);
      right_s.insert(x);
      right_v[x] = right_v[*it];
      ans -= right_v[x] - 2;
      it = upper_bound(bottom_s.begin(), bottom_s.end(), right_v[x] - 1);

      if (x < bottom_v[*it]) {
        bottom_v[*it] = x;
      }
    }
  }
  cout << ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define fst first
#define snd second
#define ll long long
#define ld long double
#define pb push_back
#define emp emplace_back
#define pii pair<int, int>
#define usg unsigned
#define sg signed
#define mp make_pair

using namespace std;

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}

const ld PI = 4 * atan((ld)1);
const int INF = 1e9 + 7;
const ll _INF = 1e18 + 7;

set<pii> st[2];

int main() {
  setIO();
  ll n, q;
  cin >> n >> q;
  ll sum = (n - 2) * (n - 2);
  st[0].insert({n, n});
  st[1].insert({n, n});
  while (q--) {
    int t, x;
    cin >> t >> x;
    t--;
    auto it = st[t].lower_bound({x, -1});
    int y = (*it).snd;
    sum -= y - 2;
    st[t ^ 1].insert({y - 1, x});
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fst first
#define snd second
#define ll long long
#define ld long double
#define pb push_back
#define emp emplace_back
#define pii pair<int, int>
#define usg unsigned
#define sg signed
#define mp make_pair

using namespace std;

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
}

const ld PI = 4 * atan((ld)1);
const int INF = 1e9 + 7;
const ll _INF = 1e18 + 7;

set<pii> r, c;

int main() {
  setIO();
  ll n, q;
  cin >> n >> q;
  ll sum = (n - 2) * (n - 2);
  r.insert(mp(n, n));
  c.insert(mp(n, n));
  while (q--) {
    int t, x;
    cin >> t >> x;
    if (t & 1) {
      auto it = lower_bound(r.begin(), r.end(), mp(x, -1));
      int y = (*it).snd;
      sum -= y - 2;
      c.insert(mp(y - 1, x));
    } else {
      auto it = lower_bound(c.begin(), c.end(), mp(x, -1));
      int y = (*it).snd;
      sum -= y - 2;
      r.insert(mp(y - 1, x));
    }
  }
  cout << sum << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

#define INF 1001001001
#define MAX 1e18

const int mod = 998244353;

int main() {
  int n, q;
  cin >> n >> q;
  ll ans = (ll)(n - 2) * (n - 2);
  vector<int> rm_row(n), rm_col(n);
  int rend = n - 1, cend = n - 1;
  rep(i, q) {
    int c, x;
    cin >> c >> x;
    x--;
    if (c == 1) {
      if (x > rend) {
        ans -= rm_row[x];
        rm_row[x] = 0;
      } else {
        ans -= cend - 1;
        for (int j = x + 1; j < rend; j++) {
          rm_row[j] = cend - 1;
        }
        rend = x;
      }
    } else {
      if (x > cend) {
        ans -= rm_col[x];
        rm_col[x] = 0;
      } else {
        ans -= rend - 1;
        for (int j = x + 1; j < cend; j++) {
          rm_col[j] = rend - 1;
        }
        cend = x;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;

#define INF 1001001001
#define MAX 1e18

const int mod = 998244353;

int main() {
  int n, q;
  cin >> n >> q;
  ll ans = (ll)(n - 2) * (n - 2);
  vector<int> candel_row(n, n - 2), candel_col(n, n - 2);
  candel_row[0] = candel_row[n - 1] = candel_col[0] = candel_col[n - 1] = 0;
  int rend = n - 1, cend = n - 1;
  rep(i, q) {
    int c, x;
    cin >> c >> x;
    x--;
    if (c == 1) {
      ans -= candel_col[x];
      candel_col[x] = 0;
      if (x < rend) {
        rend = x;
        for (int j = 1; j < cend; j++) {
          candel_row[j] = rend - 1;
        }
      }
    } else {
      ans -= candel_row[x];
      candel_row[x] = 0;
      if (x < cend) {
        cend = x;
        for (int j = 1; j < rend; j++) {
          candel_col[j] = cend - 1;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;

int main() {
  ll n;
  int q;
  cin >> n >> q;
  set<ll> st1, st2;
  st1.insert(n);
  st2.insert(n);
  ll c1[200005]{0}, c2[200005]{0};
  ll ans = (n - 2) * (n - 2);
  while (q--) {
    int c;
    ll x;
    cin >> c >> x;
    if (c == 1) {
      auto itr = st1.lower_bound(x);
      if (itr == st1.begin()) {
        st1.insert(x);
        c1[x] = *st2.begin() - 2;
        ans -= c1[x];
      } else
        ans -= c1[*--itr];
    } else {
      auto itr = st2.lower_bound(x);
      if (itr == st2.begin()) {
        st2.insert(x);
        c2[x] = *st1.begin() - 2;
        ans -= c2[x];
      } else
        ans -= c2[*--itr];
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;

int main() {
  ll n;
  int q;
  cin >> n >> q;
  set<ll> st1, st2;
  st1.insert(n);
  st2.insert(n);
  ll c1[100005]{0}, c2[100005]{0};
  ll ans = (n - 2) * (n - 2);
  while (q--) {
    int c;
    ll x;
    cin >> c >> x;
    if (c == 1) {
      auto itr = st1.lower_bound(x);
      if (itr == st1.begin()) {
        st1.insert(x);
        c1[x] = *st2.begin() - 2;
        ans -= c1[x];
      } else
        ans -= c1[*--itr];
    } else {
      auto itr = st2.lower_bound(x);
      if (itr == st2.begin()) {
        st2.insert(x);
        c2[x] = *st1.begin() - 2;
        ans -= c2[x];
      } else
        ans -= c2[*--itr];
    }
  }
  cout << ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define int long long

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int n, q;
  cin >> n >> q;
  vector<set<tuple<int, int, int>>> st(3);
  st[1].insert(make_tuple(2, n - 1, n));
  st[2].insert(make_tuple(2, n - 1, n));
  int ans = (n - 2) * (n - 2);
  for (int qq = 0; qq < q; qq++) {
    int num, x;
    cin >> num >> x;

    auto itr = st[num].lower_bound(make_tuple(x + 1, 0, 0));
    itr--;
    int l = get<0>(*itr);
    int r = get<1>(*itr);
    int fw = get<2>(*itr);
    assert(l <= x && x <= r);
    ans -= fw - 2;
    st[num].erase(*itr);
    st[num].insert(make_tuple(l, x - 1, fw));
    st[num].insert(make_tuple(x + 1, r, fw));

    // fw の更新
    auto itr2 = st[3 - num].lower_bound(make_tuple(fw + 1, 0, 0));
    itr2--;
    int ll = get<0>(*itr2);
    int rr = get<1>(*itr2);
    int ffw = min(get<2>(*itr2), x);
    assert(rr <= fw);
    st[3 - num].erase(*itr2);
    st[3 - num].insert(make_tuple(ll, rr, ffw));
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define int long long

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int n, q;
  cin >> n >> q;
  vector<set<tuple<int, int, int>>> st(3);
  st[1].insert(make_tuple(2, n - 1, n));
  st[2].insert(make_tuple(2, n - 1, n));
  int ans = (n - 2) * (n - 2);
  for (int qq = 0; qq < q; qq++) {
    int num, x;
    cin >> num >> x;

    auto itr = st[num].lower_bound(make_tuple(x, x, n + 1));
    itr--;
    int l = get<0>(*itr);
    int r = get<1>(*itr);
    int fw = get<2>(*itr);
    ans -= fw - 2;
    st[num].erase(*itr);
    st[num].insert(make_tuple(l, x - 1, fw));
    st[num].insert(make_tuple(x + 1, r, fw));

    // fw の更新
    auto itr2 = st[3 - num].lower_bound(make_tuple(fw, fw, n + 1));
    itr2--;
    int ll = get<0>(*itr2);
    int rr = get<1>(*itr2);
    int ffw = min(get<2>(*itr2), x);
    assert(rr <= fw);
    st[3 - num].erase(*itr2);
    st[3 - num].insert(make_tuple(ll, rr, ffw));
  }

  cout << ans << endl;

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

// https://atcoder.jp/contests/abc179/submissions/16845772

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> a(n + 1), b(n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = b[i] = n;
  }
  ll black = ll(n - 2) * ll(n - 2);
  int h = n, w = n;
  for (int i = 0; i < q; i++) {
    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;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc179/submissions/16845772

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> a(n + 1), b(n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = b[i] = n;
  }
  ll black = ll(n - 2) * ll(n - 2);
  int h = n, w = n;
  for (int i = 0; i < q; i++) {
    int t, x;
    cin >> t >> x;
    if (t == 1) { // 縦
      if (x < w) {
        for (int i = 1; i < w; i++) {
          b[i] = h;
        }
        w = x;
      }
      black -= b[x] - 2;
    } else {
      if (x < h) {
        for (int i = 1; i < h; i++) {
          a[i] = w;
        }
        h = x;
      }
      black -= a[x] - 2;
    }
  }
  cout << black << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;

int n, q;
struct Node {
  int c[N];
  int sum(int x) {
    int res = n;
    while (x) {
      res = min(res, c[x]);
      x -= x & -x;
    }
    return res;
  }
  void add(int x, int d) {
    while (x <= n) {
      c[x] = min(c[x], d);
      x += x & -x;
    }
  }
} T1, T2;
int main() {
  cin >> n >> q;
  LL ans = (n - 2LL) * (n - 2);
  for (int i = 1; i <= n; i++)
    T1.c[i] = T2.c[i] = n;
  while (q--) {
    int op, x;
    cin >> op >> x;
    if (op == 1) {
      int z = T1.sum(n - x + 1);
      T2.add(n - z + 1, x);
      ans -= z - 2;
    } else {
      int z = T2.sum(n - x + 1);
      T1.add(n - z + 1, x);
      ans -= z - 2;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 100005;

int n, q;
struct Node {
  int c[N];
  int sum(int x) {
    int res = n;
    while (x) {
      res = min(res, c[x]);
      x -= x & -x;
    }
    return res;
  }
  void add(int x, int d) {
    while (x <= n) {
      c[x] = min(c[x], d);
      x += x & -x;
    }
  }
} T1, T2;
int main() {
  cin >> n >> q;
  LL ans = (n - 2LL) * (n - 2);
  for (int i = 1; i <= n; i++)
    T1.c[i] = T2.c[i] = n;
  while (q--) {
    int op, x;
    cin >> op >> x;
    if (op == 1) {
      int z = T1.sum(n - x + 1);
      T2.add(n - z + 1, x);
      ans -= z - 2;
    } else {
      int z = T2.sum(n - x + 1);
      T1.add(n - z + 1, x);
      ans -= z - 2;
    }
  }
  cout << ans << endl;
  return 0;
}