Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

vector<int> pos[200005];

int A[200005];

int prev1[400005];
int next1[400005];

bool present[200005];

vector<int> stack1;

int main() {
  int N;
  long long K;
  scanf("%lld%lld", &N, &K);

  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    A[N + i] = A[i];
  }
  A[2 * N] = A[0];

  memset(prev1, -1, sizeof(prev1));

  for (int i = 0; i <= 2 * N; i++) {
    if (prev1[A[i]] == -1) {
      prev1[A[i]] = i;
    } else {
      next1[prev1[A[i]]] = i;
      prev1[A[i]] = i;
    }
  }

  long long cur_pos = 0;
  long long P = N + 1;
  while (true) {
    long long temp = cur_pos / N * N;
    long long next1_pos = temp + next1[cur_pos % N] + 1;
    if (next1_pos % N == 0) {
      P = next1_pos / N;
      break;
    } else {
      cur_pos = next1_pos;
    }
  }

  K %= P;

  cur_pos = 0;
  while (true) {
    long long temp = cur_pos / N * N;
    long long next1_pos = temp + next1[cur_pos % N] + 1;
    if (next1_pos >= N * K) {
      break;
    } else {
      cur_pos = next1_pos;
    }
  }

  cur_pos %= N;
  memset(present, 0, sizeof(present));
  for (int i = cur_pos; i < N; i++) {
    if (!present[A[i]]) {
      stack1.push_back(A[i]);
      present[A[i]] = true;
    } else {
      while (!stack1.empty()) {
        int k = stack1.back();
        stack1.pop_back();
        present[k] = false;
        if (!present[A[i]]) {
          break;
        }
      }
    }
  }

  for (int i = 0; i < (int)stack1.size(); i++) {
    printf("%d", stack1[i]);
    if (i != stack1.size() - 1) {
      printf(" ");
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int A[400005];

int prev1[400005];
int next1[400005];

bool present[200005];

vector<int> stack1;

int main() {
  int N;
  long long K;
  scanf("%lld%lld", &N, &K);

  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
    A[N + i] = A[i];
  }
  A[2 * N] = A[0];

  memset(prev1, -1, sizeof(prev1));

  for (int i = 0; i <= 2 * N; i++) {
    if (prev1[A[i]] == -1) {
      prev1[A[i]] = i;
    } else {
      next1[prev1[A[i]]] = i;
      prev1[A[i]] = i;
    }
  }

  long long cur_pos = 0;
  long long P = N + 1;
  while (true) {
    long long temp = cur_pos / N * N;
    long long next1_pos = temp + next1[cur_pos % N] + 1;
    if (next1_pos % N == 0) {
      P = next1_pos / N;
      break;
    } else {
      cur_pos = next1_pos;
    }
  }

  K %= P;

  cur_pos = 0;
  while (true) {
    long long temp = cur_pos / N * N;
    long long next1_pos = temp + next1[cur_pos % N] + 1;
    if (next1_pos >= N * K) {
      break;
    } else {
      cur_pos = next1_pos;
    }
  }

  cur_pos %= N;
  memset(present, 0, sizeof(present));
  for (int i = cur_pos; i < N; i++) {
    if (!present[A[i]]) {
      stack1.push_back(A[i]);
      present[A[i]] = true;
    } else {
      while (!stack1.empty()) {
        int k = stack1.back();
        stack1.pop_back();
        present[k] = false;
        if (!present[A[i]]) {
          break;
        }
      }
    }
  }

  for (int i = 0; i < (int)stack1.size(); i++) {
    printf("%d", stack1[i]);
    if (i != stack1.size() - 1) {
      printf(" ");
    }
  }

  return 0;
}