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

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef string str;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define pb(x) pop_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define lb(x, a) lower_bound(all(x), a)
#define ub(x, a) upper_bound(all(x), a)
#define pc(x) __builtin_popcount(x)
#define gll greater<ll>()

struct UnionFind {
  vector<int> par, si;

  UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    si[y] += si[x];
    par[x] = y;
  }

  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return si[root(x)]; }
};

void solve() {
  ll n;
  cin >> n;
  vll id(n), y(n);
  rep(i, 0, n) {
    ll u, v;
    cin >> u >> v;
    --u;
    --v;
    id[u] = i;
    y[u] = v;
  }

  UnionFind u(n);
  set<P> s;
  repr(i, n, 0) {
    P p = P(y[i], id[i]);
    auto it = s.lower_bound(p);
    while (it != s.end()) {
      u.unite(it->se, id[i]);
      p = *it;
      it = s.erase(it);
    }
    s.insert(p);
  }

  rep(i, 0, n) cout << u.size(i) << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef string str;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e18;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define pb(x) pop_back(x)
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sum(x) accumulate(all(x), 0)
#define lb(x, a) lower_bound(all(x), a)
#define ub(x, a) upper_bound(all(x), a)
#define pc(x) __builtin_popcount(x)
#define gll greater<ll>()

struct UnionFind {
  vector<int> par, si;

  UnionFind(int N) : par(N), si(N, 1) { rep(i, 0, N) par[i] = i; }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    si[y] += si[x];
    par[x] = y;
  }

  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return si[root(x)]; }
};

void solve() {
  ll n;
  cin >> n;

  vp x(n);
  map<P, ll> mp;
  rep(i, 0, n) {
    cin >> x[i].fi >> x[i].se;
    mp[x[i]] = i;
  }
  sort(all(x));
  UnionFind u(n);

  set<P> l, r;
  rep(i, 0, n) r.insert(P(x[i].se, mp[x[i]]));

  rep(i, 0, n) {
    P p = P(x[i].se, mp[x[i]]);

    auto z = r.upper_bound(p);
    while (z != r.end()) {
      u.unite(p.se, z->se);
      z++;
    }

    r.erase(p);
  }

  rep(i, 0, n) cout << u.size(i) << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}