Problem p02964 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
#define int long long
#define lint long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
struct Node {
  lint no;
  lint ind;
  vector<P> nxt;
  void input(int i, int in_ind) {
    no = i;
    nxt.push_back({in_ind, 0});
    this->ind = in_ind;
  }
};
signed main() {
  lint n, K;
  cin >> n >> K;
  vector<Node> node(n);
  unordered_map<int, int> memo;
  rep(i, n) {
    int a;
    cin >> a;
    node[i].input(a, i);
    if (a == node[0].no && i > 0) {
      if (node[0].nxt.size() == 1) {
        if (i == n - 1) {
          node[0].nxt.push_back({0, 1});
        } else
          node[0].nxt.push_back({i + 1, 0});
      }
    }
  }
  if (n == 1) {
    if (K % 2 != 0) {
      cout << node[0].no << endl;
      return 0;
    } else {
      return 0;
    }
  }
  if (node[0].nxt.size() == 1) {
    node[0].nxt.push_back({1, 1});
  }
  rep(i, n) {
    if (memo[node[i].no] == 0) {
      memo[node[i].no] = i;
    } else {
      int buf = memo[node[i].no];
      memo[node[i].no] = i;
      if (node[buf].nxt.size() == 1) {
        if (i == n - 1) {
          node[buf].nxt.push_back({0, 1});
        } else
          node[buf].nxt.push_back({i + 1, 0});
      }
    }
  }

  rep(i, n) {
    if (memo[node[i].no] == 0) {
      memo[node[i].no] = i;
    } else {
      int buf = memo[node[i].no];
      memo[node[i].no] = i;
      if (node[buf].nxt.size() == 1) {
        if (i == n - 1) {
          node[buf].nxt.push_back({0, 2});
        } else
          node[buf].nxt.push_back({i + 1, 1});
      }
    }
  }
  int sup = 0;
  rep1(k, 200) {
    int mx = 0;
    rep(i, n) {
      node[i].nxt.push_back(
          {node[node[i].nxt[k].first].nxt[k].first,
           node[node[i].nxt[k].first].nxt[k].second + node[i].nxt[k].second});
      mx = max(mx, node[i].nxt[k].second +
                       node[node[i].nxt[k].first].nxt[k].second);
    }
    if (mx > K) {
      sup = k;
      // break;
    }
  }
  rep(i, n) {
    // printf("i = %lld, nxt = %lld\n",i,node[i].nxt[1].first);
  }
  int k = K - 1;
  int cur = 0;
  // return 0;
  while (1) {
    if (k <= 1) {
      break;
    }
    for (int kkk = 0; kkk < 1; ++kkk) {
      int right = 100;
      int left = 0;
      while (right - left > 1) {
        int mid = (right + left) / 2;
        if (node[cur].nxt[mid].second <= k) {
          left = mid;
        } else
          right = mid;
      }
      int i = left;
      if (node[cur].nxt[i].second <= k) {
        k -= node[cur].nxt[i].second;
        cur = node[cur].nxt[i].first;
        break;
      }
    }
  }
  vector<int> res;
  for (cur; cur < n; ++cur) {
    res.push_back(node[cur].no);
  }
  if (k == 1) {
    rep(i, n) { res.push_back(node[i].no); }
  }
  unordered_map<int, int> hoge;
  int nr = res.size();
  /*
  rep(i,nr){
      cout<<res[i]<<" ";
  }
  cout<<endl;*/
  rep(i, nr) { hoge[res[i]] += 1; }
  int last = inf;
  rep(i, nr) {
    if (last == inf) {
      if (hoge[res[i]] == 1) {
        cout << res[i] << " ";
      } else {
        last = res[i];
      }
      hoge[res[i]] -= 1;
    } else if (last != inf && res[i] != last) {
      hoge[res[i]] -= 1;
    } else if (last != inf && res[i] == last) {
      last = inf;
      hoge[res[i]] -= 1;
    }
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
#define int long long
#define lint long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
#define ll long long
typedef pair<int, int> P;
const lint inf = 1e18 + 7;
const int MOD = 1000000007;
struct Node {
  lint no;
  lint ind;
  vector<P> nxt;
  void input(int i, int in_ind) {
    no = i;
    nxt.push_back({in_ind, 0});
    this->ind = in_ind;
  }
};
signed main() {
  lint n, K;
  cin >> n >> K;
  vector<Node> node(n);
  unordered_map<int, int> memo;
  rep(i, n) {
    int a;
    cin >> a;
    node[i].input(a, i);
    if (a == node[0].no && i > 0) {
      if (node[0].nxt.size() == 1) {
        if (i == n - 1) {
          node[0].nxt.push_back({0, 1});
        } else
          node[0].nxt.push_back({i + 1, 0});
      }
    }
  }
  if (n == 1) {
    if (K % 2 != 0) {
      cout << node[0].no << endl;
      return 0;
    } else {
      return 0;
    }
  }
  if (node[0].nxt.size() == 1) {
    node[0].nxt.push_back({1, 1});
  }
  rep(i, n) {
    if (memo[node[i].no] == 0) {
      memo[node[i].no] = i;
    } else {
      int buf = memo[node[i].no];
      memo[node[i].no] = i;
      if (node[buf].nxt.size() == 1) {
        if (i == n - 1) {
          node[buf].nxt.push_back({0, 1});
        } else
          node[buf].nxt.push_back({i + 1, 0});
      }
    }
  }

  rep(i, n) {
    if (memo[node[i].no] == 0) {
      memo[node[i].no] = i;
    } else {
      int buf = memo[node[i].no];
      memo[node[i].no] = i;
      if (node[buf].nxt.size() == 1) {
        if (i == n - 1) {
          node[buf].nxt.push_back({0, 2});
        } else
          node[buf].nxt.push_back({i + 1, 1});
      }
    }
  }
  int sup = 0;
  rep1(k, 100) {
    int mx = 0;
    rep(i, n) {
      node[i].nxt.push_back(
          {node[node[i].nxt[k].first].nxt[k].first,
           node[node[i].nxt[k].first].nxt[k].second + node[i].nxt[k].second});
      mx = max(mx, node[i].nxt[k].second +
                       node[node[i].nxt[k].first].nxt[k].second);
    }
    if (mx > K) {
      sup = k;
      // break;
    }
  }
  rep(i, n) {
    // printf("i = %lld, nxt = %lld\n",i,node[i].nxt[1].first);
  }
  int k = K - 1;
  int cur = 0;
  // return 0;
  while (1) {
    if (k <= 1) {
      break;
    }
    for (int kkk = 0; kkk < 1; ++kkk) {
      int right = 100;
      int left = 0;
      while (right - left > 1) {
        int mid = (right + left) / 2;
        if (node[cur].nxt[mid].second <= k) {
          left = mid;
        } else
          right = mid;
      }
      int i = left;
      if (node[cur].nxt[i].second <= k) {
        k -= node[cur].nxt[i].second;
        cur = node[cur].nxt[i].first;
        break;
      }
    }
  }
  vector<int> res;
  for (cur; cur < n; ++cur) {
    res.push_back(node[cur].no);
  }
  if (k == 1) {
    rep(i, n) { res.push_back(node[i].no); }
  }
  unordered_map<int, int> hoge;
  int nr = res.size();
  /*
  rep(i,nr){
      cout<<res[i]<<" ";
  }
  cout<<endl;*/
  rep(i, nr) { hoge[res[i]] += 1; }
  int last = inf;
  rep(i, nr) {
    if (last == inf) {
      if (hoge[res[i]] == 1) {
        cout << res[i] << " ";
      } else {
        last = res[i];
      }
      hoge[res[i]] -= 1;
    } else if (last != inf && res[i] != last) {
      hoge[res[i]] -= 1;
    } else if (last != inf && res[i] == last) {
      last = inf;
      hoge[res[i]] -= 1;
    }
  }
  cout << endl;
  return 0;
}