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

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define LOCAL 1

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(...)                                                               \\
  cout << #__VA_ARGS__ ": [";                                                  \\
  for (auto e : __VA_ARGS__) {                                                 \\
    cout << e << ", ";                                                         \\
  }                                                                            \\
  cout << "]\\n"
#define dbg1(...) cout << #__VA_ARGS__ " " << __VA_ARGS__
#else
#define dbg(...) 1;
#define dbg1(...) 1;
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
constexpr ll R = 2e5 + 69, INF = 2e9 + 6969;

int n, rep[R], ile[R], X[R], t[R], c[R], Y[R];

int Find(int a) {
  if (a != rep[a])
    rep[a] = Find(rep[a]);
  return rep[a];
}
void Union(int a, int b) {
  a = Find(a);
  b = Find(b);
  if (ile[a] < ile[b])
    swap(a, b);
  if (c[a] > c[b])
    c[a] = c[b];
  ile[a] += ile[b];
  rep[b] = a;
}

stack<PII> s;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    t[a] = i;
    Y[b] = i;
    X[a] = b;
    c[i] = b;
    ile[i] = 1;
    rep[i] = i;
  }
  s.push(mp(INF, 0));
  for (int i = 1; i <= n; ++i) {
    while (s.top().fi < X[i]) {
      if (Find(s.top().se) != Find(t[i])) {
        Union(s.top().se, t[i]);
        //  printf("Un: %d %d\\n", s.top().se, t[i]);
      }
      s.pop();
    }
    s.push(mp(c[Find(t[i])], Find(t[i])));
  }
  for (int i = 1; i <= n; ++i) {
    //  printf("%d: %d %d\\n", i, ile[Find(i)], rep[Find(i)]);
    printf("%d\\n", ile[Find(i)]);
  }
  return 0;
}


Error: TLE

Fixed submission:

#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define LOCAL 1

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define dbg(...)                                                               \\
  cout << #__VA_ARGS__ ": [";                                                  \\
  for (auto e : __VA_ARGS__) {                                                 \\
    cout << e << ", ";                                                         \\
  }                                                                            \\
  cout << "]\\n"
#define dbg1(...) cout << #__VA_ARGS__ " " << __VA_ARGS__
#else
#define dbg(...) 1;
#define dbg1(...) 1;
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
constexpr ll R = 2e5 + 69, INF = 2e9 + 6969;

int n;
PII t[R];
int ile[R], rep[R];

int Find(int a) {
  if (a != rep[a])
    rep[a] = Find(rep[a]);
  return rep[a];
}
void Union(int a, int b) {
  if (ile[a] < ile[b])
    swap(a, b);
  ile[a] += ile[b];
  rep[b] = Find(a);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    ile[i] = 1;
    rep[i] = i;
    scanf("%d%d", &t[i].fi, &t[i].se);
  }
  for (int i = 1; i <= n; ++i) {
    int ans = 0;
    for (int j = 1; j <= n; ++j) {
      if (Find(i) == Find(j))
        continue;
      if (t[i].fi < t[j].fi && t[i].se < t[j].se) {
        Union(Find(i), Find(j));
      }
      //  if(t[j].fi < t[i].fi && t[j].se < t[i].se) ans++;
    }
    //  printf("%d\\n", ans+1);
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\\n", ile[Find(i)]);
  return 0;
}