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

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
  return f.second > s.second;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll conbinationMemo[100][100];

void cmemoInit() {
  rep(i, 100) {
    rep(j, 100) { conbinationMemo[i][j] = -1; }
  }
}

ll nCr(ll n, ll r) {
  if (conbinationMemo[n][r] != -1)
    return conbinationMemo[n][r];
  if (r == 0 || r == n) {
    return 1;
  } else if (r == 1) {
    return n;
  }
  return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r) {
  r = n - r;
  ll ret = 1;
  for (ll i = n; i >= r + 1; i--)
    ret *= i;
  return ret;
}
//-----------------------ここから-----------
struct UnionFind {
  vector<ll> parent;
  vector<ll> s;

  UnionFind(ll n) {
    parent.resize(n);
    s.resize(n, 1);
    for (int i = 0; i < n; i++)
      parent[i] = i;
  }

  ll root(ll node) {
    if (parent[node] == node) {
      return node;
    } else {
      return parent[node] = root(parent[node]);
    }
  }

  void unite(ll x, ll y) {
    ll rootx = root(x);
    ll rooty = root(y);
    if (rootx == rooty)
      return;
    if (s[rootx] < s[rooty])
      swap(rootx, rooty);
    s[rootx] += s[rooty];
    parent[rooty] = rootx;
  }

  bool isSame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return s[root(x)]; }
};
int main(void) {
  ll n;
  cin >> n;
  vector<pair<pair<ll, ll>, ll>> vecx, vecy;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    vecx.push_back({{x, y}, i});
  }

  sort(all(vecx));

  UnionFind uf(n);
  vector<pair<ll, ll>> mi(200200, {INF, INF});
  set<pair<ll, ll>> rt;
  for (ll i = 0; i < n; i++) {
    for (auto &j : rt) {
      if (j.first < vecx[i].first.second) {
        uf.unite(vecx[i].second, j.second);
      } else {
        break;
      }
    }
    ll root = uf.root(vecx[i].second);
    if (mi[root].first > vecx[i].first.second) {
      rt.erase({mi[root].first, root});
      mi[root] = {vecx[i].first.second, vecx[i].second};
      rt.insert({mi[root].first, root});
    }
  }

  rep(i, n) { cout << uf.size(i) << endl; }
}


Error: TLE

Fixed submission:

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int> &f, const pair<int, int> &s) {
  return f.second > s.second;
}

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll conbinationMemo[100][100];

void cmemoInit() {
  rep(i, 100) {
    rep(j, 100) { conbinationMemo[i][j] = -1; }
  }
}

ll nCr(ll n, ll r) {
  if (conbinationMemo[n][r] != -1)
    return conbinationMemo[n][r];
  if (r == 0 || r == n) {
    return 1;
  } else if (r == 1) {
    return n;
  }
  return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r) {
  r = n - r;
  ll ret = 1;
  for (ll i = n; i >= r + 1; i--)
    ret *= i;
  return ret;
}
//-----------------------ここから-----------
struct UnionFind {
  vector<ll> parent;
  vector<ll> s;

  UnionFind(ll n) {
    parent.resize(n);
    s.resize(n, 1);
    for (int i = 0; i < n; i++)
      parent[i] = i;
  }

  ll root(ll node) {
    if (parent[node] == node) {
      return node;
    } else {
      return parent[node] = root(parent[node]);
    }
  }

  void unite(ll x, ll y) {
    ll rootx = root(x);
    ll rooty = root(y);
    if (rootx == rooty)
      return;
    if (s[rootx] < s[rooty])
      swap(rootx, rooty);
    s[rootx] += s[rooty];
    parent[rooty] = rootx;
  }

  bool isSame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return s[root(x)]; }
};
int main(void) {
  ll n;
  cin >> n;
  vector<pair<pair<ll, ll>, ll>> vecx, vecy;
  rep(i, n) {
    ll x, y;
    cin >> x >> y;
    vecx.push_back({{x, y}, i});
  }

  sort(all(vecx), greater<pair<pair<ll, ll>, ll>>());

  UnionFind uf(n);
  vector<pair<ll, ll>> mi(200200, {-INF, -INF});
  set<ll> rt;
  for (ll i = 0; i < n; i++) {
    for (auto &j : rt) {
      if (mi[j].first > vecx[i].first.second) {
        uf.unite(vecx[i].second, mi[j].second);
      }
    }
    ll root = uf.root(vecx[i].second);
    if (mi[root].first < vecx[i].first.second) {
      mi[root] = {vecx[i].first.second, vecx[i].second};
    }
    rt.insert(root);
  }

  rep(i, n) { cout << uf.size(i) << endl; }
}