Problem p03500 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
struct node {
  long long xx1, xx2, yy1, yy2;
};
node b[1301];
struct pt {
  long long x;
  int y;
};
pt d[211];
int n;
long long a[211], k, s1, s2, mo, c[211], h, cnt, r, s, mi[211], v, sta, sto,
    ans;
bool cmp(pt aa, pt bb) { return (aa.x < bb.x); }
bool cmp1(node aa, node bb) {
  return ((aa.xx1 < bb.xx1) || ((aa.xx1 == bb.xx1) && (aa.xx2 < bb.xx2)) ||
          ((aa.xx1 == bb.xx1) && (aa.xx2 == bb.xx2) && (aa.yy1 > bb.yy1)));
}
long long ask(long long aa, long long bb) {
  long long tt = s, w = 0;
  if (aa == -1)
    return (0);
  while (tt != 0) {
    if (bb / tt == 1) {
      if (aa / tt == 0)
        return (w + 1);
      else
        w++;
    }
    bb = bb % tt;
    aa = aa % tt;
    tt = tt / 2;
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  mo = 1000000007;
  h = 103;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  s1 = 0;
  s2 = 0;
  r = a[1];
  mi[0] = 1;
  for (int i = 1; i <= n; i++)
    mi[i] = (mi[i - 1] * h) % mo;
  for (int i = 2; i <= n; i++) {
    s1 = s1 + a[i] - a[i - 1];
    s2 = (s2 * k + a[i] - a[i - 1]) % mo;
    if (r > a[i])
      r = a[i];
  }
  cnt = 1;
  b[1].xx1 = s1;
  b[1].xx2 = (s2 + mo) % mo;
  b[1].yy1 = r;
  if (r >= k)
    b[1].yy2 = k;
  else
    b[1].yy2 = r;
  s = 1;
  for (long long j = 1; j <= 61; j++)
    if (j <= k) {
      for (int i = 1; i <= n; i++) {
        c[i] = (a[i] % 2) * s + c[i];
        a[i] = a[i] / 2;
        d[i].x = c[i];
        d[i].y = i;
      }
      sort(d + 1, d + 1 + n, cmp);
      d[0].x = -1;
      s1 = 0;
      s2 = 0;
      r = a[1];
      for (int i = 2; i <= n; i++) {
        s1 = s1 + a[i] - a[i - 1];
        s2 = (s2 * h + a[i] - a[i - 1]) % mo;
        if (r > a[i])
          r = a[i];
      }
      for (int i = 1; i <= n; i++) {
        if (d[i].x != d[i - 1].x) {
          v = j + ask(d[i - 1].x, d[i].x);
          if (v <= k) {
            cnt++;
            b[cnt].xx1 = s1;
            b[cnt].xx2 = (s2 + mo) % mo;
            b[cnt].yy1 = r;
            if (r >= k - v)
              b[cnt].yy2 = k - v;
            else
              b[cnt].yy2 = r;
          }
        }
        if (a[d[i].y] == 0)
          break;
        if (d[i].y != 1)
          s2 = (s2 - mi[n - d[i].y]) % mo;
        else
          s1++;
        if (d[i].y != n)
          s2 = (s2 + mi[n - d[i].y - 1]) % mo;
        else
          s1--;
        if (a[d[i].y] - 1 < r)
          r = a[d[i].y] - 1;
      }
      s = s * 2;
    } else
      break;
  sort(b + 1, b + 1 + cnt, cmp1);
  sta = b[1].yy1;
  sto = sta - b[1].yy2;
  ans = 0;
  for (int i = 2; i <= cnt; i++)
    if ((b[i].xx1 == b[i - 1].xx1) && (b[i].xx2 == b[i - 1].xx2)) {
      if (b[i].yy1 >= sto) {
        if (b[i].yy1 - b[i].yy2 < sto)
          sto = b[i].yy1 - b[i].yy2;
      } else {
        ans = (ans + sta - sto + 1) % mo;
        sta = b[i].yy1;
        sto = sta - b[i].yy2;
      }
    } else {
      ans = (ans + sta - sto + 1) % mo;
      sta = b[i].yy1;
      sto = sta - b[i].yy2;
    }
  ans = (ans + sta - sto + 1) % mo;
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
struct node {
  long long xx1, xx2, yy1, yy2;
};
node b[13001];
struct pt {
  long long x;
  int y;
};
pt d[211];
int n;
long long a[211], k, s1, s2, mo, c[211], h, cnt, r, s, mi[211], v, sta, sto,
    ans;
bool cmp(pt aa, pt bb) { return (aa.x < bb.x); }
bool cmp1(node aa, node bb) {
  return ((aa.xx1 < bb.xx1) || ((aa.xx1 == bb.xx1) && (aa.xx2 < bb.xx2)) ||
          ((aa.xx1 == bb.xx1) && (aa.xx2 == bb.xx2) && (aa.yy1 > bb.yy1)));
}
long long ask(long long aa, long long bb) {
  long long tt = s, w = 0;
  if (aa == -1)
    return (0);
  while (tt != 0) {
    if (bb / tt == 1) {
      if (aa / tt == 0)
        return (w + 1);
      else
        w++;
    }
    bb = bb % tt;
    aa = aa % tt;
    tt = tt / 2;
  }
}
int main() {
  scanf("%d%lld", &n, &k);
  mo = 1000000007;
  h = 103;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  s1 = 0;
  s2 = 0;
  r = a[1];
  mi[0] = 1;
  for (int i = 1; i <= n; i++)
    mi[i] = (mi[i - 1] * h) % mo;
  for (int i = 2; i <= n; i++) {
    s1 = s1 + a[i] - a[i - 1];
    s2 = (s2 * k + a[i] - a[i - 1]) % mo;
    if (r > a[i])
      r = a[i];
  }
  cnt = 1;
  b[1].xx1 = s1;
  b[1].xx2 = (s2 + mo) % mo;
  b[1].yy1 = r;
  if (r >= k)
    b[1].yy2 = k;
  else
    b[1].yy2 = r;
  s = 1;
  for (long long j = 1; j <= 61; j++)
    if (j <= k) {
      for (int i = 1; i <= n; i++) {
        c[i] = (a[i] % 2) * s + c[i];
        a[i] = a[i] / 2;
        d[i].x = c[i];
        d[i].y = i;
      }
      sort(d + 1, d + 1 + n, cmp);
      d[0].x = -1;
      s1 = 0;
      s2 = 0;
      r = a[1];
      for (int i = 2; i <= n; i++) {
        s1 = s1 + a[i] - a[i - 1];
        s2 = (s2 * h + a[i] - a[i - 1]) % mo;
        if (r > a[i])
          r = a[i];
      }
      for (int i = 1; i <= n; i++) {
        if (d[i].x != d[i - 1].x) {
          v = j + ask(d[i - 1].x, d[i].x);
          if (v <= k) {
            cnt++;
            b[cnt].xx1 = s1;
            b[cnt].xx2 = (s2 + mo) % mo;
            b[cnt].yy1 = r;
            if (r >= k - v)
              b[cnt].yy2 = k - v;
            else
              b[cnt].yy2 = r;
          }
        }
        if (a[d[i].y] == 0)
          break;
        if (d[i].y != 1)
          s2 = (s2 - mi[n - d[i].y]) % mo;
        else
          s1++;
        if (d[i].y != n)
          s2 = (s2 + mi[n - d[i].y - 1]) % mo;
        else
          s1--;
        if (a[d[i].y] - 1 < r)
          r = a[d[i].y] - 1;
      }
      s = s * 2;
    } else
      break;
  sort(b + 1, b + 1 + cnt, cmp1);
  sta = b[1].yy1;
  sto = sta - b[1].yy2;
  ans = 0;
  for (int i = 2; i <= cnt; i++)
    if ((b[i].xx1 == b[i - 1].xx1) && (b[i].xx2 == b[i - 1].xx2)) {
      if (b[i].yy1 >= sto) {
        if (b[i].yy1 - b[i].yy2 < sto)
          sto = b[i].yy1 - b[i].yy2;
      } else {
        ans = (ans + sta - sto + 1) % mo;
        sta = b[i].yy1;
        sto = sta - b[i].yy2;
      }
    } else {
      ans = (ans + sta - sto + 1) % mo;
      sta = b[i].yy1;
      sto = sta - b[i].yy2;
    }
  ans = (ans + sta - sto + 1) % mo;
  printf("%lld\n", ans);
}