Problem p03678 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 1e5 + 5;
int totlen;
ll l, r;
ll len[maxn];
int nxt[maxn];
char str[maxn];
struct val {
  ll cnt[26];
  val() { memset(cnt, 0, sizeof cnt); }
  friend val operator+(val A, val B) {
    val C;
    rep2(i, 0, 26) C.cnt[i] = A.cnt[i] + B.cnt[i];
    return C;
  }
  friend val operator-(val A, val B) {
    val C;
    rep2(i, 0, 26) C.cnt[i] = A.cnt[i] - B.cnt[i];
    return C;
  }
  friend void Print(val A) {
    rep2(i, 0, 26) printf("%lld ", A.cnt[i]);
    puts("");
  }
} v[105];
void getnxt() {
  int tmp = 0;
  rep(i, 2, totlen) {
    while (tmp && str[tmp + 1] != str[i])
      tmp = nxt[tmp];
    if (str[tmp + 1] == str[i])
      tmp++;
    nxt[i] = tmp;
  }
  for (int i = nxt[totlen];; i = nxt[i])
    if (i < (totlen + 1) / 2) {
      len[1] = totlen - i;
      break;
    }
  len[0] = len[1] - nxt[len[1]];
}
val Calc(int now, ll pos) {
  val res;
  if (now == 1) {
    rep(i, 1, pos) res.cnt[str[i] - 'a']++;
    return res;
  }
  if (pos <= len[now - 1])
    return Calc(now - 1, pos);
  return v[now - 1] + Calc(now - 1, pos - len[now - 1]);
}
int main() {
  scanf("%s", str + 1);
  totlen = strlen(str + 1);
  getnxt();
  rep(i, 1, len[0]) v[0].cnt[str[i] - 'a']++;
  rep(i, 1, len[1]) v[1].cnt[str[i] - 'a']++;
  read(l);
  read(r);
  int maxi = 1;
  rep(i, 2, 100) {
    len[i] = len[i - 1] + len[i - 2], v[i] = v[i - 1] + v[i - 2];
    if (len[i] > r) {
      maxi = i;
      break;
    }
  }
  Print(Calc(maxi, r) - Calc(maxi, l - 1));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
#define w1 first
#define w2 second
#define ls (x << 1)
#define rs (x << 1 | 1)
#define pb push_back
#define mid ((l + r) >> 1)
#define SZ(x) ((x).size())
#define All(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define rep2(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define per(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define Rep(p, x) for (int(p) = head[(x)]; (p); (p) = nxt[(p)])
template <class T> void read(T &num) {
  num = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    num = num * 10 + ch - '0', ch = getchar();
  num *= f;
}
const int maxn = 2e5 + 5;
int totlen;
ll l, r;
ll len[maxn];
int nxt[maxn];
char str[maxn];
struct val {
  ll cnt[26];
  val() { memset(cnt, 0, sizeof cnt); }
  friend val operator+(val A, val B) {
    val C;
    rep2(i, 0, 26) C.cnt[i] = A.cnt[i] + B.cnt[i];
    return C;
  }
  friend val operator-(val A, val B) {
    val C;
    rep2(i, 0, 26) C.cnt[i] = A.cnt[i] - B.cnt[i];
    return C;
  }
  friend void Print(val A) {
    rep2(i, 0, 26) printf("%lld ", A.cnt[i]);
    puts("");
  }
} v[105];
void getnxt() {
  int tmp = 0;
  rep(i, 2, totlen) {
    while (tmp && str[tmp + 1] != str[i])
      tmp = nxt[tmp];
    if (str[tmp + 1] == str[i])
      tmp++;
    nxt[i] = tmp;
  }
  for (int i = nxt[totlen];; i = nxt[i])
    if (i < (totlen + 1) / 2) {
      len[1] = totlen - i;
      break;
    }
  len[0] = len[1] - nxt[len[1]];
}
val Calc(int now, ll pos) {
  val res;
  if (now == 1) {
    rep(i, 1, pos) res.cnt[str[i] - 'a']++;
    return res;
  }
  if (pos <= len[now - 1])
    return Calc(now - 1, pos);
  return v[now - 1] + Calc(now - 1, pos - len[now - 1]);
}
int main() {
  scanf("%s", str + 1);
  totlen = strlen(str + 1);
  getnxt();
  rep(i, 1, len[0]) v[0].cnt[str[i] - 'a']++;
  rep(i, 1, len[1]) v[1].cnt[str[i] - 'a']++;
  read(l);
  read(r);
  int maxi = 1;
  rep(i, 2, 100) {
    len[i] = len[i - 1] + len[i - 2], v[i] = v[i - 1] + v[i - 2];
    if (len[i] > r) {
      maxi = i;
      break;
    }
  }
  Print(Calc(maxi, r) - Calc(maxi, l - 1));
  return 0;
}