Problem p03500 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
typedef unsigned long long ull;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 350 + 10, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int n;
ll K;
struct Hash_Table {
  static const int mod1 = 3e4 + 7;
  int Begin[mod1], Next[maxn * 100], e;
  ull to1[maxn * 100], to2[maxn * 100];
  ull Pw1[maxn], Pw2[maxn];
  vector<pair<ll, ll>> vc[maxn * 100];
  void init() {
    memset(Begin, 0, sizeof(Begin));
    e = 0;
    Pw1[0] = Pw2[0] = 1;
    REP(i, 1, n) Pw1[i] = Pw1[i - 1] * 11ull, Pw2[i] = Pw2[i - 1] * 29ull;
  }
  void insert(ll *a, ll l, ll r) {
    ull x = 0, y = 0;
    REP(i, 1, n) x = x * Pw1[1] + a[i], y = y * Pw2[1] + a[i];
    int i = 0;
    for (i = Begin[x % mod1]; i; i = Next[i])
      if (to1[i] == x && to2[i] == y) {
        //				cout<<x<<' '<<y<<' '<<to1[i]<<'
        //'<<to2[i]<<endl;
        break;
      }
    if (!i) {
      to1[++e] = x;
      to2[e] = y;
      Next[e] = Begin[x % mod1];
      Begin[x % mod1] = e;
      i = e;
    }
    //		cout<<x<<' '<<y<<endl;
    //		cout<<i<<' '<<l<<' '<<r<<endl;
    vc[i].push_back(make_pair(l, r));
  }
  int findans() {
    int ans = 0;
    for (int l = 0; l < mod1; ++l)
      for (int i = Begin[l]; i; i = Next[i]) {
        ll L = 0, R = -1;
        sort(vc[i].begin(), vc[i].end());
        REP(j, 0, vc[i].size() - 1)
        if (vc[i][j].first > R) {
          ans = (ans + (R - L + 1) % mod) % mod;
          L = vc[i][j].first, R = vc[i][j].second;
          //						cout<<ans<<endl;
          //						cout<<vc[i][j].first<<'
          //'<<vc[i][j].second<<endl; 						cout<<L<<' '<<R<<endl;
        } else
          R = max(R, vc[i][j].second);
        //				cout<<L<<' '<<R<<endl;
        ans = (ans + (R - L + 1) % mod) % mod;
        //						cout<<ans<<endl;
        vc[i].clear();
      }
    return ans;
  }
} Hash;
ll a[maxn];
struct point {
  ll b, c;
  bool operator<(const point &rhs) const { return c < rhs.c; }
} tmp[maxn];
int Minnum(ll x, ll y) {
  if (x > y)
    return inf;
  int num = 0;
  DREP(i, 59, 0) {
    if ((x & (1ll << i)) != (y & (1ll << i))) {
      if (x & ((1ll << i) - 1))
        num++;
      break;
    } else
      num += (x >> i) & 1;
  }
  return num;
}
ll r[maxn];
void add(int p, ll res) {
  if (res < 0)
    return;
  ll Min = tmp[1].b - (1 < p);
  REP(i, 1, n) {
    r[i] = tmp[i].b - (i < p);
    Min = min(Min, r[i]);
  }
  if (Min < 0)
    return;
  REP(i, 1, n) r[i] -= Min;
  sort(r + 1, r + n + 1);
  //	REP(i,1,n) printf("%lld%c",r[i],i==iend?'\n':' ');
  //	cout<<Min<<' '<<res<<endl;
  //	cout<<Min<<' '<<res<<endl;
  Hash.insert(r, max(Min - res, 0ll), Min);
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("wash.in", "r", stdin);
  freopen("wash.out", "w", stdout);
#endif
  int T = read();
  while (T--) {
    n = read(), K = readll();
    Hash.init();
    REP(i, 1, n) a[i] = readll();
    REP(k, 0, 60) {
      REP(i, 1, n)
      tmp[i] = (point){a[i] >> k, a[i] & ((1ll << k) - 1)};
      sort(tmp + 1, tmp + n + 1);
      ll l = 0;
      for (int i = 1, nxt; i <= n; l = tmp[i].c + 1, i = nxt + 1) {
        nxt = i;
        while (nxt < n && tmp[nxt + 1].c == tmp[i].c)
          ++nxt;
        add(i, K - Minnum(l, tmp[i].c) - k);
      }
    }
    printf("%d\n", Hash.findans());
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
typedef unsigned long long ull;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 350 + 10, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int n;
ll K;
struct Hash_Table {
  static const int mod1 = 3e4 + 7;
  int Begin[mod1], Next[maxn * 100], e;
  ull to1[maxn * 100], to2[maxn * 100];
  ull Pw1[maxn], Pw2[maxn];
  vector<pair<ll, ll>> vc[maxn * 100];
  void init() {
    memset(Begin, 0, sizeof(Begin));
    e = 0;
    Pw1[0] = Pw2[0] = 1;
    REP(i, 1, n) Pw1[i] = Pw1[i - 1] * 11ull, Pw2[i] = Pw2[i - 1] * 29ull;
  }
  void insert(ll *a, ll l, ll r) {
    ull x = 0, y = 0;
    REP(i, 1, n) x = x * Pw1[1] + a[i], y = y * Pw2[1] + a[i];
    int i = 0;
    for (i = Begin[x % mod1]; i; i = Next[i])
      if (to1[i] == x && to2[i] == y) {
        //				cout<<x<<' '<<y<<' '<<to1[i]<<'
        //'<<to2[i]<<endl;
        break;
      }
    if (!i) {
      to1[++e] = x;
      to2[e] = y;
      Next[e] = Begin[x % mod1];
      Begin[x % mod1] = e;
      i = e;
    }
    //		cout<<x<<' '<<y<<endl;
    //		cout<<i<<' '<<l<<' '<<r<<endl;
    vc[i].push_back(make_pair(l, r));
  }
  int findans() {
    int ans = 0;
    for (int l = 0; l < mod1; ++l)
      for (int i = Begin[l]; i; i = Next[i]) {
        ll L = 0, R = -1;
        sort(vc[i].begin(), vc[i].end());
        REP(j, 0, vc[i].size() - 1)
        if (vc[i][j].first > R) {
          ans = (ans + (R - L + 1) % mod) % mod;
          L = vc[i][j].first, R = vc[i][j].second;
          //						cout<<ans<<endl;
          //						cout<<vc[i][j].first<<'
          //'<<vc[i][j].second<<endl; 						cout<<L<<' '<<R<<endl;
        } else
          R = max(R, vc[i][j].second);
        //				cout<<L<<' '<<R<<endl;
        ans = (ans + (R - L + 1) % mod) % mod;
        //						cout<<ans<<endl;
        vc[i].clear();
      }
    return ans;
  }
} Hash;
ll a[maxn];
struct point {
  ll b, c;
  bool operator<(const point &rhs) const { return c < rhs.c; }
} tmp[maxn];
int Minnum(ll x, ll y) {
  if (x > y)
    return inf;
  int num = 0;
  DREP(i, 59, 0) {
    if ((x & (1ll << i)) != (y & (1ll << i))) {
      if (x & ((1ll << i) - 1))
        num++;
      break;
    } else
      num += (x >> i) & 1;
  }
  return num;
}
ll r[maxn];
void add(int p, ll res) {
  if (res < 0)
    return;
  ll Min = tmp[1].b - (1 < p);
  REP(i, 1, n) {
    r[i] = tmp[i].b - (i < p);
    Min = min(Min, r[i]);
  }
  if (Min < 0)
    return;
  REP(i, 1, n) r[i] -= Min;
  sort(r + 1, r + n + 1);
  //	REP(i,1,n) printf("%lld%c",r[i],i==iend?'\n':' ');
  //	cout<<Min<<' '<<res<<endl;
  //	cout<<Min<<' '<<res<<endl;
  Hash.insert(r, max(Min - res, 0ll), Min);
}
int main() {
  int T = 1;
  while (T--) {
    n = read(), K = readll();
    Hash.init();
    REP(i, 1, n) a[i] = readll();
    REP(k, 0, 60) {
      REP(i, 1, n)
      tmp[i] = (point){a[i] >> k, a[i] & ((1ll << k) - 1)};
      sort(tmp + 1, tmp + n + 1);
      ll l = 0;
      for (int i = 1, nxt; i <= n; l = tmp[i].c + 1, i = nxt + 1) {
        nxt = i;
        while (nxt < n && tmp[nxt + 1].c == tmp[i].c)
          ++nxt;
        add(i, K - Minnum(l, tmp[i].c) - k);
      }
    }
    printf("%d\n", Hash.findans());
  }
  return 0;
}