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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) : par(n), sz(n, 1) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return sz[root(x)]; }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (sz[x] >= sz[y]) {
      sz[x] += sz[y];
      par[y] = x;
    } else {
      sz[y] += sz[x];
      par[x] = y;
    }
    return true;
  }
};

using P = pair<int, int>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    a[x] = y;
    mp[a[x]] = i;
  }
  UnionFind uf(n);
  set<int> st;
  st.insert(a[0]);
  for (int i = 1; i < n; i++) {
    priority_queue<int> que;
    que.push(a[i]);
    st.insert(a[i]);
    for (auto e : st) {
      if (e >= a[i])
        break;
      uf.unite(e, a[i]);
      que.push(e);
    }
    while (que.size() > 1) {
      int num = que.top();
      que.pop();
      st.erase(num);
    }
  }
  vector<int> res(n);
  rep(i, n) res[mp[a[i]]] = uf.size(a[i]);
  rep(i, n) cout << res[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
using ll = long long;
const int INF = (1 << 30) - 1;
const long long LINF = (1LL << 62) - 1;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

class UnionFind {
  vector<int> par, sz;

public:
  UnionFind(int n) : par(n), sz(n, 1) {
    for (int i = 0; i < n; i++)
      par[i] = i;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return sz[root(x)]; }
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (sz[x] >= sz[y]) {
      sz[x] += sz[y];
      par[y] = x;
    } else {
      sz[y] += sz[x];
      par[x] = y;
    }
    return true;
  }
};

using P = pair<int, int>;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<P> a(n);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    a[x].first = i;
    a[x].second = y;
  }
  UnionFind uf(n);
  set<int> st;
  st.insert(a[0].second);
  for (int i = 1; i < n; i++) {
    for (auto e : st) {
      if (e < a[i].second)
        uf.unite(e, a[i].second);
      else {
        st.insert(a[i].second);
        break;
      }
    }
  }
  sort(a.begin(), a.end());
  rep(i, n) cout << uf.size(a[i].second) << endl;
  return 0;
}