Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

const ll MODBASE = 998244353LL;
const int MAXN = 100010;
const int MAXM = 200010;
const int MAXK = 110;
const int MAXQ = 200010;

int n;
II a[MAXN];
set<int> s;
vector<int> b;
int t[MAXN * 4];
ll mu[MAXN], res[MAXN];

int bs(int u) {
  int dau = 0, cuoi = SZ(b) - 1;
  while (dau <= cuoi) {
    int mid = (dau + cuoi) >> 1;
    if (b[mid] > u)
      cuoi = mid - 1;
    else
      dau = mid + 1;
  }
  return cuoi;
}

void update(int k, int l, int r, int u) {
  if (l > r || r < u || u < l)
    return;
  if (l == r) {
    t[k] = 1;
    return;
  }
  int m = (l + r) >> 1;
  if (u <= m)
    update(k * 2, l, m, u);
  else
    update(k * 2 + 1, m + 1, r, u);
  t[k] = t[k * 2] + t[k * 2 + 1];
}

int get(int k, int l, int r, int u, int v) {
  if (r < u || v < l || l > r)
    return 0;
  if (u <= l && r <= v)
    return t[k];
  int m = (l + r) >> 1;
  return get(k * 2, l, m, u, v) + get(k * 2 + 1, m + 1, r, u, v);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n) cin >> a[i].FI >> a[i].SE;
  sort(a + 1, a + n + 1);

  // FOR(i,1,n) cout << a[i].FI << ' ' << a[i].SE << endl;

  FOR(i, 1, n) b.emplace_back(a[i].SE);
  sort(ALL(b));
  FOR(i, 1, n) a[i].SE = bs(a[i].SE);

  // FOR(i,1,n) cout << a[i].FI << ' ' << a[i].SE << endl;

  MS(t, 0);
  mu[0] = 1;
  FOR(i, 1, n) mu[i] = mu[i - 1] * 2 % MODBASE;
  FOR(i, 1, n) res[i] = (mu[n] - 1 + MODBASE) % MODBASE;
  FOR(i, 1, n) {
    int lef = i - 1;
    int upperLeft = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lowerLeft = lef - upperLeft;
    // cout << i << ' ' << upperLeft << ' ' << lowerLeft << endl;
    res[i] = (res[i] + mu[upperLeft] + mu[lowerLeft]) % MODBASE;
    update(1, 0, n - 1, a[i].SE);
  }
  MS(t, 0);
  FORE(i, n, 1) {
    int rig = n - i;
    int upperRight = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lowerRight = rig - upperRight;
    // cout << i << ' ' << upperRight << ' ' << lowerRight << endl;
    res[i] = (res[i] + mu[upperRight] + mu[lowerRight]) % MODBASE;
    update(1, 0, n - 1, a[i].SE);
  }
  FOR(i, 1, n) {
    int upper = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lower = n - upper - 1;

    int rig = n - i;
    int lef = i - 1;
    // cout << upper << ' ' << lower << ' ' << lef << ' ' << rig << endl;
    ll p = (mu[upper] + mu[lower] + mu[rig] + mu[lef]) % MODBASE;
    res[i] = (res[i] - p + MODBASE) % MODBASE;
  }
  ll kq = 0;
  // FOR(i,1,n) cout << res[i] << ' ';
  // cout << endl;
  FOR(i, 1, n) kq = (kq + res[i]) % MODBASE;
  cout << kq;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, int> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

const ll MODBASE = 998244353LL;
const int MAXN = 200010;
const int MAXM = 200010;
const int MAXK = 110;
const int MAXQ = 200010;

int n;
II a[MAXN];
set<int> s;
vector<int> b;
int t[MAXN * 4];
ll mu[MAXN], res[MAXN];

int bs(int u) {
  int dau = 0, cuoi = SZ(b) - 1;
  while (dau <= cuoi) {
    int mid = (dau + cuoi) >> 1;
    if (b[mid] > u)
      cuoi = mid - 1;
    else
      dau = mid + 1;
  }
  return cuoi;
}

void update(int k, int l, int r, int u) {
  if (l > r || r < u || u < l)
    return;
  if (l == r) {
    t[k] = 1;
    return;
  }
  int m = (l + r) >> 1;
  if (u <= m)
    update(k * 2, l, m, u);
  else
    update(k * 2 + 1, m + 1, r, u);
  t[k] = t[k * 2] + t[k * 2 + 1];
}

int get(int k, int l, int r, int u, int v) {
  if (r < u || v < l || l > r)
    return 0;
  if (u <= l && r <= v)
    return t[k];
  int m = (l + r) >> 1;
  return get(k * 2, l, m, u, v) + get(k * 2 + 1, m + 1, r, u, v);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n) cin >> a[i].FI >> a[i].SE;
  sort(a + 1, a + n + 1);

  // FOR(i,1,n) cout << a[i].FI << ' ' << a[i].SE << endl;

  FOR(i, 1, n) b.emplace_back(a[i].SE);
  sort(ALL(b));
  FOR(i, 1, n) a[i].SE = bs(a[i].SE);

  // FOR(i,1,n) cout << a[i].FI << ' ' << a[i].SE << endl;

  MS(t, 0);
  mu[0] = 1;
  FOR(i, 1, n) mu[i] = mu[i - 1] * 2 % MODBASE;
  FOR(i, 1, n) res[i] = (mu[n] - 1 + MODBASE) % MODBASE;
  FOR(i, 1, n) {
    int lef = i - 1;
    int upperLeft = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lowerLeft = lef - upperLeft;
    // cout << i << ' ' << upperLeft << ' ' << lowerLeft << endl;
    res[i] = (res[i] + mu[upperLeft] + mu[lowerLeft]) % MODBASE;
    update(1, 0, n - 1, a[i].SE);
  }
  MS(t, 0);
  FORE(i, n, 1) {
    int rig = n - i;
    int upperRight = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lowerRight = rig - upperRight;
    // cout << i << ' ' << upperRight << ' ' << lowerRight << endl;
    res[i] = (res[i] + mu[upperRight] + mu[lowerRight]) % MODBASE;
    update(1, 0, n - 1, a[i].SE);
  }
  FOR(i, 1, n) {
    int upper = get(1, 0, n - 1, a[i].SE + 1, n - 1);
    int lower = n - upper - 1;

    int rig = n - i;
    int lef = i - 1;
    // cout << upper << ' ' << lower << ' ' << lef << ' ' << rig << endl;
    ll p = (mu[upper] + mu[lower] + mu[rig] + mu[lef]) % MODBASE;
    res[i] = (res[i] - p + MODBASE) % MODBASE;
  }
  ll kq = 0;
  // FOR(i,1,n) cout << res[i] << ' ';
  // cout << endl;
  FOR(i, 1, n) kq = (kq + res[i]) % MODBASE;
  cout << kq;
  return 0;
}