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

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
// #include<bits/stdc++.h>
#include <iostream>
#include <set>
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

const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
  if (ci - cn == cm) {
    fread_unlocked(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}
inline int getint() {
  int A = 0;
  if (ci - cn + 16 > cm)
    while ((ct = getcha()) >= '0')
      A = A * 10 + ct - '0';
  else
    while ((ct = *ci++) >= '0')
      A = A * 10 + ct - '0';
  return A;
}
const int dm = 1 << 22;
char dn[dm], *di = dn;
inline void putint(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--)
    *di++ = (*(C + i));
  *di++ = '\\n';
}

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

  int N = getint(), Q = getint();

  pair<int, int> to[500001];
  int ne[500001], he[710][710] = {};
  int cnt[710] = {};

  int C[500001];
  rep(i, N) { C[i] = getint(); }

  int kotae[500001];
  rep1(i, Q) {
    int l, r;
    l = getint() - 1;
    r = getint() - 1;

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

    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[1500];
  rep(i, 710) {
    if (cnt[i] == 0)
      continue;

    bool A[500001] = {};
    int kari = 0;
    for (int j = i + 1; j < 710; 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 * 710 + 710; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        for (int k = j * 710; 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 * 710; k < j * 710 + 710; k++) {
        if (!A[C[k]]) {
          A[C[k]] = 1;
          kari++;
        }
      }
    }
  }

  rep1(i, Q) { putint(kotae[i]); }
  fwrite(dn, di - dn, 1, stdout);

  Would you please return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
#pragma GCC target("avx2")
// #include<bits/stdc++.h>
#include <iostream>
#include <set>
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

const int cm = 1 << 17;
char cn[cm], *ci = cn + cm, ct;
inline char getcha() {
  if (ci - cn == cm) {
    fread_unlocked(cn, 1, cm, stdin);
    ci = cn;
  }
  return *ci++;
}
inline int getint() {
  int A = 0;
  if (ci - cn + 16 > cm)
    while ((ct = getcha()) >= '0')
      A = A * 10 + ct - '0';
  else
    while ((ct = *ci++) >= '0')
      A = A * 10 + ct - '0';
  return A;
}
const int dm = 1 << 22;
char dn[dm], *di = dn;
inline void putint(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--)
    *di++ = (*(C + i));
  *di++ = '\\n';
}

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

  int N = getint(), Q = getint();

  pair<int, int> to[500001];
  int ne[500001], he[500][500] = {};
  int cnt[500] = {};

  int C[500001];
  rep(i, N) { C[i] = getint(); }

  int kotae[500001];
  rep1(i, Q) {
    int l, r;
    l = getint() - 1;
    r = getint() - 1;

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

    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[2000];
  rep(i, 499) {
    if (cnt[i] == 0)
      continue;

    bool A[500001] = {};
    int kari = 0;
    for (int j = i + 1; j < 500; 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 * 1000 + 1000; k++) {
          if (!A[C[k]]) {
            A[C[k]] = 1;
            kari2++;
            tmp[kt++] = C[k];
          }
        }
        for (int k = j * 1000; 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 * 1000; k < j * 1000 + 1000; k++) {
        if (!A[C[k]]) {
          A[C[k]] = 1;
          kari++;
        }
      }
    }
  }

  rep1(i, Q) { putint(kotae[i]); }
  fwrite(dn, di - dn, 1, stdout);

  Would you please return 0;
}