Problem p03500 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed)                                                         \
  for (int i = (int)(st), i##end = (int)(ed); i <= i##end; ++i)
#define DREP(i, st, ed)                                                        \
  for (int i = (int)(st), i##end = (int)(ed); i >= i##end; --i)
template <typename T> bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <typename T> T read() {
  T x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
#define read() read<int>()
template <typename T> void write(T x, char c) {
  static char t[25];
  static int tlen;
  t[tlen = 1] = c;
  if (x < 0)
    putchar('-'), x = -x;
  do
    t[++tlen] = (x % 10) ^ 48;
  while (x /= 10);
  while (tlen)
    putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
typedef unsigned long long llu;
const int maxn = 355, mod = 1e9 + 7;
map<llu, vector<pair<ll, ll>>> h;
map<llu, vector<pair<ll, ll>>>::iterator it;
ll a[maxn], b[maxn], c[maxn], m, B[maxn];
int n, d[maxn];
bool cmp(const int &x, const int &y) {
  return c[x] == c[y] ? x < y : c[x] < c[y];
}
void work() {
  h.clear();
  n = read(), m = read<ll>();
  REP(i, 1, n) a[i] = read<ll>();
  sort(a + 1, a + 1 + n);
  REP(k, 0, 60) {
    REP(i, 1, n)
    b[i] = a[i] / (1ll << k), c[i] = a[i] % (1ll << k), d[i] = i;
    c[n + 1] = (1ll << k) - 1, d[n + 1] = n + 1;
    sort(d + 1, d + 1 + n + 1, cmp);
    REP(i, 1, n + 1) {
      ll x = c[d[i]], y = c[d[i - 1]];
      int cnt = 0;
      if (i != 1) {
        if (x == y)
          continue;
        DREP(j, 60, 0) {
          if (((x >> j) & 1) ^ ((y >> j) & 1)) {
            ++cnt;
            break;
          } else
            cnt += (x >> j) & 1;
        }
      } else
        y = -1;
      REP(j, 1, n)
      B[j] = b[j] - (c[j] <= y);
      llu hs = 0;
      REP(j, 2, n) hs = hs * 11 + (B[j] - B[j - 1]);
      ll l = max(0ll, B[1] - (m - k - cnt)), r = B[1];
      if (l <= r)
        h[hs].pb(make_pair(l, r));
    }
  }
  int ans = 0;
  static vector<pair<ll, ll>> tmp;
  for (it = h.begin(); it != h.end(); ++it) {
    tmp = (*it).second;
    sort(tmp.begin(), tmp.end());
    ll l = 0, r = -1;
    REP(i, 0, tmp.size() - 1) {
      if (tmp[i].first > r)
        ans = (ans + r - l + 1) % mod, l = tmp[i].first, r = tmp[i].second;
      else
        chkmax(r, tmp[i].second);
    }
    ans = (ans + r - l + 1) % mod;
  }
  write(ans, '\n');
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("wash.in", "r", stdin);
  freopen("wash.out", "w", stdout);
#endif
  int T = read();
  while (T--)
    work();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed)                                                         \
  for (int i = (int)(st), i##end = (int)(ed); i <= i##end; ++i)
#define DREP(i, st, ed)                                                        \
  for (int i = (int)(st), i##end = (int)(ed); i >= i##end; --i)
template <typename T> bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T> bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
template <typename T> T read() {
  T x = 0, f = 1;
  char c = getchar();
  while ((c < '0') || (c > '9')) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ((c >= '0') && (c <= '9'))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return x * f;
}
#define read() read<int>()
template <typename T> void write(T x, char c) {
  static char t[25];
  static int tlen;
  t[tlen = 1] = c;
  if (x < 0)
    putchar('-'), x = -x;
  do
    t[++tlen] = (x % 10) ^ 48;
  while (x /= 10);
  while (tlen)
    putchar(t[tlen--]);
}
#define pb push_back
typedef long long ll;
typedef double lf;
typedef unsigned long long llu;
const int maxn = 355, mod = 1e9 + 7;
map<llu, vector<pair<ll, ll>>> h;
map<llu, vector<pair<ll, ll>>>::iterator it;
ll a[maxn], b[maxn], c[maxn], m, B[maxn];
int n, d[maxn];
bool cmp(const int &x, const int &y) {
  return c[x] == c[y] ? x < y : c[x] < c[y];
}
void work() {
  h.clear();
  n = read(), m = read<ll>();
  REP(i, 1, n) a[i] = read<ll>();
  sort(a + 1, a + 1 + n);
  REP(k, 0, 60) {
    REP(i, 1, n)
    b[i] = a[i] / (1ll << k), c[i] = a[i] % (1ll << k), d[i] = i;
    c[n + 1] = (1ll << k) - 1, d[n + 1] = n + 1;
    sort(d + 1, d + 1 + n + 1, cmp);
    REP(i, 1, n + 1) {
      ll x = c[d[i]], y = c[d[i - 1]];
      int cnt = 0;
      if (i != 1) {
        if (x == y)
          continue;
        DREP(j, 60, 0) {
          if (((x >> j) & 1) ^ ((y >> j) & 1)) {
            ++cnt;
            break;
          } else
            cnt += (x >> j) & 1;
        }
      } else
        y = -1;
      REP(j, 1, n)
      B[j] = b[j] - (c[j] <= y);
      llu hs = 0;
      REP(j, 2, n) hs = hs * 11 + (B[j] - B[j - 1]);
      ll l = max(0ll, B[1] - (m - k - cnt)), r = B[1];
      if (l <= r)
        h[hs].pb(make_pair(l, r));
    }
  }
  int ans = 0;
  static vector<pair<ll, ll>> tmp;
  for (it = h.begin(); it != h.end(); ++it) {
    tmp = (*it).second;
    sort(tmp.begin(), tmp.end());
    ll l = 0, r = -1;
    REP(i, 0, tmp.size() - 1) {
      if (tmp[i].first > r)
        ans = (ans + r - l + 1) % mod, l = tmp[i].first, r = tmp[i].second;
      else
        chkmax(r, tmp[i].second);
    }
    ans = (ans + r - l + 1) % mod;
  }
  write(ans, '\n');
}
int main() {
  work();
  return 0;
}