Problem p03279 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define eps 1e-4
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define P(a, b) make_pair(a, b)
inline int rd() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
const int N = 100010, mod = 1000000007;
int n, m, a[N], b[N], t = 0, v[N << 1];
int ans = 1;
pii p[N];

inline int pls(int a, int b) { return a + b > mod ? a + b - mod : a + b; }
inline bool cmp(pii a, pii b) {
  return a.first == b.first ? a.second > b.second : a.first < b.first;
}

namespace TA {
#define lowbit(x) (x & -x)
int tr[N];
inline void insert(int x, int d) {
  for (; x <= n; x += lowbit(x))
    tr[x] = pls(tr[x], d);
}
inline int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    res = pls(res, tr[x]);
  return res;
}

} // namespace TA

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  n = rd();
  m = rd();
  fo(i, 1, n) a[i] = rd();
  fo(i, 1, m) b[i] = rd();
  int pos = 1;
  fo(i, 1, n) {
    if (a[i] <= b[1])
      continue;
    if (a[i] >= b[m])
      break;
    for (; b[pos + 1] <= a[i]; pos++)
      ;
    if (a[i] == b[pos])
      continue;
    //		cout<<a[i]<<' '<<b[pos]<<endl;
    p[++t].first = a[i] - b[pos];
    p[t].second = v[t] = b[pos + 1] - a[i];
    //		cout<<v[t]<<' ';
  }
  //	cout<<endl;
  sort(v + 1, v + t + 1);
  n = unique(v + 1, v + t + 1) - v - 1;
  //	cout<<t<<endl<<n<<endl;
  fo(i, 1, t) p[i].second = lower_bound(v + 1, v + n + 1, p[i].second) - v;
  sort(p + 1, p + t + 1, cmp);
  t = unique(p + 1, p + t + 1) - p - 1;
  //	cout<<t<<endl<<endl;
  fo(i, 1, t) {
    int res = pls(TA::query(p[i].second - 1), 1);
    //		cout<<p[i].second<<' '<<res<<endl;
    ans = pls(ans, res);
    TA::insert(p[i].second, res);
  }
  //	cout<<endl;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define eps 1e-4
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define of(i, l, r) for (int i = l; i >= r; i--)
#define fe(i, u) for (int i = head[u]; i; i = e[i].next)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define P(a, b) make_pair(a, b)
inline int rd() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x * f;
}
const int N = 100010, mod = 1000000007;
int n, m, a[N], b[N], t = 0, v[N << 1];
int ans = 1;
pii p[N];

inline int pls(int a, int b) { return a + b > mod ? a + b - mod : a + b; }
inline bool cmp(pii a, pii b) {
  return a.first == b.first ? a.second > b.second : a.first < b.first;
}

namespace TA {
#define lowbit(x) (x & -x)
int tr[N];
inline void insert(int x, int d) {
  for (; x <= n; x += lowbit(x))
    tr[x] = pls(tr[x], d);
}
inline int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    res = pls(res, tr[x]);
  return res;
}

} // namespace TA

int main() {
  n = rd();
  m = rd();
  fo(i, 1, n) a[i] = rd();
  fo(i, 1, m) b[i] = rd();
  int pos = 1;
  fo(i, 1, n) {
    if (a[i] <= b[1])
      continue;
    if (a[i] >= b[m])
      break;
    for (; b[pos + 1] <= a[i]; pos++)
      ;
    if (a[i] == b[pos])
      continue;
    //		cout<<a[i]<<' '<<b[pos]<<endl;
    p[++t].first = a[i] - b[pos];
    p[t].second = v[t] = b[pos + 1] - a[i];
    //		cout<<v[t]<<' ';
  }
  //	cout<<endl;
  sort(v + 1, v + t + 1);
  n = unique(v + 1, v + t + 1) - v - 1;
  //	cout<<t<<endl<<n<<endl;
  fo(i, 1, t) p[i].second = lower_bound(v + 1, v + n + 1, p[i].second) - v;
  sort(p + 1, p + t + 1, cmp);
  t = unique(p + 1, p + t + 1) - p - 1;
  //	cout<<t<<endl<<endl;
  fo(i, 1, t) {
    int res = pls(TA::query(p[i].second - 1), 1);
    //		cout<<p[i].second<<' '<<res<<endl;
    ans = pls(ans, res);
    TA::insert(p[i].second, res);
  }
  //	cout<<endl;
  printf("%d\n", ans);
  return 0;
}