Problem p03709 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int n, f[N], c[N], mx[N], mn[N], ans = 0;

struct line {
  int x, v, L, R;
} a[N];

int lowbit(int x) { return x & (-x); }

void update(int x, int val) {
  for (; x <= n; x += lowbit(x))
    (c[x] += val) %= mod;
}

int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    (res += c[x]) %= mod;
  return res;
}

bool cmp1(line a, line b) { return a.v < b.v; }
bool cmp2(line a, line b) { return a.R != b.R ? a.R < b.R : a.L < b.L; }

int main() {
  //	freopen("river.in", "r", stdin);
  //	freopen("river.out", "w", stdout);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].v;
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i <= n; i++) {
    int l = 1, r = i - 1;
    mx[i] = max(mx[i - 1], a[i].x);
    while (l <= r) {
      int mid = (l + r >> 1);
      if (mx[mid] > a[i].x)
        a[i].L = mid, r = mid - 1;
      else
        l = mid + 1;
    }
  }
  mn[n + 1] = inf;
  for (int i = n; i >= 1; i--) {
    int l = i + 1, r = n;
    mn[i] = min(mn[i + 1], a[i].x);
    while (l <= r) {
      int mid = (l + r >> 1);
      if (mn[mid] < a[i].x)
        a[i].R = mid, l = mid + 1;
      else
        r = mid - 1;
    }
  }
  for (int i = 1; i <= n; i++)
    a[i].L ? 0 : a[i].L = i, a[i].R ? 0 : a[i].R = i;
  sort(a + 1, a + n + 1, cmp2);
  for (int i = 1; i <= n; i++) {
    if (a[i].L == 1)
      f[i] = (f[i] + 1) % mod;
    f[i] += (query(a[i].R) - query(max(a[i].L - 2, 0)) + mod) % mod;
    if (a[i].R == n)
      (ans += f[i]) %= mod;
    update(a[i].R, f[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int n, f[N], c[N], mx[N], mn[N], ans = 0;

struct line {
  int x, v, L, R;
} a[N];

int lowbit(int x) { return x & (-x); }

void update(int x, int val) {
  for (; x <= n; x += lowbit(x))
    (c[x] += val) %= mod;
}

int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    (res += c[x]) %= mod;
  return res;
}

bool cmp1(line a, line b) { return a.v < b.v; }
bool cmp2(line a, line b) { return a.R != b.R ? a.R < b.R : a.L < b.L; }

int main() {
  //	freopen("river.in", "r", stdin);
  //	freopen("river.out", "w", stdout);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].x >> a[i].v;
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i <= n; i++) {
    int l = 1, r = i - 1;
    mx[i] = max(mx[i - 1], a[i].x);
    while (l <= r) {
      int mid = (l + r >> 1);
      if (mx[mid] > a[i].x)
        a[i].L = mid, r = mid - 1;
      else
        l = mid + 1;
    }
  }
  mn[n + 1] = inf;
  for (int i = n; i >= 1; i--) {
    int l = i + 1, r = n;
    mn[i] = min(mn[i + 1], a[i].x);
    while (l <= r) {
      int mid = (l + r >> 1);
      if (mn[mid] < a[i].x)
        a[i].R = mid, l = mid + 1;
      else
        r = mid - 1;
    }
  }
  for (int i = 1; i <= n; i++)
    a[i].L ? 0 : a[i].L = i, a[i].R ? 0 : a[i].R = i;
  sort(a + 1, a + n + 1, cmp2);
  for (int i = 1; i <= n; i++) {
    if (a[i].L == 1)
      f[i] = (f[i] + 1) % mod;
    f[i] += (query(a[i].R) - query(max(a[i].L - 2, 0)) + mod) % mod;
    if (a[i].R == n)
      (ans += f[i]) %= mod;
    update(a[i].R, f[i]);
  }
  cout << ans << endl;
  return 0;
}