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

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;

  const int AM = 500001, RM = 708;
  pair<int, int> to[AM];
  int ne[AM], he[RM][RM] = {};
  int cnt[RM] = {};

  int C[AM];
  rep(i, N) { cin >> C[i]; }

  int kotae[AM];
  rep1(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;

    int L = l / RM;
    int R = r / RM;

    if (L == R) {
      set<int> ST;
      int kari = 0;
      for (int j = l; j <= r; j++) {
        if (ST.find(C[j]) == ST.end()) {
          kari++;
          ST.insert(C[j]);
        }
      }
      kotae[i] = kari;
    } else {
      cnt[L]++;
      to[i] = mp(l, r);
      ne[i] = he[L][R];
      he[L][R] = i;
    }
  }
  int tmp[RM * 2];
  rep(i, RM) {
    if (cnt[i] == 0)
      continue;

    bool A[AM] = {};
    int kari = 0;
    for (int j = i + 1; j < RM; j++) {

      for (int t = he[i][j]; t; t = ne[t]) {
        int kt = 0;
        int kari2 = kari;
        int l = to[t].first;
        int r = to[t].second;
        for (int k = l; k < i * RM + RM; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        for (int k = j * RM; k <= r; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        kotae[t] = kari2;
        cnt[i]--;
        rep(k, kt) { A[tmp[k]] = 0; }
      }
      if (cnt[i] == 0)
        break;

      for (int k = j * RM; k < j * RM + RM; k++) {
        if (!A[C[k]]) {
          A[C[k]] = 1;
          kari++;
        }
      }
    }
  }

  rep1(i, Q) { co(kotae[i]); }

  Would you please return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;

  const int AM = 500001, RM = 708;
  pair<int, int> to[AM];
  int ne[AM], he[RM][RM] = {};
  int cnt[RM] = {};

  int C[AM];
  rep(i, N) { cin >> C[i]; }

  int kotae[AM];
  rep1(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;

    int L = l / RM;
    int R = r / RM;

    if (L == R) {
      set<int> ST;
      int kari = 0;
      for (int j = l; j <= r; j++) {
        if (ST.find(C[j]) == ST.end()) {
          kari++;
          ST.insert(C[j]);
        }
      }
      kotae[i] = kari;
    } else {
      cnt[L]++;
      to[i] = mp(l, r);
      ne[i] = he[L][R];
      he[L][R] = i;
    }
  }
  int tmp[RM * 2];
  rep(i, RM) {
    if (cnt[i] == 0)
      continue;

    bool A[AM] = {};
    int kari = 0;
    for (int j = i + 1; j < RM; j++) {

      for (int t = he[i][j]; t; t = ne[t]) {
        int kt = 0;
        int kari2 = kari;
        int l = to[t].first;
        int r = to[t].second;
        for (int k = l; k < i * RM + RM; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        for (int k = j * RM; k <= r; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        kotae[t] = kari2;
        rep(k, kt) { A[tmp[k]] = 0; }
      }

      for (int k = j * RM; k < j * RM + RM; k++) {
        if (!A[C[k]]) {
          A[C[k]] = 1;
          kari++;
        }
      }
    }
  }

  rep1(i, Q) { co(kotae[i]); }

  Would you please return 0;
}