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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

ll N;

template <typename T> struct BIT {
  int n;
  std::vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

using Tup = std::tuple<ll, ll, ll>;

struct UnionFind {
  std::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])
      std::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)]; }
};

UnionFind uf(200010);

ll ip[200010];

int main() {
  std::cin >> N;

  std::vector<ll> x(N), y(N);

  std::vector<Tup> ts;

  rep(i, N) {
    std::cin >> x[i] >> y[i];
    --x[i];
    --y[i];

    ts.emplace_back(x[i], y[i], i);

    ip[y[i]] = i;
  }

  std::sort(all(ts));

  std::set<ll> S;

  rep(i, N) {
    auto [tx, ty, ti] = ts[i];

    auto it = S.begin();
    auto ub = S.upper_bound(ty);

    if (it == ub) {
      S.emplace(ty);

      continue;
    }

    ll mi = *it;

    for (; it != ub;) {
      uf.unite(ip[*it], ti);
      it = S.erase(it);
    }

    S.emplace(mi);
  }

  rep(i, N) std::cout << uf.size(i) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

ll N;

template <typename T> struct BIT {
  int n;
  std::vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

using Tup = std::tuple<ll, ll, ll>;

struct UnionFind {
  std::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])
      std::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)]; }
};

UnionFind uf(200010);

void solve(const std::vector<ll> &x, const std::vector<ll> &y) {
  std::vector<Tup> ps;

  rep(i, N) ps.emplace_back(x[i], y[i], i);

  std::sort(all(ps));

  std::set<P> prs;

  rep(i, N) {
    auto [px, py, pi] = ps[i];

    if (!prs.empty()) {
      auto bound = prs.lower_bound(P(-py, -INF));

      for (auto it = bound; it != prs.end(); ++it) {
        uf.unite(it->second, pi);
      }
    }

    prs.emplace(-py, pi);
  }

  return;
}

int main() {
  std::cin >> N;

  std::vector<ll> x(N), y(N);

  rep(i, N) {
    std::cin >> x[i] >> y[i];
    --x[i];
    --y[i];
  }

  solve(x, y);

  rep(i, N) {
    x[i] = N - 1 - x[i];
    y[i] = N - 1 - y[i];
  }

  rep(i, N) { std::cout << uf.size(i) << endl; }

  return 0;
}