Problem p00344 (C++) — buggy submission, verdict: Time Limit Exceeded

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  cin >> n;
  vi a(n), p(n, -INF), q(n, -INF);
  rep(i, n) cin >> a[i];
  int s = 0;
  rep(i, n) {
    int x = i, c = 0;
    for (; p[x] < 0; c++) {
      p[x] = c;
      q[x] = i;
      x = (x + a[x]) % n;
    }
    if (q[x] == i)
      s += c - p[x];
  }
  cout << s << endl;
}

Error: TLE

Fixed submission:

// clang-format off

#include <bits/stdc++.h>

#define int long long

#define main signed main()

// #define main int main()

#define loop(i, a, n) for (int i = (a); i < (n); i++)

#define rep(i, n) loop(i, 0, n)

#define all(v) (v).begin(), (v).end()

#define rall(v) (v).rbegin(), (v).rend()

#define prec(n) fixed << setprecision(n)

#define stlice(from, to) substr(from, (to) - (from) + 1)

#define pb push_back

#define mp make_pair

#define mt make_tuple

#define fi first

#define se second

using namespace std;

typedef pair<int, int> pii;

typedef vector<int> vi;

typedef vector<double> vd;

typedef vector<char> vc;

typedef vector<bool> vb;

typedef vector<string> vs;

typedef vector<pii> vpii;

typedef vector<vi> vvi;

typedef vector<vb> vvb;

typedef vector<vpii> vvpii;

const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;

const int MOD = 1000000007;

const double PI = acos(-1);

template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }

template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }

bool odd(const int &n) { return n & 1; }

bool even(const int &n) { return !odd(n); }

void solve();

main { solve(); return 0; }

// clang-format on

void solve() {

  int n;

  cin >> n;

  vi a(n);

  rep(i, n) cin >> a[i];

  vi hai(n);

  rep(i, n) {

    int x = i;

    set<int> v;

    v.insert(i);

    for (;;) {

      x = (x + a[x]) % n;

      if (x == i) {

        for (int p : v)
          hai[p] = true;
      }

      if (v.count(x))
        break;

      v.insert(x);
    }
  }

  cout << count(all(hai), true) << endl;
}