Problem p03678 (C++)

Buggy submission (status: Time Limit Exceeded):
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5, alpha = 26;

char s0[MAXN + 5];
int S[MAXN + 5];

int n, N;
LL L, R;

inline void input() {
  scanf("%s", s0 + 1), n = strlen(s0 + 1), N = n / 2;
  for (int i = 1; i <= n; ++i)
    S[i] = s0[i] - 'a';
  L = read<LL>(), R = read<LL>();
}

int T[MAXN + 5];
int M;

inline void calc_T(int *s, int len) {
  static int fail[MAXN + 5];

  for (int i = 2, j = 0; i <= len; ++i) {
    while (j && s[j + 1] != s[i])
      j = fail[j];
    if (s[j + 1] == s[i])
      ++j;
    fail[i] = j;
  }

  M = fail[len] ? len - fail[len] : len;
  for (int i = 1; i <= M; ++i)
    T[i] = s[i];
}

struct vec {
  LL cnt[alpha];

  inline LL &operator[](int pos) { return cnt[pos]; }
  inline const LL &operator[](int pos) const { return cnt[pos]; }

  inline vec operator+(const vec &rhs) const {
    static vec res;
    for (int i = 0; i < alpha; ++i)
      res[i] = (*this)[i] + rhs[i];
    return res;
  }
  inline vec operator-(const vec &rhs) const {
    static vec res;
    for (int i = 0; i < alpha; ++i)
      res[i] = (*this)[i] - rhs[i];
    return res;
  }

  vec() { memset(cnt, 0, sizeof cnt); }
};

const int MAX_LOG = 100;

vec cnt[MAX_LOG + 1];
LL len[MAX_LOG + 1];
int k;

inline vec prefix(LL LIM) {
  if (LIM == 0)
    return vec();
  for (int i = k; i >= 1; --i)
    if (len[i] <= LIM)
      return cnt[i] + prefix(LIM - len[i]);

  vec res;
  for (int i = 1; i <= LIM; ++i)
    ++res[S[i]];
  return res;
}

inline void solve() {
  calc_T(S, N);

  len[1] = N;
  for (int i = 1; i <= N; ++i)
    ++cnt[1][S[i]];

  len[2] = N + M;
  for (int i = 1; i <= N; ++i)
    ++cnt[2][S[i]];
  for (int i = 1; i <= M; ++i)
    ++cnt[2][T[i]];

  for (k = 2; len[k] * 2 < R;) {
    ++k;
    len[k] = len[k - 1] + len[k - 2];
    for (int i = 0; i < alpha; ++i)
      cnt[k][i] = cnt[k - 1][i] + cnt[k - 2][i];
  }

  vec ans;
  if (R > len[k])
    ans = prefix(len[k]) - prefix(L - 1) + prefix(R - len[k]);
  else
    ans = prefix(R) - prefix(L - 1);
  for (int i = 0; i < alpha; ++i)
    printf("%lld ", ans[i]);
  puts("");
}

int main() {
#ifdef K_ON // K-ON!
  freopen("F.in", "r", stdin);
  freopen("F.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// Ho-kago Tea Time

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)2e5, alpha = 26;

char s0[MAXN + 5];
int S[MAXN + 5];

int n, N;
LL L, R;

inline void input() {
  scanf("%s", s0 + 1), n = strlen(s0 + 1), N = n / 2;
  for (int i = 1; i <= n; ++i)
    S[i] = s0[i] - 'a';
  L = read<LL>(), R = read<LL>();
}

int T[MAXN + 5];
int M;

inline void calc_T(int *s, int len) {
  static int fail[MAXN + 5];

  for (int i = 2, j = 0; i <= len; ++i) {
    while (j && s[j + 1] != s[i])
      j = fail[j];
    if (s[j + 1] == s[i])
      ++j;
    fail[i] = j;
  }

  M = fail[len] ? len - fail[len] : len;
  for (int i = 1; i <= M; ++i)
    T[i] = s[i];
}

struct vec {
  LL cnt[alpha];

  inline LL &operator[](int pos) { return cnt[pos]; }
  inline const LL &operator[](int pos) const { return cnt[pos]; }

  inline vec operator+(const vec &rhs) const {
    static vec res;
    for (int i = 0; i < alpha; ++i)
      res[i] = (*this)[i] + rhs[i];
    return res;
  }
  inline vec operator-(const vec &rhs) const {
    static vec res;
    for (int i = 0; i < alpha; ++i)
      res[i] = (*this)[i] - rhs[i];
    return res;
  }

  vec() { memset(cnt, 0, sizeof cnt); }
};

const int MAX_LOG = 100;

vec cnt[MAX_LOG + 1];
LL len[MAX_LOG + 1];
int k;

inline vec prefix(LL LIM) {
  if (LIM == 0)
    return vec();
  for (int i = k; i >= 1; --i)
    if (len[i] <= LIM)
      return cnt[i] + prefix(LIM - len[i]);

  vec res;
  for (int i = 1; i <= LIM; ++i)
    ++res[S[i]];
  return res;
}

inline void solve() {
  calc_T(S, N);

  len[1] = N;
  for (int i = 1; i <= N; ++i)
    ++cnt[1][S[i]];

  len[2] = N + M;
  for (int i = 1; i <= N; ++i)
    ++cnt[2][S[i]];
  for (int i = 1; i <= M; ++i)
    ++cnt[2][T[i]];

  for (k = 2; len[k] * 2 < R;) {
    ++k;
    len[k] = len[k - 1] + len[k - 2];
    for (int i = 0; i < alpha; ++i)
      cnt[k][i] = cnt[k - 1][i] + cnt[k - 2][i];
  }

  vec ans;
  if (R > len[k])
    ans = prefix(len[k]) - prefix(L - 1) + prefix(R - len[k]);
  else
    ans = prefix(R) - prefix(L - 1);
  for (int i = 0; i < alpha; ++i)
    printf("%lld ", ans[i]);
  puts("");
}

int main() {
#ifdef K_ON // K-ON!
  freopen("F.in", "r", stdin);
  freopen("F.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}