Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#define maxn 200 // 100

using namespace std;
vector<long long> q[maxn];
int main() {
  long long N, K;
  long long a[maxn];
  long long ma[maxn], L[maxn];
  scanf("%lld%lld", &N, &K);
  for (long long i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
    q[a[i]].push_back(i);
  }
  for (long long i = 0; i < maxn; i++)
    if (!q[i].empty())
      L[i] = q[i].size();
  long long o = a[N - 1], p = 1, sign = N - 1;
  while (1) {
    int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
    if (S == 0) {
      S = L[o] - 1;
      p++;
    } else
      S--;
    if (q[o][S] == 0)
      break;
    else {
      sign = (q[o][S] + N - 1) % N;
      o = a[sign];
    }
  }
  // printf("%lld\n",p);
  K = K % p;
  stack<long long> U;
  for (long long j = 0; j < maxn; j++)
    ma[j] = 0;
  o = a[0];
  p = 1;
  sign = 0;
  while (1 && K != 0) {
    // printf("1\n");
    int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
    // printf("1\n");
    // printf("%d %lld\n",S,L[o]-1);
    if (S == L[o] - 1) {
      // printf("%lld %lld\n",p,K);

      if (p == K)
        U.push(o);
      else {
        // printf("1\n");
        S = 0;
        p++;
      }
      // printf("1\n");
    } else
      S++;

    // printf("1\n");
    sign = q[o][S] + 1;
    if (p == K && sign == N)
      break;
    else {
      sign %= N;
      o = a[sign];
    }
    // printf("%lld %lld\n",o,sign);
    // printf("%d\n",U.size());
  }
  long long f[maxn];
  long long l = U.size();
  for (long long i = l; i > 0; i--) {
    f[i] = U.top();
    U.pop();
  }
  for (long long i = 1; i <= l; i++)
    printf("%lld ", f[i]);
  printf("\n");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
#define maxn 200100

using namespace std;
vector<long long> q[maxn];
int main() {
  long long N, K;
  long long a[maxn];
  long long ma[maxn], L[maxn];
  scanf("%lld%lld", &N, &K);
  for (long long i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
    q[a[i]].push_back(i);
  }
  for (long long i = 0; i < maxn; i++)
    if (!q[i].empty())
      L[i] = q[i].size();
  long long o = a[N - 1], p = 1, sign = N - 1;
  while (1) {
    int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
    if (S == 0) {
      S = L[o] - 1;
      p++;
    } else
      S--;
    if (q[o][S] == 0)
      break;
    else {
      sign = (q[o][S] + N - 1) % N;
      o = a[sign];
    }
  }
  // printf("%lld\n",p);
  K = K % p;
  stack<long long> U;
  for (long long j = 0; j < maxn; j++)
    ma[j] = 0;
  o = a[0];
  p = 1;
  sign = 0;
  while (1 && K != 0) {
    // printf("1\n");
    int S = lower_bound(q[o].begin(), q[o].end(), sign) - q[o].begin();
    // printf("1\n");
    // printf("%d %lld\n",S,L[o]-1);
    if (S == L[o] - 1) {
      // printf("%lld %lld\n",p,K);

      if (p == K)
        U.push(o);
      else {
        // printf("1\n");
        S = 0;
        p++;
      }
      // printf("1\n");
    } else
      S++;

    // printf("1\n");
    sign = q[o][S] + 1;
    if (p == K && sign == N)
      break;
    else {
      sign %= N;
      o = a[sign];
    }
    // printf("%lld %lld\n",o,sign);
    // printf("%d\n",U.size());
  }
  long long f[maxn];
  long long l = U.size();
  for (long long i = l; i > 0; i--) {
    f[i] = U.top();
    U.pop();
  }
  for (long long i = 1; i <= l; i++)
    printf("%lld ", f[i]);
  printf("\n");
}