Problem p03279 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <fstream>
#include <iostream>
#define mp make_pair
#define x first
#define y second
#define Debug(...) fprintf(stdout, __VA_ARGS__)

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 1e5 + 7;
const int MOD = 1e9 + 7;

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

inline void Add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

namespace BIT {
int tr[MAXN];
inline int lowbit(int x) { return x & (-x); }
inline void update(int x, int val) {
  for (; x < MAXN; x += lowbit(x))
    Add(tr[x], val);
}
inline int query(int x) {
  int res = 0;
  for (; x > 0; x -= lowbit(x))
    Add(res, tr[x]);
  return res;
}
} // namespace BIT

int n, m;
int tx[MAXN], ty[MAXN];
pii p[MAXN];
int f[MAXN];
int vis[MAXN];

inline void init() {
  n = read<int>();
  m = read<int>();
  for (int i = 1; i <= n; ++i)
    tx[i] = read<int>();
  for (int i = 1; i <= m; ++i)
    ty[i] = read<int>();
  int pos = 1;
  for (int i = 1; i <= m; ++i) {
    while (pos <= n && tx[pos] < ty[i]) {
      p[pos].y = ty[i] - tx[pos];
      ++pos;
    }
    //		printf("FF %d\n", pos);
    if (i == 1)
      for (int j = 1; j < pos; ++j)
        vis[j] = 1;
    while (pos <= n && tx[pos] <= ty[i])
      vis[pos++] = 1;
  }

  pos = n;
  for (int i = m; i >= 1; --i) {
    while (pos >= 1 && tx[pos] > ty[i]) {
      p[pos].x = tx[pos] - ty[i];
      --pos;
    }
    if (i == m)
      for (int j = n; j > pos; --j)
        vis[j] = 1;
    while (pos >= 1 && tx[pos] >= ty[i])
      vis[pos--] = 1;
  }

  int tot = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      p[++tot] = p[i];
  n = tot;

  //	for(int i = 1; i <= n; ++i) {
  //		printf("(%d, %d)\n", p[i].x, p[i].y);
  //	}
  sort(p + 1, p + n + 1);
  n = unique(p + 1, p + n + 1) - p - 1;
  //	exit(0);
}

inline void solve() {
  int pos = 1;
  for (int i = 1; i <= n; ++i) {
    while (p[pos].x == p[i].x) {
      f[pos] = BIT::query(p[pos].y - 1) + 1;
      ++pos;
    }
    for (int j = i; j < pos; ++j)
      BIT::update(p[j].y, f[j]);
    i = pos - 1;
    //		printf("In Pos(%d, %d) V = %d\n", p[i].x, p[i].y, BIT::query(p[i].y -
    //1) + 1);
  }
  //	exit(0);
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    Add(ans, f[i]);
    //		printf("FF %d\n", f[i]);
  }

  printf("%d\n", ans);
}

int main() {
  init();
  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <fstream>
#include <iostream>
#define mp make_pair
#define x first
#define y second
#define Debug(...) fprintf(stdout, __VA_ARGS__)

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 1e5 + 7;
const int MOD = 1e9 + 7;

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

inline void Add(int &x, int y) {
  x += y;
  if (x >= MOD)
    x -= MOD;
}

namespace BIT {
int tr[MAXN];
inline int lowbit(int x) { return x & (-x); }
inline void update(int x, int val) {
  for (; x < MAXN; x += lowbit(x))
    Add(tr[x], val);
}
inline int query(int x) {
  int res = 0;
  for (; x > 0; x -= lowbit(x))
    Add(res, tr[x]);
  return res;
}
} // namespace BIT

int n, m;
int tx[MAXN], ty[MAXN];
pii p[MAXN];
int f[MAXN];
int vis[MAXN];

inline void init() {
  n = read<int>();
  m = read<int>();
  for (int i = 1; i <= n; ++i)
    tx[i] = read<int>();
  for (int i = 1; i <= m; ++i)
    ty[i] = read<int>();
  int pos = 1;
  for (int i = 1; i <= m; ++i) {
    while (pos <= n && tx[pos] < ty[i]) {
      p[pos].y = ty[i] - tx[pos];
      ++pos;
    }
    //		printf("FF %d\n", pos);
    if (i == 1)
      for (int j = 1; j < pos; ++j)
        vis[j] = 1;
    while (pos <= n && tx[pos] <= ty[i])
      vis[pos++] = 1;
  }

  pos = n;
  for (int i = m; i >= 1; --i) {
    while (pos >= 1 && tx[pos] > ty[i]) {
      p[pos].x = tx[pos] - ty[i];
      --pos;
    }
    if (i == m)
      for (int j = n; j > pos; --j)
        vis[j] = 1;
    while (pos >= 1 && tx[pos] >= ty[i])
      vis[pos--] = 1;
  }

  int tot = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i])
      p[++tot] = p[i];
  n = tot;

  //	for(int i = 1; i <= n; ++i) {
  //		printf("(%d, %d)\n", p[i].x, p[i].y);
  //	}
  sort(p + 1, p + n + 1);
  n = unique(p + 1, p + n + 1) - p - 1;

  for (int i = 1; i <= n; ++i)
    ty[i] = p[i].y;
  sort(ty + 1, ty + n + 1);
  tot = unique(ty + 1, ty + n + 1) - ty - 1;

  for (int i = 1; i <= n; ++i)
    p[i].y = lower_bound(ty + 1, ty + tot + 1, p[i].y) - ty;
  //	exit(0);
}

inline void solve() {
  int pos = 1;
  for (int i = 1; i <= n; ++i) {
    while (p[pos].x == p[i].x) {
      f[pos] = BIT::query(p[pos].y - 1) + 1;
      ++pos;
    }
    for (int j = i; j < pos; ++j)
      BIT::update(p[j].y, f[j]);
    i = pos - 1;
    //		printf("In Pos(%d, %d) V = %d\n", p[i].x, p[i].y, BIT::query(p[i].y -
    //1) + 1);
  }
  //	exit(0);
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    Add(ans, f[i]);
    //		printf("FF %d\n", f[i]);
  }

  printf("%d\n", ans);
}

int main() {
  init();
  solve();

  return 0;
}