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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

string app() {
  int n;
  cin >> n;

  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  // ふるい
  int MAX = 1e6 + 10;
  vector<int> divisors(MAX);
  rep(i, MAX) divisors[i] = i;
  rep_range(i, 2, 1e3 + 1) {
    int j = 0;
    while ((j += i) < MAX) {
      if (divisors[j] == j)
        divisors[j] = i;
    }
  }

  set<int> any, all;
  int t = a[0];
  while (t > 1) {
    all.emplace(divisors[t]);
    t /= divisors[t];
  }

  bool anyfailed = false;
  rep(i, n) {
    auto itall = all.begin();
    int t = a[i];
    int lastu = 0;
    while (t > 1) {
      int u = divisors[t];
      if (u != lastu) {
        if (any.count(u) > 0)
          anyfailed = true;
        any.emplace(u);
        while (itall != all.end() && *itall < u)
          itall = all.erase(itall);
        if (itall != all.end() && *itall == u)
          itall++;
      }
      t /= divisors[t];
      lastu = u;
    }
    while (itall != all.end())
      itall = all.erase(itall);
  }

  if (anyfailed == false)
    return "pairwise coprime";
  if (all.empty())
    return "setwise coprime";
  return "not coprime";
}

int main() {
  cout << app() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

string app() {
  int n;
  cin >> n;

  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  // ふるい
  int MAX = 1e6 + 1;
  vector<int> divisors(MAX);
  rep(i, MAX) divisors[i] = i;
  rep_range(i, 2, 1e3 + 1) {
    int j = 0;
    while ((j += i) < MAX) {
      if (divisors[j] == j)
        divisors[j] = i;
    }
  }

  set<int> any, all;
  int t = a[0];
  while (t > 1) {
    all.emplace(divisors[t]);
    t /= divisors[t];
  }

  bool anyfailed = false;
  rep(i, n) {
    auto itany = any.begin();
    auto itall = all.begin();
    int t = a[i];
    while (t > 1) {
      int u = divisors[t];

      while (itany != any.end() && *itany < u)
        itany++;
      if (itany != any.end() && *itany == u)
        anyfailed = true;
      any.emplace(u);
      while (itall != all.end() && *itall < u)
        itall = all.erase(itall);
      if (itall != all.end() && *itall == u)
        itall++;

      t /= divisors[t];
    }
    while (itall != all.end())
      itall = all.erase(itall);
  }

  if (anyfailed == false)
    return "pairwise coprime";
  if (all.empty())
    return "setwise coprime";
  return "not coprime";
}

int main() {
  cout << app() << endl;
  return 0;
}