Problem p03709 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
const int MAXN = 200010;
const int INF = 1000000000;
const ll MOD = 1000000007;
int n, cnt, ord[MAXN];
int q[MAXN], head, tail;
ll dp[MAXN], sum[MAXN];
struct Point {
  int x, v, id;
} P[MAXN];
vector<int> G[MAXN];
struct Line {
  int l, r;
  bool operator<(const Line &rhs) const {
    return l < rhs.l || (l == rhs.l && r > rhs.r);
  }
} L[MAXN];

bool cmp1(Point a, Point b) { return a.v < b.v; }

bool cmp2(Point a, Point b) { return a.x < b.x; }

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  return x * f;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("infection.in", "r", stdin);
  freopen("infection.out", "w", stdout);
#endif
  int i, j;
  n = read();
  for (i = 1; i <= n; i++)
    P[i].x = read(), P[i].v = read(), P[i].id = i;
  sort(P + 1, P + n + 1, cmp1);
  for (i = 1; i <= n; i++)
    ord[P[i].id] = i;
  sort(P + 1, P + n + 1, cmp2);
  // for(i = 1; i <= n; i++) printf("%d ", ord[i]);
  // printf("\n");
  int Maxv = -INF, u;
  for (i = 1; i <= n; i++) {
    if (P[i].v > Maxv)
      Maxv = P[i].v, u = i;
    L[i].r = ord[P[u].id];
    // printf("%d\n", L[i].r);
  }
  // printf("\n");
  int Minv = INF;
  for (i = n; i >= 1; i--) {
    if (P[i].v < Minv)
      Minv = P[i].v, u = i;
    L[i].l = ord[P[u].id];
    // printf("%d\n", L[i].l);
  }
  // printf("\n");
  sort(L + 1, L + n + 1);
  /*for(i = 1; i <= n; i++) printf("%d %d\n", L[i].l, L[i].r);
  printf("\n");*/
  for (i = 1; i <= n; i++)
    G[L[i].r].push_back(L[i].l);
  dp[0] = 1, sum[0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < G[i].size(); j++) {
      sum[i] = (sum[i - 1] + dp[i]) % MOD;
      dp[i] =
          (dp[i] +
           ((sum[i] - sum[G[i][j] - 1] + MOD) % MOD + dp[G[i][j] - 1]) % MOD) %
          MOD;
    }
    sum[i] = (sum[i - 1] + dp[i]) % MOD;
  }
  printf("%lld\n", dp[n]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
const int MAXN = 200010;
const int INF = 1000000000;
const ll MOD = 1000000007;
int n, cnt, ord[MAXN];
int q[MAXN], head, tail;
ll dp[MAXN], sum[MAXN];
struct Point {
  int x, v, id;
} P[MAXN];
vector<int> G[MAXN];
struct Line {
  int l, r;
  bool operator<(const Line &rhs) const {
    return l < rhs.l || (l == rhs.l && r > rhs.r);
  }
} L[MAXN];

bool cmp1(Point a, Point b) { return a.v < b.v; }

bool cmp2(Point a, Point b) { return a.x < b.x; }

inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ 48);
  return x * f;
}

int main() {
  int i, j;
  n = read();
  for (i = 1; i <= n; i++)
    P[i].x = read(), P[i].v = read(), P[i].id = i;
  sort(P + 1, P + n + 1, cmp1);
  for (i = 1; i <= n; i++)
    ord[P[i].id] = i;
  sort(P + 1, P + n + 1, cmp2);
  // for(i = 1; i <= n; i++) printf("%d ", ord[i]);
  // printf("\n");
  int Maxv = -INF, u;
  for (i = 1; i <= n; i++) {
    if (P[i].v > Maxv)
      Maxv = P[i].v, u = i;
    L[i].r = ord[P[u].id];
    // printf("%d\n", L[i].r);
  }
  // printf("\n");
  int Minv = INF;
  for (i = n; i >= 1; i--) {
    if (P[i].v < Minv)
      Minv = P[i].v, u = i;
    L[i].l = ord[P[u].id];
    // printf("%d\n", L[i].l);
  }
  // printf("\n");
  sort(L + 1, L + n + 1);
  /*for(i = 1; i <= n; i++) printf("%d %d\n", L[i].l, L[i].r);
  printf("\n");*/
  for (i = 1; i <= n; i++)
    G[L[i].r].push_back(L[i].l);
  dp[0] = 1, sum[0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < G[i].size(); j++) {
      sum[i] = (sum[i - 1] + dp[i]) % MOD;
      dp[i] =
          (dp[i] +
           ((sum[i] - sum[G[i][j] - 1] + MOD) % MOD + dp[G[i][j] - 1]) % MOD) %
          MOD;
    }
    sum[i] = (sum[i - 1] + dp[i]) % MOD;
  }
  printf("%lld\n", dp[n]);
  return 0;
}