Problem p03279 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = (int)j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = (int)j; i >= (int)k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int maxn = 2e5 + 1e2;
const int modd = 1e9 + 7;
const int inf = 0x3f3f3f3f;
struct node {
  int x, y;
  bool operator<(const node &aa) const {
    return ((x < aa.x) || (x == aa.x && y < aa.y));
  }
  bool operator==(const node &aa) const { return (x == aa.x && y == aa.y); }
} s[maxn];
int n, m;
int cnt, N;
int a[maxn], b[maxn], c[maxn], dp[maxn];

inline void file() {
  freopen("ARC101F.in", "r", stdin);
  freopen("ARC101F.out", "w", stdout);
}

inline int read() {
  int x = 0, p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  return x * p;
}

inline void Add(int &a, int b) {
  a += b;
  a = (a >= modd ? a - modd : a);
  a = (a < 0 ? a + modd : a);
}

struct Bit {
  int bit[maxn];

  inline void init() { Set(bit, 0); }

  inline int lowbit(int x) { return x & -x; };

  inline void update(int x, int y) {
    for (; x <= maxn; x += lowbit(x))
      Add(bit[x], y);
  }

  inline int query(int x) {
    int sum = 0;
    for (; x; x -= lowbit(x))
      Add(sum, bit[x]);
    return sum;
  }
} T;

int main() {
  file();
  n = read();
  m = read();
  For(i, 1, n) a[i] = read();
  For(i, 1, m) b[i] = read();
  For(i, 1, n) {
    int p = lower_bound(b + 1, b + m + 1, a[i]) - b;
    if (p == 1 || p == m + 1)
      continue;
    s[++N].x = a[i] - b[p - 1];
    s[N].y = b[p] - a[i];
    c[++cnt] = a[i] - b[p - 1];
    c[++cnt] = b[p] - a[i];
  }
  //	For (i,1,N) cout << s[i].x << " " << s[i].y << endl;
  c[++cnt] = 0;
  c[++cnt] = inf;
  sort(c + 1, c + cnt + 1);
  cnt = unique(c + 1, c + cnt + 1) - (c + 1);
  //	For (i,1,cnt) cout << c[i] << " " ; cout << endl;
  For(i, 1, N) {
    s[i].x = lower_bound(c + 1, c + cnt + 1, s[i].x) - c;
    s[i].y = lower_bound(c + 1, c + cnt + 1, s[i].y) - c;
  }
  sort(s + 1, s + N + 1);
  N = unique(s + 1, s + N + 1) - (s + 1);
  //	For (i,1,N) cout << s[i].x << " " << s[i].y << endl;
  T.init();
  T.update(1, 1);
  for (int i = 1, j; i <= N; i = j + 1) {
    j = i;
    while (j < N && s[j + 1].x == s[i].x)
      ++j;
    //		cout << i << "*" << j << endl;
    For(k, i, j) dp[k] = T.query(s[k].y - 1);
    //		cout << dp[i] << endl;
    For(k, i, j) T.update(s[k].y, dp[k]);
  }
  printf("%d", T.query(cnt));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long ll;
typedef double dd;
#define For(i, j, k) for (int i = (int)j; i <= (int)k; ++i)
#define Forr(i, j, k) for (int i = (int)j; i >= (int)k; --i)
#define Set(a, p) memset(a, p, sizeof(a))
using namespace std;

template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }

const int maxn = 2e5 + 1e2;
const int modd = 1e9 + 7;
const int inf = 0x3f3f3f3f;
struct node {
  int x, y;
  bool operator<(const node &aa) const {
    return ((x < aa.x) || (x == aa.x && y < aa.y));
  }
  bool operator==(const node &aa) const { return (x == aa.x && y == aa.y); }
} s[maxn];
int n, m;
int cnt, N;
int a[maxn], b[maxn], c[maxn], dp[maxn];

inline void file() {
  freopen("ARC101F.in", "r", stdin);
  freopen("ARC101F.out", "w", stdout);
}

inline int read() {
  int x = 0, p = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - '0');
    c = getchar();
  }
  return x * p;
}

inline void Add(int &a, int b) {
  a += b;
  a = (a >= modd ? a - modd : a);
  a = (a < 0 ? a + modd : a);
}

struct Bit {
  int bit[maxn];

  inline void init() { Set(bit, 0); }

  inline int lowbit(int x) { return x & -x; };

  inline void update(int x, int y) {
    for (; x <= maxn; x += lowbit(x))
      Add(bit[x], y);
  }

  inline int query(int x) {
    int sum = 0;
    for (; x; x -= lowbit(x))
      Add(sum, bit[x]);
    return sum;
  }
} T;

int main() {
  //	file();
  n = read();
  m = read();
  For(i, 1, n) a[i] = read();
  For(i, 1, m) b[i] = read();
  For(i, 1, n) {
    int p = lower_bound(b + 1, b + m + 1, a[i]) - b;
    if (p == 1 || p == m + 1)
      continue;
    s[++N].x = a[i] - b[p - 1];
    s[N].y = b[p] - a[i];
    c[++cnt] = a[i] - b[p - 1];
    c[++cnt] = b[p] - a[i];
  }
  //	For (i,1,N) cout << s[i].x << " " << s[i].y << endl;
  c[++cnt] = 0;
  c[++cnt] = inf;
  sort(c + 1, c + cnt + 1);
  cnt = unique(c + 1, c + cnt + 1) - (c + 1);
  //	For (i,1,cnt) cout << c[i] << " " ; cout << endl;
  For(i, 1, N) {
    s[i].x = lower_bound(c + 1, c + cnt + 1, s[i].x) - c;
    s[i].y = lower_bound(c + 1, c + cnt + 1, s[i].y) - c;
  }
  sort(s + 1, s + N + 1);
  N = unique(s + 1, s + N + 1) - (s + 1);
  //	For (i,1,N) cout << s[i].x << " " << s[i].y << endl;
  T.init();
  T.update(1, 1);
  for (int i = 1, j; i <= N; i = j + 1) {
    j = i;
    while (j < N && s[j + 1].x == s[i].x)
      ++j;
    //		cout << i << "*" << j << endl;
    For(k, i, j) dp[k] = T.query(s[k].y - 1);
    //		cout << dp[i] << endl;
    For(k, i, j) T.update(s[k].y, dp[k]);
  }
  printf("%d", T.query(cnt));
  return 0;
}