Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

#define int long long

const int N = 2e5 + 5;
int n, k, a[N], st[N], last[N], nxt[N], rd = 1, ed[N];
vector<int> roll;

signed main() {
  freopen("01-02.txt", "r", stdin);
  read(n, k);
  for (int i = 1; i <= n; i++)
    read(a[i]), st[a[i]] ? 1 : st[a[i]] = i;
  for (int i = n; i; i--)
    nxt[i] = last[a[i]] ? last[a[i]] : st[a[i]], last[a[i]] = i;
  for (int x = 1; x <= n; x = nxt[x] + 1)
    ed[rd] = roll.size(), roll.push_back(x), nxt[x] <= x ? ++rd : 1;
  ed[rd] = roll.size();
  roll.push_back(n + 1);
  k %= rd;
  if (!k)
    k = rd;
  ed[0] = -1;
  //	for(int i=1;i<=rd;i++){
  //		putchar('#');
  //		for(int j=ed[i-1]+1;j<=ed[i];j++) write(roll[j]),putchar(' ');
  //		puts("");
  //	}
  //	printf("@%lld %lld\n",roll[ed[k]],nxt[roll[ed[k]]]);
  for (int i = 1; i <= n; i++)
    last[a[i]] = nxt[a[i]] = 0;
  for (int i = n; i >= roll[ed[k]]; i--)
    nxt[i] = last[a[i]] ? last[a[i]] + 1 : i + 1, last[a[i]] = i;
  for (int i = roll[ed[k]], j; i <= n; i = j + 1) {
    j = i;
    while (nxt[j] != j + 1 && j <= n)
      j = nxt[j];
    if (j > n)
      break;
    write(a[j]);
    putchar(' ');
  }
}

Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

#define int long long

const int N = 2e5 + 5;
int n, k, a[N], st[N], last[N], nxt[N], rd = 1, ed[N];
vector<int> roll;

signed main() {
  read(n, k);
  for (int i = 1; i <= n; i++)
    read(a[i]), st[a[i]] ? 1 : st[a[i]] = i;
  for (int i = n; i; i--)
    nxt[i] = last[a[i]] ? last[a[i]] : st[a[i]], last[a[i]] = i;
  for (int x = 1; x <= n; x = nxt[x] + 1)
    ed[rd] = roll.size(), roll.push_back(x), nxt[x] <= x ? ++rd : 1;
  ed[rd] = roll.size();
  roll.push_back(n + 1);
  k %= rd;
  if (!k)
    k = rd;
  ed[0] = -1;
  //	for(int i=1;i<=rd;i++){
  //		putchar('#');
  //		for(int j=ed[i-1]+1;j<=ed[i];j++) write(roll[j]),putchar(' ');
  //		puts("");
  //	}
  //	printf("@%lld %lld\n",roll[ed[k]],nxt[roll[ed[k]]]);
  for (int i = 1; i <= n; i++)
    last[a[i]] = nxt[a[i]] = 0;
  for (int i = n; i >= roll[ed[k]]; i--)
    nxt[i] = last[a[i]] ? last[a[i]] + 1 : i + 1, last[a[i]] = i;
  for (int i = roll[ed[k]], j; i <= n; i = j + 1) {
    j = i;
    while (nxt[j] != j + 1 && j <= n)
      j = nxt[j];
    if (j > n)
      break;
    write(a[j]);
    putchar(' ');
  }
}