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

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x, y) memset(x, y, sizeof x)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int mod = 1e9 + 7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;

int pos[200009];
int ind[200009], inv[200009], g[200009];
int ans[200009];
set<int> s;
int par[200009], sz[200009];
int fin(int v) {
  if (par[v] == -1)
    return par[v] = v;
  if (v == par[v])
    return v;
  return par[v] = fin(par[v]);
}

void uni(int a, int b) {
  a = fin(a);
  b = fin(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
  g[a] = min(g[a], g[b]);
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    pos[a] = b;
    ind[a] = i;
    inv[b] = a;
    s.insert(b);
    par[i] = i;
    sz[i] = 1;
    g[i] = b;
  }
  set<int> small;
  for (int i = 1; i <= n; i++) {
    auto it = small.upper_bound(pos[i]);
    while (!small.empty() && it != small.begin()) {
      it--;
      assert(pos[i] > *it);
      uni(ind[i], ind[inv[*it]]);
      small.erase(it);
      it = small.upper_bound(pos[i]);
    }
    small.insert(g[fin(ind[i])]);
  }
  for (int i = 1; i <= n; i++)
    cout << sz[fin(i)] << '\\n';
}

int main() {
  IOS int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x, y) memset(x, y, sizeof x)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

const int mod = 1e9 + 7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
pair<int, int> p[200009];
int cnt[200009];
int ind[200009];
int par[200009], sz[200009];
int fin(int v) {
  if (par[v] == -1)
    return par[v] = v;
  if (v == par[v])
    return v;
  return par[v] = fin(par[v]);
}
// union by size
void make_set(int v) {
  par[v] = -1;
  sz[v] = 1;
}

void uni(int a, int b) {
  a = fin(a);
  b = fin(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  par[b] = a;
  sz[a] += sz[b];
}

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].fi >> p[i].se;
  iota(ind, ind + n, 0);
  sort(ind, ind + n, [](int A, int B) { return p[A] < p[B]; });
  for (int i = 0; i < n; i++)
    make_set(i);
  for (int i = 0; i < n; i++) {
    if (par[ind[i]] != -1)
      continue;
    par[ind[i]] = ind[i];
    for (int j = i + 1; j < n; j++)
      if (p[ind[j]].se > p[ind[i]].se)
        uni(ind[i], ind[j]);
  }
  for (int i = 0; i < n; i++)
    cout << sz[fin(i)] << '\\n';
}

int main() {
  IOS int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}