Problem p02956 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using ll = long long int;
using P = std::pair<int, int>;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rag(con) std::begin(con), std::end(con)

constexpr ll mod = 998244353;

ll pow(ll x, ll m) {

  ll re = 1, y = 1;

  for (int i = 0; i < 60; ++i) {
    if (m & y)
      re = re * x % mod;
    x = x * x % mod;
    y *= 2;
  }

  return re;
}

ll inv(ll x) { return pow(x, mod - 2); }

class mint {
  ll num = 0;

public:
  mint(ll x = 0) : num(x) {}

  mint operator+(const mint &rhs) const { return (num + rhs.num) % mod; }
  mint operator*(const mint &rhs) const { return num * rhs.num % mod; }
  mint operator-(const mint &rhs) const { return (num - rhs.num + mod) % mod; }
  mint operator/(const mint &rhs) const { return num * inv(rhs.num) % mod; }
  mint operator+=(const mint &rhs) { return *this = *this + rhs; }
  mint operator*=(const mint &rhs) { return *this = *this * rhs; }
  mint operator-=(const mint &rhs) { return *this = *this - rhs; }
  mint operator/=(const mint &rhs) { return *this = *this / rhs; }
  friend std::ostream &operator<<(std::ostream &os, const mint &rhs);
};

std::ostream &operator<<(std::ostream &os, const mint &rhs) {

  os << rhs.num;

  return os;
}

int m;

std::vector<ll> node;

void debug() {

  int x = 0;

  rep(i, 2 * m - 1) {
    std::cerr << node[i] << ' ';
    if (x == i) {
      std::cerr << std::endl;
      x = x * 2 + 2;
    }
  }
}

void init(int n) {

  m = 1;

  while (m < n)
    m *= 2;

  node.resize(m * 2 - 1);
}

void add(int y) {
  y += m - 1;
  node[y]++;

  while (y > 0) {
    y = (y - 1) / 2;
    node[y]++;
  }
}

int get(int a, int b, int k = 0, int l = 0, int r = m) {

  if (r <= a || b <= l)
    return 0;

  if (a <= l && r <= b)
    return node[k];

  int lv = get(a, b, 2 * k + 1, l, (l + r) / 2);
  int rv = get(a, b, 2 * k + 2, (l + r) / 2, r);

  return lv + rv;
}

int main() {
  int n;

  std::cin >> n;

  init(n);

  std::vector<P> vec;
  std::vector<int> xs, ys;

  rep(i, n) {
    int x, y;
    std::cin >> x >> y;
    xs.push_back(x);
    ys.push_back(y);
    vec.emplace_back(x, y);
  }

  std::sort(rag(xs));
  std::sort(rag(ys));

  rep(i, n) {
    vec[i].first = std::lower_bound(rag(xs), vec[i].first) - std::begin(xs);
    vec[i].second = std::lower_bound(rag(ys), vec[i].second) - std::begin(ys);
  }

  std::sort(rag(vec));

  std::vector<int> a(n), b(n), c(n), d(n);

  // std::cerr << "A&C" << std::endl;

  rep(i, n) {
    int ny = vec[i].second;
    a[i] = get(ny + 1, n);
    c[i] = get(0, ny);
    add(ny);
    // std::cerr << i << std::endl;
    // debug();
  }

  rep(i, 2 * m - 1) node[i] = 0;

  // std::cerr << "B&D" << std::endl;

  for (int i = n - 1; i >= 0; --i) {
    int ny = vec[i].second;
    b[i] = get(ny + 1, n);
    d[i] = get(0, ny);
    add(ny);
    // debug();
  }

  // std::cerr << "DEBUG" << std::endl;

  rep(i, n) {
    std::cerr << a[i] << ' ' << b[i] << ' ' << c[i] << ' ' << d[i] << std::endl;
  }

  mint ans = 0;

  std::vector<mint> p2(n + 1);

  p2[0] = 1;

  rep(i, n) { p2[i + 1] = p2[i] * 2; }

  rep(i, n) {
    ans += p2[n] - 1 + p2[a[i]] + p2[b[i]] + p2[c[i]] + p2[d[i]];
    ans -=
        p2[a[i] + b[i]] + p2[d[i] + b[i]] + p2[d[i] + c[i]] + p2[a[i] + c[i]];
  }

  std::cout << ans << std::endl;

  return 0;
}

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

