alexjercan/bugnet huggingface dataset:
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long n;
  long x, m;
  cin >> n >> x >> m;
  vector<long> a(m + 100), ar(m + 100);
  vector<long> numchk(m, -1); // 最後にその数字があった場所
  a[0] = x;
  ar[0] = 0;
  numchk[x] = 0;
  long loopstart, loopcnt;
  for (int i = 1; i <= m; i++) {
    a[i] = (a[i - 1] * a[i - 1]) % m;
    if (numchk[a[i]] >= 0) {
      loopstart = numchk[a[i]];
      loopcnt = i - numchk[a[i]];
      ar[i] = ar[i - 1] + a[i - 1];
      break;
    } else
      numchk[a[i]] = i;
    ar[i] = ar[i - 1] + a[i - 1];
  }
  // for(int i=0; i<m; i++)cerr << ar[i] << ' ';
  if (n <= loopstart) {
    cout << ar[n];
  } else {
    long ret = ar[loopstart];
    ret +=
        (n - loopstart) / loopcnt * (ar[loopstart + loopcnt] - ar[loopstart]);
    ret += ar[loopstart + (n - loopstart) % loopcnt] - ar[loopstart];
    cout << ret;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  long n;
  long x, m;
  cin >> n >> x >> m;
  vector<long> a(m), ar(m + 1);
  vector<long> numchk(m, -1); // 最後にその数字があった場所
  a[0] = x;
  ar[0] = 0;
  numchk[x] = 0;
  long loopstart, loopcnt;
  for (int i = 1; i < m; i++) {
    a[i] = (a[i - 1] * a[i - 1]) % m;
    if (numchk[a[i]] >= 0) {
      loopstart = numchk[a[i]];
      loopcnt = i - numchk[a[i]];
      ar[i] = ar[i - 1] + a[i - 1];
      break;
    } else
      numchk[a[i]] = i;
    ar[i] = ar[i - 1] + a[i - 1];
  }
  // for(int i=0; i<m; i++)cerr << ar[i] << ' ';
  if (n <= loopstart) {
    cout << ar[n];
  } else {
    long ret = ar[loopstart];
    ret +=
        (n - loopstart) / loopcnt * (ar[loopstart + loopcnt] - ar[loopstart]);
    ret += ar[loopstart + (n - loopstart) % loopcnt] - ar[loopstart];
    cout << ret;
  }
  return 0;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#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>;

int main() {
  ll N, X, M;
  cin >> N >> X >> M;
  if (X == 0) {
    cout << X << endl;
    return 0;
  }

  vector<ll> S(M + 1, 0);
  vector<ll> T(M + 1, 0);
  S.at(X) = 1;
  T.at(1) = X;

  ll i = 2;

  bool loop = false;
  ll l, r;
  while (i <= N) {
    X = (X * X) % M;
    T.at(i) = T.at(i - 1) + X;
    if (S.at(X) > 0) {
      l = S.at(X) - 1;
      r = i - 1;
      loop = true;
      break;
    } else {
      S.at(X) = i;
    }
    i++;
  }
  ll ans = T.at(i - 1);

  if (loop) {
    ll dx = r - l;
    ll dy = T.at(r) - T.at(l);
    ans += dy * ((N - r) / dx);
    ans += T.at(l + (N - r) % dx) - T.at(l);
  }

  cout << ans << endl;
}


Error: 0

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>;

int main() {
  ll N;
  ll X, M;
  cin >> N >> X >> M;
  vector<ll> S(M + 1, 0);
  vector<ll> T(M + 1, 0);
  S.at(X) = 1;
  T.at(1) = X;
  if (N == 1) {
    cout << X << endl;
    return 0;
  }

  ll i = 2;

  bool loop = false;
  ll l, r;
  while (i <= N) {
    X = (X * X) % M;
    T.at(i) = T.at(i - 1) + X;
    if (S.at(X) > 0) {
      l = S.at(X) - 1;
      r = i - 1;
      loop = true;
      break;
    } else {
      S.at(X) = i;
    }
    i++;
  }
  ll ans = T.at(i - 1);

  if (loop) {
    ll dx = r - l;
    ll dy = T.at(r) - T.at(l);
    ans += dy * ((N - r) / dx);
    ans += T.at(l + (N - r) % dx) - T.at(l);
  }

  cout << ans << endl;
}
Problem p02550 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

long n, x, m;
vector<long> vec;
int visited[100003];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

  scanf("%lld %lld %lld", &n, &x, &m);
  long a = x;
  while (1) {
    visited[a] = vec.size();
    vec.push_back(a);
    a = (a * a) % m;
    if (visited[a]) {
      // printf("%lld\\n", a);
      break;
    }
  }
  int sz1 = 0, sz2 = vec.size();
  long sum = 0;
  for (int i = visited[a]; i < vec.size(); i++) {
    sz2--;
    sz1++;
    sum += vec[i];
  }
  // printf("%lld\\n", sum);
  // for(auto x : vec) printf("%lld ", x); printf("\\n");

  long res = 0;
  if (n <= sz2) {
    for (int i = 0; i < n; i++)
      res += vec[i];
  } else {
    for (int i = 0; i < sz2; i++)
      res += vec[i];
    n -= sz2;

    res += sum * (n / sz1);
    n %= sz1;
    for (int i = 0; i < n; i++)
      res += vec[visited[a] + i];
  }
  printf("%lld\\n", res);
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;

#define fbo find_by_order
#define ook order_of_key
#define fi first
#define se second
#define long long long
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; mt19937_64
// rng(std::chrono::system_clock::now().time_since_epoch().count());

// /mnt/c/Users/vince/Desktop/POST-IOI/

long n, x, m;
vector<long> vec;
int visited[100003];

int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.in", "r", stdin);

  // scanf("%lld %lld %lld", &n, &x, &m);
  long a = x;
  while (1) {
    visited[a] = vec.size();
    vec.push_back(a);
    a = (a * a) % m;
    if (visited[a]) {
      // printf("%lld\\n", a);
      break;
    }
  }
  int sz1 = 0, sz2 = vec.size();
  long sum = 0;
  for (int i = visited[a]; i < vec.size(); i++) {
    sz2--;
    sz1++;
    sum += vec[i];
  }
  // printf("%lld\\n", sum);
  // for(auto x : vec) printf("%lld ", x); printf("\\n");

  long res = 0;
  if (n <= sz2) {
    for (int i = 0; i < n; i++)
      res += vec[i];
  } else {
    for (int i = 0; i < sz2; i++)
      res += vec[i];
    n -= sz2;

    res += sum * (n / sz1);
    n %= sz1;
    for (int i = 0; i < n; i++)
      res += vec[visited[a] + i];
  }
  printf("%lld\\n", res);
}
Problem p02550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using ml = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

