Problem p02956 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 998244353;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

vector<ll> compress(vector<ll> &vec) {
  vector<ll> v = vec;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  vector<ll> ret;
  rep(i, vec.size()) {
    ret.push_back(find(v.begin(), v.end(), vec[i]) - v.begin());
  }
  return ret;
}

#define MAX_N (1 << 18) // 260000
template <typename T> struct segtree {
  int n;
  T dat[2 * MAX_N - 1];
  T cnt[2 * MAX_N - 1];

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }

  void update(int k, T a) {
    k += n - 1;
    dat[k] += a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
    }
  }

  //(a,b,0,0,seg.n)で呼ぶ
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
};

int n;
vector<ll> X, Y;
vector<ll> x, y;
vector<pl> p;
segtree<ll> seg;
ll block[200000][4];
ll po2[200001];

int main() {
  cin >> n;
  po2[0] = 1;
  for (int i = 1; i <= n; i++)
    po2[i] = po2[i - 1] * 2 % MOD;
  rep(i, n) {
    ll tx, ty;
    cin >> tx >> ty;
    X.push_back(tx);
    Y.push_back(ty);
  }
  x = compress(X);
  y = compress(Y);
  rep(i, n) p.push_back(pl(y[i], x[i]));
  sort(p.begin(), p.end());
  seg.init(p.size());
  rep(i, p.size()) {
    block[i][0] = seg.query(0, p[i].second, 0, 0, seg.n);
    block[i][1] = i - block[i][0];
    seg.update(p[i].second, 1);
  }
  reverse(p.begin(), p.end());
  seg.init(p.size());
  rep(i, p.size()) {
    block[p.size() - 1 - i][3] = seg.query(0, p[i].second, 0, 0, seg.n);
    block[p.size() - 1 - i][2] = i - block[p.size() - 1 - i][3];
    seg.update(p[i].second, 1);
  }
  ll ans = 0;
  rep(i, n) {
    ll add = po2[n] - 1;
    rep(j, 4) add =
        (add - po2[block[i][j] + block[i][(j + 1) % 4]] + 1 + 2 * MOD) % MOD;
    rep(j, 4) add = (add + (po2[block[i][j]] - 1)) % MOD;
    (ans += add) %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 998244353;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

vector<ll> compress(vector<ll> &vec) {
  vector<ll> v = vec;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  vector<ll> ret;
  rep(i, vec.size()) {
    ret.push_back(lower_bound(v.begin(), v.end(), vec[i]) - v.begin());
  }
  return ret;
}

#define MAX_N (1 << 18) // 260000
template <typename T> struct segtree {
  int n;
  T dat[2 * MAX_N - 1];
  T cnt[2 * MAX_N - 1];

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++)
      dat[i] = 0;
  }

  void update(int k, T a) {
    k += n - 1;
    dat[k] += a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
    }
  }

  //(a,b,0,0,seg.n)で呼ぶ
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
};

int n;
vector<ll> X, Y;
vector<ll> x, y;
vector<pl> p;
segtree<ll> seg;
ll block[200000][4];
ll po2[200001];

int main() {
  cin >> n;
  po2[0] = 1;
  for (int i = 1; i <= n; i++)
    po2[i] = po2[i - 1] * 2 % MOD;
  rep(i, n) {
    ll tx, ty;
    cin >> tx >> ty;
    X.push_back(tx);
    Y.push_back(ty);
  }
  x = compress(X);
  y = compress(Y);
  rep(i, n) p.push_back(pl(y[i], x[i]));
  sort(p.begin(), p.end());
  seg.init(p.size());
  rep(i, p.size()) {
    block[i][0] = seg.query(0, p[i].second, 0, 0, seg.n);
    block[i][1] = i - block[i][0];
    seg.update(p[i].second, 1);
  }
  reverse(p.begin(), p.end());
  seg.init(p.size());
  rep(i, p.size()) {
    block[p.size() - 1 - i][3] = seg.query(0, p[i].second, 0, 0, seg.n);
    block[p.size() - 1 - i][2] = i - block[p.size() - 1 - i][3];
    seg.update(p[i].second, 1);
  }
  ll ans = 0;
  rep(i, n) {
    ll add = po2[n] - 1;
    rep(j, 4) add =
        (add - po2[block[i][j] + block[i][(j + 1) % 4]] + 1 + 2 * MOD) % MOD;
    rep(j, 4) add = (add + (po2[block[i][j]] - 1)) % MOD;
    (ans += add) %= MOD;
  }
  cout << ans << endl;
}