Problem p03709 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

// #define FILE_IO

typedef pair<int, int> pii;

const int mod = 1e9 + 7;

int N;
pii cpy[100005];
int lft[100005], rgt[100005];
int dp[100005], sum[100005];

int power(int x, int y) {
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

class Normalizator {
public:
  map<int, int> mp;
  vector<int> ord;

  void add(int x) {
    if (mp.count(x))
      return;
    mp[x];
    ord.push_back(x);
  }
  void normalize() {
    sort(ord.begin(), ord.end());
    for (int i = 0; i < ord.size(); i++)
      mp[ord[i]] = i + 1;
  }
  int get(int x) { return mp[x]; }
} nrm;

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    cpy[i] = {x, y};
    nrm.add(y);
  }
  nrm.normalize();
  sort(cpy + 1, cpy + N + 1);
  for (int i = 1; i <= N; i++)
    cpy[i].second = nrm.get(cpy[i].second);

  int mx = 0;
  for (int i = 1; i <= N; i++) {
    int y = cpy[i].second;
    mx = max(mx, y);
    rgt[i] = mx;
  }
  int mn = 1 << 30;
  for (int i = N; i >= 1; i--) {
    int y = cpy[i].second;
    mn = min(mn, y);
    lft[i] = mn;
  }

  dp[0] = 1;
  int ans = 0;
  int j = 1;
  for (int i = 1; i <= N; i++) {
    while (rgt[j] + 1 < lft[i])
      j++;

    dp[i] = (sum[i - 1] - sum[j - 1] + mod) % mod;
    if (lft[i] == 1)
      (dp[i] += 1) %= mod;
    if (rgt[i] == N)
      (ans += dp[i]) %= mod;

    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  printf("%d\n", ans);

  return 0;
}


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

using namespace std;

// #define FILE_IO

typedef pair<int, int> pii;

const int mod = 1e9 + 7;

int N;
pii cpy[200005];
int lft[200005], rgt[200005];
int dp[200005], sum[200005];

int power(int x, int y) {
  if (y <= 0)
    return 1;
  int ans = power((1LL * x * x) % mod, y >> 1);
  if (y & 1)
    ans = (1LL * ans * x) % mod;
  return ans;
}

class Normalizator {
public:
  map<int, int> mp;
  vector<int> ord;

  void add(int x) {
    if (mp.count(x))
      return;
    mp[x];
    ord.push_back(x);
  }
  void normalize() {
    sort(ord.begin(), ord.end());
    for (int i = 0; i < ord.size(); i++)
      mp[ord[i]] = i + 1;
  }
  int get(int x) { return mp[x]; }
} nrm;

int main() {
#ifdef FILE_IO
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    cpy[i] = {x, y};
    nrm.add(y);
  }
  nrm.normalize();
  sort(cpy + 1, cpy + N + 1);
  for (int i = 1; i <= N; i++)
    cpy[i].second = nrm.get(cpy[i].second);

  int mx = 0;
  for (int i = 1; i <= N; i++) {
    int y = cpy[i].second;
    mx = max(mx, y);
    rgt[i] = mx;
  }
  int mn = 1 << 30;
  for (int i = N; i >= 1; i--) {
    int y = cpy[i].second;
    mn = min(mn, y);
    lft[i] = mn;
  }

  dp[0] = 1;
  int ans = 0;
  int j = 1;
  for (int i = 1; i <= N; i++) {
    while (rgt[j] + 1 < lft[i])
      j++;

    dp[i] = (sum[i - 1] - sum[j - 1] + mod) % mod;
    if (lft[i] == 1)
      (dp[i] += 1) %= mod;
    if (rgt[i] == N)
      (ans += dp[i]) %= mod;

    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }
  printf("%d\n", ans);

  return 0;
}