void solve() {
  int N, X, M, S = 0;
  cin >> N >> X >> M;
  vector<pair<int, int>> memo(M);
  REP(i, N) {
    // 循環した場合
    if (memo[X].first) {
      // ループ間の長さ
      int t = i - memo[X].first;
      // ループが繰り返される回数
      int c = (N - i - 1) / t;
      i += t * c;
      S += (S - memo[X].second) * c;
    } else {
      memo[X] = {i, S};
    }
    S += X;
    X = X * X % M;
  }
  cout << S << endl;
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using ml = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

long long fastpow(long long n, long long x, long long m) {
  long long ret = 1;
  while (x) {
    if (x & 1)
      (ret *= n) %= m;
    x >>= 1;
    (n *= n) %= m;
  }
  return ret;
}

void solve() {
  int N, X, M;
  cin >> N >> X >> M;
  int sum = X;
  REP(i, N - 1) {
    X = fastpow(X, 2, M);
    sum += X;
  }
  cout << sum << endl;
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  solve();
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = int(a); i < (int)b; ++i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vecctor<ll>>
#define vp vector<pair<ll, ll>>
#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  ll ans = (n - 2) * (n - 2);
  ll h = n - 2, w = n - 2;
  vl yoko(n - 1, n - 2), tate(n - 1, n - 2);
  while (q--) {
    ll t, x;
    cin >> t >> x;
    --t;
    if (t) {
      if (x - 2 < h) {
        for (int j = x - 1; j <= h; ++j) {
          tate[j] = w;
        }
        h = x - 2;
      }
      ans -= tate[x - 1];
    } else {
      if (x - 2 < w) {
        for (int j = x - 1; j <= w; ++j) {
          yoko[j] = h;
        }
        w = x - 2;
      }
      ans -= yoko[x - 1];
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = int(a); i < (int)b; ++i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vecctor<ll>>
#define vp vector<pair<ll, ll>>
#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  ll ans = (n - 2) * (n - 2);
  ll h = n - 2, w = n - 2;
  vl yoko(n - 1, n - 2), tate(n - 1, n - 2);
  while (q--) {
    ll t, x;
    cin >> t >> x;
    --t;
    if (t) {
      ans -= tate[x - 1];
      if (x - 2 < h) {
        for (int j = 1; j <= w; ++j) {
          yoko[j] = x - 2;
        }
        h = x - 2;
      }
    } else {
      ans -= yoko[x - 1];
      if (x - 2 < w) {
        for (int j = 1; j <= h; ++j) {
          tate[j] = x - 2;
        }
        w = x - 2;
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define int long long
#define maxn 200005
using namespace std;

const int INF = 1e9;

struct ZKW {
  vector<pair<int, int>> arr;
  int n;
  ZKW(int sz) : arr(sz * 2, make_pair(INF, 0)), n(sz) {}
  void upd(int x, pair<int, int> v) {
    for (arr[x += n] = v; x > 1; x >>= 1) {
      arr[x >> 1] = min(arr[x], arr[x ^ 1]);
    }
  }
  pair<int, int> ask(int l, int r) { // [l, r]
    pair<int, int> ret(INF, 0);
    r++;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        ret = min(ret, arr[l++]);
      if (r & 1)
        ret = min(ret, arr[--r]);
    }
    return ret;
  }
};

ZKW LR(maxn), BT(maxn);
int n, k;

int solve(int l, int r, int b, int t) {
  if (r < l || t < b)
    return 0;
  // printf("%lld %lld %lld %lld\\n", l, r, b, t);
  int ret = 0;
  auto A = b == 2 ? LR.ask(l, r) : make_pair(INF, 0LL);
  auto B = l == 2 ? BT.ask(b, t) : make_pair(INF, 0LL);
  // printf("%lld %lld\\n", A.second, B.second);
  if (min(A, B).first == INF)
    return (r - l + 1) * (t - b + 1);
  if (A < B) { // L, R
    ret += solve(l, A.second - 1, b, t);
    ret += solve(A.second + 1, r, b, t);
  } else { // B, T
    ret += solve(l, r, b, B.second - 1);
    ret += solve(l, r, B.second + 1, t);
  }
  return ret;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; i++) {
    int op, x;
    cin >> op >> x;
    if (op == 1)
      LR.upd(x, {i, x});
    else
      BT.upd(x, {i, x});
  }
  cout << solve(2, n - 1, 2, n - 1) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define int long long
using namespace std;

int n, k;

int solve(int l, int r, int b, int t, vector<pair<int, int>> &v) {
  // printf("%lld %lld %lld %lld\\n", l, r, b, t);
  if (r < l || t < b)
    return 0;
  if (v.empty()) {
    return (r - l + 1) * (t - b + 1);
  }
  int ret = 0;
  if (v[0].first == 1) { // l, r
    vector<pair<int, int>> L, R;
    for (int i = 1; i < SZ(v); i++) {
      if (v[i].second < v[0].second)
        L.push_back(v[i]);
      else
        R.push_back(v[i]);
    }
    ret += solve(l, v[0].second - 1, b, t, L);
    ret += solve(v[0].second + 1, r, b, t, R);
  } else { // b, t
    vector<pair<int, int>> B, T;
    for (int i = 1; i < SZ(v); i++) {
      if (v[i].first < v[0].first)
        B.push_back(v[i]);
      else
        T.push_back(v[i]);
    }
    ret += solve(l, r, b, v[0].first - 1, B);
    ret += solve(l, r, v[0].first + 1, t, T);
  }
  return ret;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; i++) {
    int op, x;
    cin >> op >> x;
    if (op == 1)
      v.emplace_back(1, x);
    else
      v.emplace_back(x, 1);
  }
  cout << solve(2, n - 1, 2, n - 1, v) << '\\n';
  return 0;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int N, Q;
  cin >> N >> Q;

  int64_t num = N - 2;
  num *= num;

  int d, n, f, s;
  f = 0;
  vector<int> V(1, N), H(1, N);

  for (int i = 0; i < Q; i++) {
    cin >> d >> n;

    if (f == 0) {
      f = d;
    }
    if (f == 1) {
      d = abs(d - 3);
    }

    if (d == 2) {
      s = H.size();
      int a = 0, b = s;

      while (a + 1 != b) {
        if (H.at((a + b) / 2) < n) {
          b = (a + b) / 2;
        } else {
          a = (a + b) / 2;
        }
      }

      if (b < s) {
        num -= V.at(b - 1) - 2;
      } else {
        if (s == V.size()) {
          num -= V.at(s - 1) - 2;
          H.push_back(n);
        } else {
          num -= V.at(s - 2) - 2;
          H.at(s - 1) = n;
        }
      }

    } else {
      s = V.size();
      int a = 0, b = s;

      while (a + 1 != b) {
        if (V.at((a + b) / 2) < n) {
          b = (a + b) / 2;
        } else {
          a = (a + b) / 2;
        }
      }

      if (b < s) {
        num -= H.at(b) - 2;
      } else {
        if (s + 1 == H.size()) {
          num -= H.at(s) - 2;
          V.push_back(n);
        } else {
          num -= H.at(s - 1) - 2;
          V.at(s - 1) = n;
        }
      }
    }
  }

  cout << num << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {

  int N, Q;
  cin >> N >> Q;

  int64_t num = N - 2;
  num *= num;

  int d, n, f, s;
  f = 0;
  vector<int> V(1, N), H(1, N);

  for (int i = 0; i < Q; i++) {
    cin >> d >> n;

    if (f == 0) {
      f = d;
    }
    if (f == 1) {
      d = abs(d - 3);
    }

    bool check = true;

    if (d == 2) {
      s = H.size();
      for (int i = 1; i < s; i++) {
        if (H.at(i) < n) {
          num -= V.at(i - 1) - 2;
          check = false;
          break;
        }
      }

      if (check) {
        if (s == V.size()) {
          num -= V.at(s - 1) - 2;
          H.push_back(n);
        } else {
          num -= V.at(s - 2) - 2;
          H.at(s - 1) = n;
        }
      }

    } else {
      s = V.size();
      for (int i = 1; i < s; i++) {
        if (V.at(i) < n) {
          num -= H.at(i) - 2;
          check = false;
          break;
        }
      }

      if (check) {
        if (s + 1 == H.size()) {
          num -= H.at(s) - 2;
          V.push_back(n);
        } else {
          num -= H.at(s - 1) - 2;
          V.at(s - 1) = n;
        }
      }
    }
  }

  cout << num << endl;

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

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

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 <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define REP(i, a, b) for (int i = int(a); i < (int)b; ++i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vecctor<ll>>
#define vp vector<pair<ll, ll>>
#define pb push_back
#define all(v) v.begin(), v.end()

using namespace std;
using ll = long long;
const ll INF = 1e18;
const double PI = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main(void) {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  ll n, q;
  cin >> n >> q;
  ll ans = (n - 2) * (n - 2);
  ll h = n - 2, w = n - 2;
  vl yoko(n - 1, n - 2), tate(n - 1, n - 2);
  rep(i, q) {
    ll t, x;
    cin >> t >> x;
    --t;
    if (t) {
      ans -= tate[x - 1];
      if (x - 2 < h) {
        for (int j = 1; j <= w; ++j) {
          yoko[j] = x - 2;
        }
        h = x - 2;
      }
    } else {
      ans -= yoko[x - 1];
      if (x - 2 < w) {
        for (int j = 1; j <= h; ++j) {
          tate[j] = x - 2;
        }
        w = x - 2;
      }
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int main() {
  ll n, ans, h, w, q, x, y;
  cin >> n >> q;
  vector<ll> a(n + 1), b(n + 1);
  ans = (n - 2) * (n - 2);
  for (int i = 0; i <= n; i++) {
    a[i] = b[i] = n;
  }
  h = w = n;
  while (q--) {
    cin >> x >> y;
    if (x == 1) {
      if (y < w) {
        ans -= h - 2;
        for (int j = y + 1; j < w; j++) {
          b[j] = h;
        }
        w = y;
      } else {
        ans -= b[y] - 2;
      }
    } else {
      if (y < h) {
        ans -= w - 2;
        for (int j = y + 1; j < h; j++) {
          a[j] = w;
        }
        h = y;
      } else {
        ans -= a[y] - 2;
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>

int main() {
  ll n, ans, h, w, q, x, y;
  cin >> n >> q;
  vector<ll> a(n + 1), b(n + 1);
  ans = (n - 2) * (n - 2);
  for (int i = 0; i <= n; i++) {
    a[i] = b[i] = n;
  }
  h = w = n;
  while (q--) {
    cin >> x >> y;
    if (x == 1) {
      ans -= a[y] - 2;
      if (y < w) {
        for (int j = 1; j <= h; j++) {
          b[j] = y;
        }
        w = y;
      }
    } else {
      ans -= b[y] - 2;
      if (y < h) {
        for (int j = 1; j <= w; j++) {
          a[j] = y;
        }
        h = y;
      }
    }
  }
  cout << ans << endl;
}
Problem p02551 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

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

  vector<int64_t> Ws(N + 1, N);
  vector<int64_t> Hs(N + 1, N);
  int64_t black = (N - 2) * (N - 2);
  int64_t W = N, H = N;
  for (int64_t q = 0; q < Q; ++q) {
    int64_t type, x;
    cin >> type >> x;
    if (type == 1) {
      if (x < W) {
        for (int64_t c = x; c < W; ++c) {
          Hs[c] = H;
        }
        W = x;
      }
      black -= Hs[x] - 2;
    } else if (type == 2) {
      if (x < H) {
        for (int64_t r = x; r < H; ++r) {
          Ws[r] = W;
        }
        H = x;
      }
      black -= Ws[x] - 2;
    }
  }
  cout << black << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

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

  vector<int64_t> Ws(N + 1, N);
  vector<int64_t> Hs(N + 1, N);
  int64_t black = (N - 2) * (N - 2);
  int64_t W = N, H = N;
  for (int64_t q = 0; q < Q; ++q) {
    int64_t type, x;
    cin >> type >> x;
    if (type == 1) {
      if (x < W) {
        for (int64_t r = 0; r < Hs[x]; ++r) {
          Ws[r] = x;
        }
        W = x;
      }
      black -= Hs[x] - 2;
    } else if (type == 2) {
      if (x < H) {
        for (int64_t c = 0; c < Ws[x]; ++c) {
          Hs[c] = x;
        }
        H = x;
      }
      black -= Ws[x] - 2;
    }
  }
  cout << black << endl;

  return 0;
}