Problem p02551 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long
#define counter(m) for (int i = 0; i < m; i++)
#define gcd __gcd
#define endl "\n"
#define pb emplace_back
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define mod2 998244353
#define maxe *max_element
#define mine *min_element
#define inf LLONG_MAX
#define deci(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nitin                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define PI 3.141592653589793238
using namespace std;

struct segte {
  int n;
  int *a;
  int *tree;
  int *lazy;
  void init(int N, const int *A = nullptr) {
    n = N;
    a = new int[n + 1];
    lazy = new int[5 * (n + 1) + 1];
    tree = new int[5 * (n + 1) + 1];
    for (int i = 0; i < 5 * (n + 1); i++) {
      lazy[i] = inf;
      tree[i] = inf;
    }
    for (int i = 1; i <= n; i++)
      a[i] = n - 1;
  }
  void build(int index, int ss, int se) {
    if (ss == se) {
      tree[index] = a[ss];
      return;
    }
    int mid = (ss + se) / 2;
    build(2 * index, ss, mid);
    build(2 * index + 1, mid + 1, se);
    tree[index] = min(tree[2 * index], tree[2 * index + 1]);
  }
  void build() { build(1, 1, n); }

  void update_range(int index, int ss, int se, int qs, int qe, int val) {
    if (lazy[index] != inf) {
      tree[index] = min(tree[index], lazy[index]);
      if (ss != se) {
        lazy[2 * index] = min(lazy[2 * index], lazy[index]);
        lazy[2 * index + 1] = min(lazy[2 * index + 1], lazy[index]);
      }
      lazy[index] = inf;
    }
    if (ss > qe || se < qs)
      return;
    if (ss >= qs && se <= qe) {
      tree[index] = min(tree[index], val);
      lazy[2 * index] = min(lazy[2 * index], val);
      lazy[2 * index + 1] = min(lazy[2 * index + 1], val);
      return;
    }
    int mid = (ss + se) / 2;
    update_range(2 * index, ss, mid, qs, qe, val);
    update_range(2 * index + 1, mid + 1, se, qs, qe, val);
    tree[index] = min(tree[2 * index], tree[2 * index + 1]);
  }
  void update_range(int qs, int qe, int val) {
    update_range(1, 1, n, qs, qe, val);
  }

  int get(int index, int ss, int se, int idx) {

    if (lazy[index] != inf) {
      tree[index] = min(tree[index], lazy[index]);
      if (ss != se) {
        lazy[2 * index] = min(lazy[2 * index], lazy[index]);
        lazy[2 * index + 1] = min(lazy[2 * index + 1], lazy[index]);
      }
      lazy[index] = inf;
    }

    if (ss == se) {
      // cout<<index<<" "<<tree[index]<<endl;
      return tree[index];
    }
    int mid = (ss + se) / 2;
    if (idx <= mid)
      return get(2 * index, ss, mid, idx);
    else
      return get(2 * index + 1, mid + 1, se, idx);
  }

  int get(int idx) { return get(1, 1, n, idx); }
};

int32_t main() {
  nitin;
  int n;
  cin >> n;
  segte x;
  x.init(n);
  x.build();
  segte y;
  y.init(n);
  y.build();
  int black = n * n - (n + n + n - 2 + n - 2);
  int q;
  cin >> q;
  //    for(int i=1;i<=n;i++)
  //        cout<<x.get(i)<<" ";
  //    cout<<endl;
  //    for(int i=1;i<=n;i++)
  //        cout<<y.get(i)<<" ";
  //    cout<<endl;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int col;
      cin >> col;
      int val = y.get(col);
      if (val > 1) {
        black -= (val - 1);
      }
      if (val != 0)
        x.update_range(1, val, col - 1);
      //            for(int i=1;i<=n;i++)
      //                cout<<x.get(i)<<" ";
      //            cout<<endl;
    } else {
      int row;
      cin >> row;
      int val = x.get(row);
      if (val > 1) {
        black -= (val - 1);
      }
      if (val != 0)
        y.update_range(1, val, row - 1);
      //            for(int i=1;i<=n;i++)
      //                cout<<y.get(i)<<" ";
      //            cout<<endl;
    }
  }
  cout << black << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long
