Problem p03279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1e9 + 7;

void compress_coordinate(VI &x) {
  int n = x.size();
  VI xc;
  xc.push_back(-1);
  REP(i, n) xc.push_back(x[i]);
  sort(ALL(xc));
  xc.erase(unique(ALL(xc)), xc.end());
  map<int, int> mp;
  REP(i, xc.size()) mp[xc[i]] = i;
  REP(i, n) x[i] = mp[x[i]];
}

struct BIT {
  int n;
  VL bit;

  BIT() {}
  BIT(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void init(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i <= n) {
      (bit[i] += x) %= mod;
      i += i & -i;
    }
  }

  // [0, i]
  ll sum(int i) {
    i++;
    ll ret = 0;
    while (i > 0) {
      (ret += bit[i]) %= mod;
      i -= i & -i;
    }
    return ret;
  }

  // [l, r)
  ll sum(int l, int r) {
    if (l >= r)
      return 0;
    return (sum(r - 1) - sum(l - 1) + mod) % mod;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  VI x(n), y(m);
  REP(i, n) x[i] = in();
  REP(i, m) y[i] = in();
  VI l, r;
  int k = 0;
  REP(i, n) {
    while (k < m && x[i] >= y[k])
      k++;
    if (k == 0 || k == m)
      continue;
    l.push_back(x[i] - y[k - 1]);
    r.push_back(y[k] - x[i]);
  }

  n = l.size();

  // compress_coordinate(l);
  // compress_coordinate(r);

  vector<P> pa(n);
  REP(i, n) pa[i] = P(l[i], -r[i]);
  sort(ALL(pa));

  VVI p(n + 1);
  REP(i, n) { p[pa[i].first].push_back(-pa[i].second); }

  BIT bit(n + 1);
  bit.add(0, 1);
  REP(i, n + 1) {
    REP(j, p[i].size()) {
      if (j > 0 && p[i][j] == p[i][j - 1])
        continue;
      int x = p[i][j];
      bit.add(x, bit.sum(x - 1));
    }
  }

  ll ans = bit.sum(n);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1e9 + 7;

void compress_coordinate(VI &x) {
  int n = x.size();
  VI xc;
  xc.push_back(-1);
  REP(i, n) xc.push_back(x[i]);
  sort(ALL(xc));
  xc.erase(unique(ALL(xc)), xc.end());
  map<int, int> mp;
  REP(i, xc.size()) mp[xc[i]] = i;
  REP(i, n) x[i] = mp[x[i]];
}

struct BIT {
  int n;
  VL bit;

  BIT() {}
  BIT(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void init(int x) {
    n = x;
    bit.assign(n + 1, 0);
  }

  void add(int i, ll x) {
    i++;
    while (i <= n) {
      (bit[i] += x) %= mod;
      i += i & -i;
    }
  }

  // [0, i]
  ll sum(int i) {
    i++;
    ll ret = 0;
    while (i > 0) {
      (ret += bit[i]) %= mod;
      i -= i & -i;
    }
    return ret;
  }

  // [l, r)
  ll sum(int l, int r) {
    if (l >= r)
      return 0;
    return (sum(r - 1) - sum(l - 1) + mod) % mod;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  VI x(n), y(m);
  REP(i, n) x[i] = in();
  REP(i, m) y[i] = in();
  VI l, r;
  int k = 0;
  REP(i, n) {
    while (k < m && x[i] >= y[k])
      k++;
    if (k == 0 || k == m)
      continue;
    l.push_back(x[i] - y[k - 1]);
    r.push_back(y[k] - x[i]);
  }

  n = l.size();

  compress_coordinate(l);
  compress_coordinate(r);

  vector<P> pa(n);
  REP(i, n) pa[i] = P(l[i], -r[i]);
  sort(ALL(pa));

  VVI p(n + 1);
  REP(i, n) { p[pa[i].first].push_back(-pa[i].second); }

  BIT bit(n + 1);
  bit.add(0, 1);
  REP(i, n + 1) {
    REP(j, p[i].size()) {
      if (j > 0 && p[i][j] == p[i][j - 1])
        continue;
      int x = p[i][j];
      bit.add(x, bit.sum(x - 1));
    }
  }

  ll ans = bit.sum(n);
  cout << ans << endl;
  return 0;
}