Problem p03500 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fst first
#define snd second
#define mkp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll, ll> pll;
const int MAXN = 210;
const ll MOD = 1000000007;

inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

int n, cn;
map<vector<ll>, vector<pll>> m;
map<vector<ll>, vector<pll>>::iterator it;
ll a[MAXN], c[MAXN], b[MAXN];
ll ans, K;
vector<ll> t;
vector<pll> q;

int main() {

  int T = read(), i, j, k;
  n = read(), K = read();
  ans = 0;
  for (i = 1; i <= n; i++)
    a[i] = read();
  for (k = 0; k <= min(K, 60LL); k++) {
    ll all = (1LL << k) - 1;
    for (i = 1; i <= n; i++)
      c[i] = a[i] & all;
    sort(c + 1, c + n + 1);
    cn = unique(c + 1, c + n + 1) - c - 1;
    c[0] = -1;
    if (c[cn] != all)
      c[++cn] = all;
    for (i = 0; i < cn; i++) {
      ll P, res = 1LL << 61;
      if (c[i] == -1)
        P = 0;
      else {
        for (j = 60; j >= 0; j--)
          if ((c[i] >> j) ^ (c[i + 1] >> j))
            break;
        P = ((c[i + 1] >> j) | 1LL) << j;
      }
      for (j = 1; j <= n; j++) {
        b[j] = (a[j] & all) >= P ? a[j] >> k : (a[j] >> k) - 1;
        res = min(res, b[j]);
      }
      int cnt = __builtin_popcountll(P);
      res = min(res, K - cnt - k);
      if (res < 0)
        continue;
      t.clear();
      for (j = 1; j < n; j++)
        t.push_back(b[j + 1] - b[j]);
      m[t].push_back(mkp(b[1] - res, b[1]));
    }
  }
  for (it = m.begin(); it != m.end(); it++) {
    q = it->snd;
    sort(q.begin(), q.end());
    ll maxr = q[0].snd, lef = q[0].fst;
    for (i = 1; i < (int)q.size(); i++) {
      if (q[i].snd <= maxr)
        continue;
      if (q[i].fst > maxr) {
        ans = (ans + maxr - lef + 1) % MOD;
        lef = q[i].fst, maxr = q[i].snd;
        continue;
      }
      maxr = max(maxr, q[i].snd);
    }
    ans = (ans + maxr - lef + 1) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define fst first
#define snd second
#define mkp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll, ll> pll;
const int MAXN = 210;
const ll MOD = 1000000007;

inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * f;
}

int n, cn;
map<vector<ll>, vector<pll>> m;
map<vector<ll>, vector<pll>>::iterator it;
ll a[MAXN], c[MAXN], b[MAXN];
ll ans, K;
vector<ll> t;
vector<pll> q;

int main() {
  int i, j, k;
  n = read(), K = read();
  ans = 0;
  for (i = 1; i <= n; i++)
    a[i] = read();
  for (k = 0; k <= min(K, 60LL); k++) {
    ll all = (1LL << k) - 1;
    for (i = 1; i <= n; i++)
      c[i] = a[i] & all;
    sort(c + 1, c + n + 1);
    cn = unique(c + 1, c + n + 1) - c - 1;
    c[0] = -1;
    if (c[cn] != all)
      c[++cn] = all;
    for (i = 0; i < cn; i++) {
      ll P, res = 1LL << 61;
      if (c[i] == -1)
        P = 0;
      else {
        for (j = 60; j >= 0; j--)
          if ((c[i] >> j) ^ (c[i + 1] >> j))
            break;
        P = ((c[i + 1] >> j) | 1LL) << j;
      }
      for (j = 1; j <= n; j++) {
        b[j] = (a[j] & all) >= P ? a[j] >> k : (a[j] >> k) - 1;
        res = min(res, b[j]);
      }
      int cnt = __builtin_popcountll(P);
      res = min(res, K - cnt - k);
      if (res < 0)
        continue;
      t.clear();
      for (j = 1; j < n; j++)
        t.push_back(b[j + 1] - b[j]);
      m[t].push_back(mkp(b[1] - res, b[1]));
    }
  }
  for (it = m.begin(); it != m.end(); it++) {
    q = it->snd;
    sort(q.begin(), q.end());
    ll maxr = q[0].snd, lef = q[0].fst;
    for (i = 1; i < (int)q.size(); i++) {
      if (q[i].snd <= maxr)
        continue;
      if (q[i].fst > maxr) {
        ans = (ans + maxr - lef + 1) % MOD;
        lef = q[i].fst, maxr = q[i].snd;
        continue;
      }
      maxr = max(maxr, q[i].snd);
    }
    ans = (ans + maxr - lef + 1) % MOD;
  }
  printf("%lld\n", ans);
  return 0;
}