#define counter(m) for (int i = 0; i < m; i++)
#define gcd __gcd
#define endl "\n"
#define pb emplace_back
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define mod2 998244353
#define maxe *max_element
#define mine *min_element
#define inf LLONG_MAX
#define deci(x, y) fixed << setprecision(y) << x
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define nitin                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define PI 3.141592653589793238
using namespace std;

struct segte {
  int n;
  int *a;
  int *tree;
  int *lazy;
  void init(int N, const int *A = nullptr) {
    n = N;
    a = new int[n + 1];
    lazy = new int[5 * (n + 1) + 1];
    tree = new int[5 * (n + 1) + 1];
    for (int i = 0; i < 5 * (n + 1); i++) {
      lazy[i] = inf;
      tree[i] = inf;
    }
    for (int i = 1; i <= n; i++)
      a[i] = n - 1;
  }
  void build(int index, int ss, int se) {
    if (ss == se) {
      tree[index] = a[ss];
      return;
    }
    int mid = (ss + se) / 2;
    build(2 * index, ss, mid);
    build(2 * index + 1, mid + 1, se);
    tree[index] = min(tree[2 * index], tree[2 * index + 1]);
  }
  void build() { build(1, 1, n); }

  void update_range(int index, int ss, int se, int qs, int qe, int val) {
    if (lazy[index] != inf) {
      tree[index] = min(tree[index], lazy[index]);
      if (ss != se) {
        lazy[2 * index] = min(lazy[2 * index], lazy[index]);
        lazy[2 * index + 1] = min(lazy[2 * index + 1], lazy[index]);
      }
      lazy[index] = inf;
    }
    if (ss > qe || se < qs)
      return;
    if (ss >= qs && se <= qe) {
      tree[index] = min(tree[index], val);
      if (ss != se) {
        lazy[2 * index] = min(lazy[2 * index], val);
        lazy[2 * index + 1] = min(lazy[2 * index + 1], val);
      }
      return;
    }
    int mid = (ss + se) / 2;
    update_range(2 * index, ss, mid, qs, qe, val);
    update_range(2 * index + 1, mid + 1, se, qs, qe, val);
    tree[index] = min(tree[2 * index], tree[2 * index + 1]);
  }
  void update_range(int qs, int qe, int val) {
    update_range(1, 1, n, qs, qe, val);
  }

  int get(int index, int ss, int se, int idx) {

    if (lazy[index] != inf) {
      tree[index] = min(tree[index], lazy[index]);
      if (ss != se) {
        lazy[2 * index] = min(lazy[2 * index], lazy[index]);
        lazy[2 * index + 1] = min(lazy[2 * index + 1], lazy[index]);
      }
      lazy[index] = inf;
    }

    if (ss == se) {
      // cout<<index<<" "<<tree[index]<<endl;
      return tree[index];
    }
    int mid = (ss + se) / 2;
    if (idx <= mid)
      return get(2 * index, ss, mid, idx);
    else
      return get(2 * index + 1, mid + 1, se, idx);
  }

  int get(int idx) { return get(1, 1, n, idx); }
};

int32_t main() {
  nitin;
  int n;
  cin >> n;
  segte x;
  x.init(n);
  x.build();
  segte y;
  y.init(n);
  y.build();
  int black = n * n - (n + n + n - 2 + n - 2);
  int q;
  cin >> q;
  //    for(int i=1;i<=n;i++)
  //        cout<<x.get(i)<<" ";
  //    cout<<endl;
  //    for(int i=1;i<=n;i++)
  //        cout<<y.get(i)<<" ";
  //    cout<<endl;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int col;
      cin >> col;
      int val = y.get(col);
      if (val > 1) {
        black -= (val - 1);
      }
      if (val != 0)
        x.update_range(1, val, col - 1);
      //            for(int i=1;i<=n;i++)
      //                cout<<x.get(i)<<" ";
      //            cout<<endl;
    } else {
      int row;
      cin >> row;
      int val = x.get(row);
      if (val > 1) {
        black -= (val - 1);
      }
      if (val != 0)
        y.update_range(1, val, row - 1);
      //            for(int i=1;i<=n;i++)
      //                cout<<y.get(i)<<" ";
      //            cout<<endl;
    }
  }
  cout << black << endl;
  return 0;
}