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

#include <bits/stdc++.h>

#define fto(i, x, y) for (int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for (int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for (int i = (x); i <= (y); i += a)
#define ftoit(it, var)                                                         \\
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

#define ii pair<int, int>
#define mp make_pair
#define FF first
#define SS second

#define vi vector<int>
#define vii vector<ii>
#define ll long long
#define vll vector<ll>

#define eb emplace_back
#define ef emplace_front
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()

#define oo 1000000007
#define OO 1000000000000000007LL

using namespace std;

#define Pro "tmp"
#define maxN 300007

int a[maxN], b[maxN];
set<int> st[maxN];

int mergeSet(int a, int b) {
  if ((int)st[a].size() < (int)st[b].size())
    swap(a, b);
  ftoit(it, st[b]) st[a].insert(*it);
  st[b].clear();
  return a;
}

int main() {

  int n;
  scanf("%d", &n);

  fto(i, 1, n) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);

  queue<int> q;
  fto(i, 1, n) {
    st[i].insert(a[i]);
    q.push(i);
  }

  ll sol = 0LL;
  fto(i, 1, n) {
    int ans = 0;
    set<int> cur_set;

    int new_idx = 0;
    while (!q.empty()) {
      int idx = q.front();
      q.pop();

      ans += (int)st[idx].size();
      if (st[idx].count(b[i])) {
        st[idx].erase(b[i]);
        new_idx = mergeSet(idx, new_idx);

        if ((int)st[new_idx].size())
          q.push(new_idx);
        break;
      }

      new_idx = mergeSet(idx, new_idx);
    }

    sol += ans - 1;
  }

  printf("%lld\\n", sol);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fto(i, x, y) for (int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for (int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for (int i = (x); i <= (y); i += a)
#define ftoit(it, var)                                                         \\
  for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

#define ii pair<int, int>
#define mp make_pair
#define FF first
#define SS second

#define vi vector<int>
#define vii vector<ii>
#define ll long long
#define vll vector<ll>

#define eb emplace_back
#define ef emplace_front
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()

#define oo 1000000007
#define OO 1000000000000000007LL

using namespace std;

#define Pro "tmp"
#define maxN 300007

int a[maxN], b[maxN];
set<int> st[maxN];

set<int> mergeSet(set<int> a, set<int> b) {
  if ((int)a.size() < (int)b.size())
    swap(a, b);
  ftoit(it, b) a.insert(*it);
  return a;
}

int main() {
  //    #ifndef ONLINE_JUDGE
  //        freopen(Pro".inp", "r", stdin);
  //        freopen(Pro".out", "w", stdout);
  //    #endif // ONLINE_JUDGE

  int n;
  scanf("%d", &n);

  fto(i, 1, n) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  sort(b + 1, b + n + 1);

  queue<int> q;
  fto(i, 1, n) {
    st[i].insert(a[i]);
    q.push(i);
  }

  ll sol = 0LL;
  int new_idx = n;

  fto(i, 1, n) {
    int ans = 0;
    set<int> cur_set;

    while (!q.empty()) {
      int idx = q.front();
      q.pop();

      ans += (int)st[idx].size();
      if (st[idx].count(b[i])) {
        st[idx].erase(b[i]);
        cur_set = mergeSet(st[idx], cur_set);

        if ((int)cur_set.size()) {
          st[idx].clear();
          ++new_idx;
          if (new_idx == 200007)
            new_idx = 1;
          st[new_idx] = cur_set;
          q.push(new_idx);
        }
        break;
      }

      cur_set = mergeSet(st[idx], cur_set);
      st[idx].clear();
    }

    sol += ans - 1;
  }

  printf("%lld\\n", sol);
  return 0;
}