Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 1e5 + 7;
const int MOD = 998244353;

int add(int a, int b) { return (a + b) % MOD; }

int sub(int a, int b) { return (a - b + MOD) % MOD; }

int mul(int a, int b) { return (a * b) % MOD; }

int pw[N];

void init() {
  pw[0] = 1;
  for (int i = 1; i < N; i++) {
    pw[i] = mul(pw[i - 1], 2);
  }
}

int cnt_bad(vector<int> p) {
  int ans = 0;
  for (auto t : p) {
    ans = add(ans, pw[t] - 1);
  }
  for (int i = 0; i < 4; i++) {
    int c = pw[p[i] + p[(i + 1) % 4]];
    c = sub(c, pw[p[i]]);
    c = sub(c, pw[p[(i + 1) % 4]]);
    c = add(c, 1);
    ans = add(ans, c);
  }
  return ans;
}

struct Fenw {
  int n;
  vector<int> f;

  void add(int x, int c) {
    for (int i = x; i < n; i = i | (i + 1)) {
      f[i] += c;
    }
  }

  int sum(int x) {
    int ans = 0;
    for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
      ans += f[i];
    }
    return ans;
  }

  Fenw(int n_) {
    n = n_;
    f.resize(n);
  }
};

void zip(vector<int> &a) {
  vector<int> sn = a;
  sort(sn.begin(), sn.end());
  sn.resize(unique(sn.begin(), sn.end()) - sn.begin());
  for (auto &t : a) {
    t = lower_bound(sn.begin(), sn.end(), t) - sn.begin();
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  init();
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  zip(x);
  zip(y);
  vector<int> ind(n);
  iota(ind.begin(), ind.end(), 0);
  sort(ind.begin(), ind.end(), [&](int i, int j) { return x[i] < x[j]; });
  Fenw lp(n);
  Fenw rp(n);
  for (auto t : ind) {
    rp.add(y[t], 1);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0)
      lp.add(y[ind[i - 1]], 1);
    rp.add(y[ind[i]], -1);
    vector<int> p(4);
    p[1] = lp.sum(y[ind[i]]);
    p[0] = i - p[1];
    p[2] = rp.sum(y[ind[i]]);
    p[3] = (n - i - 1) - p[2];
    {
      int all = sub(pw[n], 1);
      int none = cnt_bad(p);
      all = sub(all, none);
      ans = add(ans, all);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

const int N = 2e5 + 7;
const int MOD = 998244353;

int add(int a, int b) { return (a + b) % MOD; }

int sub(int a, int b) { return (a - b + MOD) % MOD; }

int mul(int a, int b) { return (a * b) % MOD; }

int pw[N];

void init() {
  pw[0] = 1;
  for (int i = 1; i < N; i++) {
    pw[i] = mul(pw[i - 1], 2);
  }
}

int cnt_bad(vector<int> p) {
  int ans = 0;
  for (auto t : p) {
    ans = add(ans, pw[t] - 1);
  }
  for (int i = 0; i < 4; i++) {
    int c = pw[p[i] + p[(i + 1) % 4]];
    c = sub(c, pw[p[i]]);
    c = sub(c, pw[p[(i + 1) % 4]]);
    c = add(c, 1);
    ans = add(ans, c);
  }
  return ans;
}

struct Fenw {
  int n;
  vector<int> f;

  void add(int x, int c) {
    for (int i = x; i < n; i = i | (i + 1)) {
      f[i] += c;
    }
  }

  int sum(int x) {
    int ans = 0;
    for (int i = x; i >= 0; i = (i & (i + 1)) - 1) {
      ans += f[i];
    }
    return ans;
  }

  Fenw(int n_) {
    n = n_;
    f.resize(n);
  }
};

void zip(vector<int> &a) {
  vector<int> sn = a;
  sort(sn.begin(), sn.end());
  sn.resize(unique(sn.begin(), sn.end()) - sn.begin());
  for (auto &t : a) {
    t = lower_bound(sn.begin(), sn.end(), t) - sn.begin();
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  init();
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> y(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  zip(x);
  zip(y);
  vector<int> ind(n);
  iota(ind.begin(), ind.end(), 0);
  sort(ind.begin(), ind.end(), [&](int i, int j) { return x[i] < x[j]; });
  Fenw lp(n);
  Fenw rp(n);
  for (auto t : ind) {
    rp.add(y[t], 1);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > 0)
      lp.add(y[ind[i - 1]], 1);
    rp.add(y[ind[i]], -1);
    vector<int> p(4);
    p[1] = lp.sum(y[ind[i]]);
    p[0] = i - p[1];
    p[2] = rp.sum(y[ind[i]]);
    p[3] = (n - i - 1) - p[2];
    {
      int all = sub(pw[n], 1);
      int none = cnt_bad(p);
      all = sub(all, none);
      ans = add(ans, all);
    }
  }
  cout << ans << endl;
}