Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100, mod = 998244353;
void plusle(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return;
}
void minun(int &a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
  return;
}
int add(int a, int b) {
  a += b;
  return a >= mod ? a - mod : a;
}
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + mod : a;
}
int mul(int a, int b) { return (int)((long long)a * b % mod); }
struct BIT {
  int bit[maxn];
  int n;
  BIT(int _n) { n = _n + 1; }
  void update(int idx, int val) {
    for (; idx < n; idx += (idx & (-idx)))
      bit[idx] += val;
    return;
  }
  int get(int idx) {
    int sum = 0;
    for (; idx > 0; idx -= (idx & (-idx)))
      sum += bit[idx];
    return sum;
  }
  int get(int l, int r) { return get(r) - get(l - 1); }
};
struct pt {
  int x, y;
  pt() : x(0), y(0) {}
  void read() { scanf("%d%d", &x, &y); }
  friend bool operator<(const pt &a, const pt &b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
  }
} p[maxn];
vector<int> ss, sss;
int ans, n;
int p2[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    p[i].read();
    ss.push_back(p[i].x);
    sss.push_back(p[i].y);
  }
  ss.push_back((int)(-1e9 - 7));
  sss.push_back((int)(-1e9 - 7));
  sort(ss.begin(), ss.end());
  sort(sss.begin(), sss.end());
  ss.erase(unique(ss.begin(), ss.end()), ss.end());
  sss.erase(unique(sss.begin(), sss.end()), sss.end());
  for (int i = 1; i <= n; i++) {
    p[i].x = (int)(lower_bound(ss.begin(), ss.end(), p[i].x) - ss.begin());
    p[i].y = (int)(lower_bound(sss.begin(), sss.end(), p[i].y) - sss.begin());
  }
  p2[0] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i] = add(p2[i - 1], p2[i - 1]);
  }
  sort(p + 1, p + 1 + n);
  int cur = 0, u1, u2, d1, d2;
  BIT f1(n), f2(n);
  for (int i = 1; i <= n; i++)
    f1.update(p[i].y, 1);
  for (int i = 1; i <= n; i++) {
    int j = i, ct = 0;
    while (p[j].x == p[i].x && j <= n) {
      f1.update(p[j].y, -1);
      ct++;
      j++;
    }
    j--;
    for (int k = i; k <= j; k++) {
      u1 = f1.get(p[k].y + 1, n), u2 = f2.get(p[k].y + 1, n);
      d1 = f1.get(p[k].y - 1), d2 = f2.get(p[k].y - 1);
      plusle(ans, p2[n] - 1);
      /// completely above
      cur = u1 + u2 + (j - k);
      minun(ans, p2[cur] - 1);
      /// completely below
      cur = d1 + d2 + (k - i);
      minun(ans, p2[cur] - 1);
      /// completely left
      minun(ans, p2[i - 1] - 1);
      /// completely right
      minun(ans, p2[n - j] - 1);
      /// shitty four corners
      /// left-up
      plusle(ans, p2[u2] - 1);
      /// left down
      plusle(ans, p2[d2] - 1);
      /// right up
      plusle(ans, p2[u1] - 1);
      /// right down
      plusle(ans, p2[d1] - 1);
    }
    for (int k = i; k <= j; k++) {
      f2.update(p[k].y, 1);
    }
    i = j;
  }
  printf("%d\n", ans);
}

/*
    Good Luck
        -Lucina
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100, mod = 998244353;
void plusle(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
  return;
}
void minun(int &a, int b) {
  a -= b;
  if (a < 0)
    a += mod;
  return;
}
int add(int a, int b) {
  a += b;
  return a >= mod ? a - mod : a;
}
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + mod : a;
}
int mul(int a, int b) { return (int)((long long)a * b % mod); }
struct BIT {
  int bit[maxn];
  int n;
  BIT(int _n) {
    n = _n + 1;
    for (int i = 0; i <= n; i++)
      bit[i] = 0;
  }
  void update(int idx, int val) {
    for (; idx < n; idx += (idx & (-idx)))
      bit[idx] += val;
    return;
  }
  int get(int idx) {
    int sum = 0;
    for (; idx > 0; idx -= (idx & (-idx)))
      sum += bit[idx];
    return sum;
  }
  int get(int l, int r) { return get(r) - get(l - 1); }
};
struct pt {
  int x, y;
  pt() : x(0), y(0) {}
  void read() { scanf("%d%d", &x, &y); }
  friend bool operator<(const pt &a, const pt &b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
  }
} p[maxn];
vector<int> ss, sss;
int ans, n;
int p2[maxn];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    p[i].read();
    ss.push_back(p[i].x);
    sss.push_back(p[i].y);
  }
  ss.push_back((int)(-1e9 - 7));
  sss.push_back((int)(-1e9 - 7));
  sort(ss.begin(), ss.end());
  sort(sss.begin(), sss.end());
  ss.erase(unique(ss.begin(), ss.end()), ss.end());
  sss.erase(unique(sss.begin(), sss.end()), sss.end());
  for (int i = 1; i <= n; i++) {
    p[i].x = (int)(lower_bound(ss.begin(), ss.end(), p[i].x) - ss.begin());
    p[i].y = (int)(lower_bound(sss.begin(), sss.end(), p[i].y) - sss.begin());
  }
  p2[0] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i] = add(p2[i - 1], p2[i - 1]);
  }
  sort(p + 1, p + 1 + n);
  int cur = 0, u1, u2, d1, d2;
  BIT f1(n), f2(n);
  for (int i = 1; i <= n; i++)
    f1.update(p[i].y, 1);
  for (int i = 1; i <= n; i++) {
    int j = i, ct = 0;
    while (p[j].x == p[i].x && j <= n) {
      f1.update(p[j].y, -1);
      ct++;
      j++;
    }
    j--;
    for (int k = i; k <= j; k++) {
      u1 = f1.get(p[k].y + 1, n), u2 = f2.get(p[k].y + 1, n);
      d1 = f1.get(p[k].y - 1), d2 = f2.get(p[k].y - 1);
      plusle(ans, p2[n] - 1);
      /// completely above
      cur = u1 + u2 + (j - k);
      minun(ans, p2[cur] - 1);
      /// completely below
      cur = d1 + d2 + (k - i);
      minun(ans, p2[cur] - 1);
      /// completely left
      minun(ans, p2[i - 1] - 1);
      /// completely right
      minun(ans, p2[n - j] - 1);
      /// shitty four corners
      /// left-up
      plusle(ans, p2[u2] - 1);
      /// left down
      plusle(ans, p2[d2] - 1);
      /// right up
      plusle(ans, p2[u1] - 1);
      /// right down
      plusle(ans, p2[d1] - 1);
    }
    for (int k = i; k <= j; k++) {
      f2.update(p[k].y, 1);
    }
    i = j;
  }
  printf("%d\n", ans);
}

/*
    Good Luck
        -Lucina
*/