Problem p03279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

const int MAXN = 2005;
const int MOD = 1000000007;

int n, m, L, ans;
int x[MAXN];
int sum[MAXN];

bool mark[MAXN];

pair<int, int> l[MAXN];
pair<int, int> t[MAXN];

set<int> S;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first == b.first ? a.second > b.second : a.first < b.first;
}

void modify(int p, int v) {
  while (p <= L) {
    (sum[p] += v) %= MOD;
    p += p & -p;
  }
}

int query(int p) {
  int res = 0;
  while (p >= 1) {
    (res += sum[p]) %= MOD;
    p -= p & -p;
  }
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  for (int y, i = 1; i <= m; i++) {
    scanf("%d", &y);
    S.insert(y);
  }
  S.insert(-1e9);
  S.insert(2e9);
  for (int i = 1; i <= n; i++) {
    set<int>::iterator it = S.lower_bound(x[i]);
    l[i].first = (*it) - x[i];
    it--;
    l[i].second = x[i] - (*it);
  }
  for (int i = 1; i <= n; i++)
    if (l[i].first >= 1e9 || l[i].second >= 1e9) {
      swap(l[i], l[n]);
      i--;
      n--;
    }
  sort(l + 1, l + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    mark[i] = (l[i].first == l[i - 1].first);
  for (int i = 1; i <= n; i++) {
    if (mark[i])
      l[i].first = l[i - 1].first;
    else
      l[i].first = ++L;
  }
  for (int i = 1; i <= n; i++)
    swap(l[i].first, l[i].second);
  sort(l + 1, l + n + 1, cmp);
  memcpy(t, l, sizeof(t));
  int N = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i] == t[i - 1] || t[i].first >= 1e9 || t[i].second >= 1e9)
      continue;
    l[++N] = t[i];
  }
  n = N;
  for (int i = 1; i <= n; i++)
    modify(l[i].second, query(l[i].second - 1) + 1);
  printf("%d\n", (query(L) + 1) % MOD);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

const int MAXN = 100005;
const int MOD = 1000000007;

int n, m, L, ans;
int x[MAXN];
int sum[MAXN];

bool mark[MAXN];

pair<int, int> l[MAXN];
pair<int, int> t[MAXN];

set<int> S;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return a.first == b.first ? a.second > b.second : a.first < b.first;
}

void modify(int p, int v) {
  while (p <= L) {
    (sum[p] += v) %= MOD;
    p += p & -p;
  }
}

int query(int p) {
  int res = 0;
  while (p >= 1) {
    (res += sum[p]) %= MOD;
    p -= p & -p;
  }
  return res;
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &x[i]);
  for (int y, i = 1; i <= m; i++) {
    scanf("%d", &y);
    S.insert(y);
  }
  S.insert(-1e9);
  S.insert(2e9);
  for (int i = 1; i <= n; i++) {
    set<int>::iterator it = S.lower_bound(x[i]);
    l[i].first = (*it) - x[i];
    it--;
    l[i].second = x[i] - (*it);
  }
  for (int i = 1; i <= n; i++)
    if (l[i].first >= 1e9 || l[i].second >= 1e9) {
      swap(l[i], l[n]);
      i--;
      n--;
    }
  sort(l + 1, l + n + 1, cmp);
  for (int i = 1; i <= n; i++)
    mark[i] = (l[i].first == l[i - 1].first);
  for (int i = 1; i <= n; i++) {
    if (mark[i])
      l[i].first = l[i - 1].first;
    else
      l[i].first = ++L;
  }
  for (int i = 1; i <= n; i++)
    swap(l[i].first, l[i].second);
  sort(l + 1, l + n + 1, cmp);
  memcpy(t, l, sizeof(t));
  int N = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i] == t[i - 1] || t[i].first >= 1e9 || t[i].second >= 1e9)
      continue;
    l[++N] = t[i];
  }
  n = N;
  for (int i = 1; i <= n; i++)
    modify(l[i].second, query(l[i].second - 1) + 1);
  printf("%d\n", (query(L) + 1) % MOD);
  return 0;
}