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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};
struct DATA {
  int a, b, c;
  DATA(int a, int b, int c) : a(a), b(b), c(c){};
  bool operator<(const DATA &rhs) const {
    if (a < rhs.a) {
      return true;
    } else if (a == rhs.a) {
      if (b < rhs.b) {
        return true;
      } else if (b == rhs.b) {
        return c < rhs.c;
      }
    }
    return false;
  }
};
int main() {
  int N;
  cin >> N;
  vector<DATA> A;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    A.emplace_back(x, y, i);
  }
  sort(ALL(A));
  set<P> s;
  // rep2(i, 1, N) s.insert(P(A[i].b, A[i].c));

  UnionFind UF(N);
  rep(i, N) {
    int x = A[i].a;
    int y = A[i].b;
    int idx = A[i].c;
    while (!s.empty() && A[i].b >= s.begin()->first) {
      UF.unite(s.begin()->second, idx);
      chmin(y, s.begin()->first);
      s.erase(s.begin());
    }
    s.insert(P(y, idx));
  }
  // rep(j, N)rep(i, j){
  //     int x = A[i].a;
  //     int y = A[i].b;
  //     int index = A[i].c;
  //     if(y < A[j].b) UF.unite(index, A[j].c);

  // }
  rep(i, N) { cout << UF.size(i) << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};
struct DATA {
  int a, b, c;
  DATA(int a, int b, int c) : a(a), b(b), c(c){};
  bool operator<(const DATA &rhs) const {
    if (a < rhs.a) {
      return true;
    } else if (a == rhs.a) {
      if (b < rhs.b) {
        return true;
      } else if (b == rhs.b) {
        return c < rhs.c;
      }
    }
    return false;
  }
};
int main() {
  int N;
  cin >> N;
  vector<DATA> A;
  rep(i, N) {
    int x, y;
    cin >> x >> y;
    A.emplace_back(x, y, i);
  }
  sort(ALL(A));

  UnionFind UF(N);
  rep(j, N) rep(i, j) {
    int x = A[i].a;
    int y = A[i].b;
    int index = A[i].c;
    // rep2(j, i+1, N){// min(N,i+N-y)){
    if (y < A[j].b)
      UF.unite(index, A[j].c);
    // }
  }
  rep(i, N) { cout << UF.size(i) << endl; }

  return 0;
}