Problem p03709 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define rep(i, a, b) for (int i = int(a), nn = int(b); i <= nn; ++i)
#define vep(i, a, b) for (int i = int(a), nn = int(b); i >= nn; --i)
#define xep(i, b) for (int i = 0, nn = int(b); i < nn; ++i)

#define LB lower_bound
#define md 1000000007

const int N = 200100;

int v[N], w[N], vc[N], od[N];
int r[N], l[N], f[N], sk[N];
int vn, n;

char *cp = (char *)malloc(1 << 15), *is = cp, *it = cp;
#define getc()                                                                 \
  (is == it && (it = (is = cp) + fread(cp, 1, 1 << 15, stdin), is == it)       \
       ? 0                                                                     \
       : *is++)

inline void in(int &x) {
  char c = getc();
  int f = 1;
  for (; c < '0' || c > '9'; c = getc())
    f = (c == '-' ? -1 : f);
  for (x = 0; c >= '0' && c <= '9'; c = getc())
    x = x * 10 + c - 48;
  x *= f;
}

#define mo(x) x = (x >= md ? x - md : (x < 0 ? x + md : x))
bool cmpw(int a, int b) { return w[a] < w[b]; }

int main() {
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);

  in(n);
  rep(i, 1, n) {
    in(w[i]), in(v[i]);
    vc[i] = v[i];
  }

  sort(vc + 1, vc + 1 + n);
  vn = unique(vc + 1, vc + 1 + n) - (vc + 1);

  rep(i, 1, n) v[i] = LB(vc + 1, vc + 1 + vn, v[i]) - vc;

  rep(i, 1, n) od[i] = i;
  sort(od + 1, od + 1 + n, cmpw);

  int mxv = 0, x;
  rep(i, 1, n) {
    x = od[i];
    v[x] > mxv ? mxv = v[x] : 233;
    r[x] = mxv;
  }

  mxv = vn + 1;
  vep(i, n, 1) {
    x = od[i];
    v[x] < mxv ? mxv = v[x] : 233;
    l[x] = mxv;
  }

  // rep(i, 1, n) cout << l[i] << ' ' << r[i] << endl;
  int sm = 0, hd = 0, tp = 0;

  l[0] = 1, r[0] = 0;
  f[0] = 1, sk[tp++] = 0, sm = 1;

  ++n, l[n] = r[n] = n;
  od[n] = n;

  rep(i, 1, n) {
    x = od[i];
    while (hd < tp && l[x] > r[sk[hd]] + 1)
      sm = sm - f[sk[hd]], mo(sm), ++hd;
    f[x] = sm;
    sm = sm + f[x], mo(sm);
    sk[tp++] = x;
  }

  printf("%d\n", f[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define rep(i, a, b) for (int i = int(a), nn = int(b); i <= nn; ++i)
#define vep(i, a, b) for (int i = int(a), nn = int(b); i >= nn; --i)
#define xep(i, b) for (int i = 0, nn = int(b); i < nn; ++i)

#define LB lower_bound
#define md 1000000007

const int N = 200100;

int v[N], w[N], vc[N], od[N];
int r[N], l[N], f[N], sk[N];
int vn, n;

char *cp = (char *)malloc(1 << 15), *is = cp, *it = cp;
#define getc()                                                                 \
  (is == it && (it = (is = cp) + fread(cp, 1, 1 << 15, stdin), is == it)       \
       ? 0                                                                     \
       : *is++)

inline void in(int &x) {
  char c = getc();
  int f = 1;
  for (; c < '0' || c > '9'; c = getc())
    f = (c == '-' ? -1 : f);
  for (x = 0; c >= '0' && c <= '9'; c = getc())
    x = x * 10 + c - 48;
  x *= f;
}

#define mo(x) x = (x >= md ? x - md : (x < 0 ? x + md : x))
bool cmpw(int a, int b) { return w[a] < w[b]; }

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);

  in(n);
  rep(i, 1, n) {
    in(w[i]), in(v[i]);
    vc[i] = v[i];
  }

  sort(vc + 1, vc + 1 + n);
  vn = unique(vc + 1, vc + 1 + n) - (vc + 1);

  rep(i, 1, n) v[i] = LB(vc + 1, vc + 1 + vn, v[i]) - vc;

  rep(i, 1, n) od[i] = i;
  sort(od + 1, od + 1 + n, cmpw);

  int mxv = 0, x;
  rep(i, 1, n) {
    x = od[i];
    v[x] > mxv ? mxv = v[x] : 233;
    r[x] = mxv;
  }

  mxv = vn + 1;
  vep(i, n, 1) {
    x = od[i];
    v[x] < mxv ? mxv = v[x] : 233;
    l[x] = mxv;
  }

  // rep(i, 1, n) cout << l[i] << ' ' << r[i] << endl;
  int sm = 0, hd = 0, tp = 0;

  l[0] = 1, r[0] = 0;
  f[0] = 1, sk[tp++] = 0, sm = 1;

  ++n, l[n] = r[n] = n;
  od[n] = n;

  rep(i, 1, n) {
    x = od[i];
    while (hd < tp && l[x] > r[sk[hd]] + 1)
      sm = sm - f[sk[hd]], mo(sm), ++hd;
    f[x] = sm;
    sm = sm + f[x], mo(sm);
    sk[tp++] = x;
  }

  printf("%d\n", f[n]);
  return 0;
}