using ll = long long int;
using P = std::pair<int, int>;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rag(con) std::begin(con), std::end(con)

constexpr ll mod = 998244353;

ll pow(ll x, ll m) {

  ll re = 1, y = 1;

  for (int i = 0; i < 60; ++i) {
    if (m & y)
      re = re * x % mod;
    x = x * x % mod;
    y *= 2;
  }

  return re;
}

ll inv(ll x) { return pow(x, mod - 2); }

class mint {
  ll num = 0;

public:
  mint(ll x = 0) : num(x) {}

  mint operator+(const mint &rhs) const { return (num + rhs.num) % mod; }
  mint operator*(const mint &rhs) const { return num * rhs.num % mod; }
  mint operator-(const mint &rhs) const { return (num - rhs.num + mod) % mod; }
  mint operator/(const mint &rhs) const { return num * inv(rhs.num) % mod; }
  mint operator+=(const mint &rhs) { return *this = *this + rhs; }
  mint operator*=(const mint &rhs) { return *this = *this * rhs; }
  mint operator-=(const mint &rhs) { return *this = *this - rhs; }
  mint operator/=(const mint &rhs) { return *this = *this / rhs; }
  friend std::ostream &operator<<(std::ostream &os, const mint &rhs);
};

std::ostream &operator<<(std::ostream &os, const mint &rhs) {

  os << rhs.num;

  return os;
}

int m;

std::vector<ll> node;

void debug() {

  int x = 0;

  rep(i, 2 * m - 1) {
    std::cerr << node[i] << ' ';
    if (x == i) {
      std::cerr << std::endl;
      x = x * 2 + 2;
    }
  }
}

void init(int n) {

  m = 1;

  while (m < n)
    m *= 2;

  node.resize(m * 2 - 1);
}

void add(int y) {
  y += m - 1;
  node[y]++;

  while (y > 0) {
    y = (y - 1) / 2;
    node[y]++;
  }
}

int get(int a, int b, int k = 0, int l = 0, int r = m) {

  if (r <= a || b <= l)
    return 0;

  if (a <= l && r <= b)
    return node[k];

  int lv = get(a, b, 2 * k + 1, l, (l + r) / 2);
  int rv = get(a, b, 2 * k + 2, (l + r) / 2, r);

  return lv + rv;
}

int main() {
  int n;

  std::cin >> n;

  init(n);

  std::vector<P> vec;
  std::vector<int> xs, ys;

  rep(i, n) {
    int x, y;
    std::cin >> x >> y;
    xs.push_back(x);
    ys.push_back(y);
    vec.emplace_back(x, y);
  }

  std::sort(rag(xs));
  std::sort(rag(ys));

  rep(i, n) {
    vec[i].first = std::lower_bound(rag(xs), vec[i].first) - std::begin(xs);
    vec[i].second = std::lower_bound(rag(ys), vec[i].second) - std::begin(ys);
  }

  std::sort(rag(vec));

  std::vector<int> a(n), b(n), c(n), d(n);

  // std::cerr << "A&C" << std::endl;

  rep(i, n) {
    int ny = vec[i].second;
    a[i] = get(ny + 1, n);
    c[i] = get(0, ny);
    add(ny);
    // std::cerr << i << std::endl;
    // debug();
  }

  rep(i, 2 * m - 1) node[i] = 0;

  // std::cerr << "B&D" << std::endl;

  for (int i = n - 1; i >= 0; --i) {
    int ny = vec[i].second;
    b[i] = get(ny + 1, n);
    d[i] = get(0, ny);
    add(ny);
    // debug();
  }

  // std::cerr << "DEBUG" << std::endl;

  /*rep(i, n)
  {
      std::cerr << a[i] << ' ' << b[i] << ' ' << c[i] << ' ' << d[i] <<
  std::endl;
  }*/

  mint ans = 0;

  std::vector<mint> p2(n + 1);

  p2[0] = 1;

  rep(i, n) { p2[i + 1] = p2[i] * 2; }

  rep(i, n) {
    ans += p2[n] - 1 + p2[a[i]] + p2[b[i]] + p2[c[i]] + p2[d[i]];
    ans -=
        p2[a[i] + b[i]] + p2[d[i] + b[i]] + p2[d[i] + c[i]] + p2[a[i] + c[i]];
  }

  std::cout << ans << std::endl;

  return 0;
}