Problem p03678 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#define DIM 200005
using namespace std;
// ifstream cin("date.in");
// ofstream cout("date.out");
int n, L, i, j, ok, k;
char s[DIM], a[DIM];
int p[DIM];
long long st, dr, fib[10000], sol[200], sn[200], sk[200];
void solve(int m, long long x, int semn) {
  if (m <= 2) {
    if (x <= n) {
      for (int i = 1; i <= x; i++) {
        sol[s[i]] += semn;
      }
    } else {
      for (int i = 1; i <= n; i++) {
        sol[s[i]] += semn;
      }
      for (i = 1; i <= x - n; i++) {
        sol[s[i]] += semn;
      }
    }
    return;
  }
  if (fib[m - 1] * n + fib[m - 2] * k >= x) {
    solve(m - 1, x, semn);
  } else {
    x -= fib[m - 1] * n + fib[m - 2] * k;
    for (int i = 'a'; i <= 'z'; i++) {
      sol[i] += sn[i] * fib[m - 1] * semn;
      sol[i] += sk[i] * fib[m - 2] * semn;
    }
    solve(m - 2, x, semn);
  }
}
int main() {
  cin >> s + 1;
  n = strlen(s + 1);
  cin >> st >> dr;
  n /= 2;
  for (i = 2; i <= n; i++) {
    while (L > 0 && s[i] != s[L + 1]) {
      L = p[L];
    }
    if (s[i] == s[L + 1]) {
      L++;
    }
    p[i] = L;
  }
  for (i = 1; i <= n; i++) {
    ok = 1;
    for (j = i + i; j <= n; j += i) {
      if (p[j] != j - i) {
        ok = 0;
      }
    }
    L = 1;
    for (j = j - i + 1; j <= n; j++) {
      if (s[j] != s[L]) {
        ok = 0;
      }
      L++;
    }
    if (ok == 1) {
      k = i;
      break;
    }
  }
  for (i = 1; i <= n; i++) {
    sn[s[i]]++;
  }
  for (i = 1; i <= k; i++) {
    sk[s[i]]++;
  }
  fib[1] = fib[2] = 1;
  for (i = 3;; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
    if (fib[i] * n >= dr || fib[i] * n + fib[i - 1] * k >= dr) {
      solve(i, dr, 1);
      solve(i, st - 1, -1);
      break;
    }
  }
  for (i = 'a'; i <= 'z'; i++) {
    cout << sol[i] << " ";
  }
}


Fixed submission:
#include <cstring>
#include <iostream>
#define DIM 200005
using namespace std;
// ifstream cin("date.in");
// ofstream cout("date.out");
int n, L, i, j, ok, k;
char s[DIM], a[DIM];
int p[DIM];
long long st, dr, fib[10000], sol[200], sn[200], sk[200];
void solve(int m, long long x, int semn) {
  if (m <= 2) {
    if (x <= n) {
      for (int i = 1; i <= x; i++) {
        sol[s[i]] += semn;
      }
    } else {
      for (int i = 1; i <= n; i++) {
        sol[s[i]] += semn;
      }
      for (int i = 1; i <= x - n; i++) {
        sol[s[i]] += semn;
      }
    }
    return;
  }
  if (fib[m - 1] * n + fib[m - 2] * k >= x) {
    solve(m - 1, x, semn);
  } else {
    x -= fib[m - 1] * n + fib[m - 2] * k;
    for (int i = 'a'; i <= 'z'; i++) {
      sol[i] += sn[i] * fib[m - 1] * semn;
      sol[i] += sk[i] * fib[m - 2] * semn;
    }
    solve(m - 2, x, semn);
  }
}
int main() {
  cin >> s + 1;
  n = strlen(s + 1);
  cin >> st >> dr;
  n /= 2;
  for (i = 2; i <= n; i++) {
    while (L > 0 && s[i] != s[L + 1]) {
      L = p[L];
    }
    if (s[i] == s[L + 1]) {
      L++;
    }
    p[i] = L;
  }
  for (i = 1; i <= n; i++) {
    ok = 1;
    for (j = i + i; j <= n; j += i) {
      if (p[j] != j - i) {
        ok = 0;
      }
    }
    L = 1;
    for (j = j - i + 1; j <= n; j++) {
      if (s[j] != s[L]) {
        ok = 0;
      }
      L++;
    }
    if (ok == 1) {
      k = i;
      break;
    }
  }
  for (i = 1; i <= n; i++) {
    sn[s[i]]++;
  }
  for (i = 1; i <= k; i++) {
    sk[s[i]]++;
  }
  fib[1] = fib[2] = 1;
  for (i = 3;; i++) {
    fib[i] = fib[i - 1] + fib[i - 2];
    if (fib[i] * n >= dr || fib[i] * n + fib[i - 1] * k >= dr) {
      solve(i, dr, 1);
      solve(i, st - 1, -1);
      break;
    }
  }
  for (i = 'a'; i <= 'z'; i++) {
    cout << sol[i] << " ";
  }
}