Problem p02964 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vl s;
  map<int, int> as;
  vvi pos(10); /// 200001!!!
  for (int i = 0; i < n; ++i) {
    pos[a[i]].push_back(i);
  }
  int idx = 0;
  ll ks = 0;
  while (true) {
    auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
    auto nextpositr = next(currpositr);
    if (nextpositr == pos[a[idx]].end()) { // loop
      nextpositr = pos[a[idx]].begin();
      ks++;
    }
    idx = *nextpositr + 1;
    if (idx == n) {
      ks++;
      idx = 0;
      break;
    }
  }
  k %= ks;
  idx = 0;
  ks = 0;
  while (true) {
    auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
    auto nextpositr = next(currpositr);
    if (nextpositr == pos[a[idx]].end()) { // loop
      nextpositr = pos[a[idx]].begin();
      ks++;
      if (ks == k) {
        break;
      }
    }
    idx = *nextpositr;
    if (idx == n - 1) {
      ks++;
      idx = 0;
      break;
    } else {
      idx++;
    }
  }

  for (int i = idx; i < n; ++i) {
    int j = i; // % n;
    if (as[a[j]] == 0) {
      s.push_back(a[j]);
      as[a[j]]++;
    } else {
      while (true) {
        if (s.back() == a[j]) {
          as[s.back()]--;
          s.pop_back();
          break;
        } else {
          as[s.back()]--;
          s.pop_back();
        }
      }
    }
  }

  for (int i = 0; i < s.size(); ++i) {
    cout << s[i];
    if (i == s.size() - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vl a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vl s;
  map<int, int> as;
  vvi pos(200001); /// 200001!!!
  for (int i = 0; i < n; ++i) {
    pos[a[i]].push_back(i);
  }
  int idx = 0;
  ll ks = 0;
  while (true) {
    auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
    auto nextpositr = next(currpositr);
    if (nextpositr == pos[a[idx]].end()) { // loop
      nextpositr = pos[a[idx]].begin();
      ks++;
    }
    idx = *nextpositr + 1;
    if (idx == n) {
      ks++;
      idx = 0;
      break;
    }
  }
  k %= ks;
  idx = 0;
  ks = 0;
  while (true) {
    auto currpositr = find(pos[a[idx]].begin(), pos[a[idx]].end(), idx);
    auto nextpositr = next(currpositr);
    if (nextpositr == pos[a[idx]].end()) { // loop
      nextpositr = pos[a[idx]].begin();
      ks++;
      if (ks == k) {
        break;
      }
    }
    idx = *nextpositr;
    if (idx == n - 1) {
      ks++;
      idx = 0;
      break;
    } else {
      idx++;
    }
  }

  for (int i = idx; i < n; ++i) {
    int j = i; // % n;
    if (as[a[j]] == 0) {
      s.push_back(a[j]);
      as[a[j]]++;
    } else {
      while (true) {
        if (s.back() == a[j]) {
          as[s.back()]--;
          s.pop_back();
          break;
        } else {
          as[s.back()]--;
          s.pop_back();
        }
      }
    }
  }

  for (int i = 0; i < s.size(); ++i) {
    cout << s[i];
    if (i == s.size() - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}