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

#include <bits/stdc++.h>

using namespace std;

class help {
public:
  int a, b, k;

  help(int x, int y, int z) {
    a = x;
    b = y;
    k = z;
  }
};

bool xsort(help &a, help &b) { return a.a < b.a; }
bool ysort(help &a, help &b) { return a.b < b.b; }

int p[200000], sz[200000];
int find(int v) { return (v == p[v]) ? v : p[v] = find(p[v]); }
void merge(int a, int b) {
  a = find(a), b = find(b);
  if (a == b)
    return;
  if (sz[a] < sz[b])
    swap(a, b);
  p[b] = a, sz[a] += sz[b];
}

int main() {

  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    p[i] = i;
  vector<help> arr;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    arr.push_back(help(a, b, i));
  }
  sort(arr.begin(), arr.end(), xsort);

  set<int> set;
  unordered_map<int, int> tmap;

  for (int i = 0; i < n; i++) {
    int val = -1 * arr[i].b;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  set.clear();
  tmap.clear();
  for (int i = n - 1; i >= 0; i--) {
    int val = arr[i].b;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  sort(arr.begin(), arr.end(), ysort);

  set.clear();
  tmap.clear();

  for (int i = 0; i < n; i++) {
    int val = -1 * arr[i].a;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  set.clear();
  tmap.clear();
  for (int i = n - 1; i >= 0; i--) {
    int val = arr[i].a;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }
  tmap.clear();
  for (int i = 0; i < n; i++) {
    tmap[find(i)]++;
  }
  for (int i = 0; i < n; i++)
    cout << tmap[find(i)] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

class help {
public:
  int a, b, k;

  help(int x, int y, int z) {
    a = x;
    b = y;
    k = z;
  }
};

bool xsort(help &a, help &b) { return a.a < b.a; }
bool ysort(help &a, help &b) { return a.b < b.b; }

unordered_map<int, int> par;
unordered_map<int, int> r;
int find(int a) {
  if (par.count(a) != 1) {
    par[a] = a;
  }

  if (par[a] == a)
    return a;
  else {
    int pr = find(par[a]);
    par[a] = pr;
    return pr;
  }
}

void merge(int a, int b) {

  int pra = find(a);

  int prb = find(b);

  if (pra != prb) {

    if (r[pra] > r[prb]) {
      par[prb] = pra;
    } else if (r[pra] < r[prb]) {
      par[pra] = prb;
    } else {
      r[pra] = r[pra] + 1;
      par[prb] = pra;
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<help> arr;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    arr.push_back(help(a, b, i));
  }
  sort(arr.begin(), arr.end(), xsort);

  set<int> set;
  unordered_map<int, int> tmap;

  for (int i = 0; i < n; i++) {
    int val = -1 * arr[i].b;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  set.clear();
  tmap.clear();
  for (int i = n - 1; i >= 0; i--) {
    int val = arr[i].b;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  sort(arr.begin(), arr.end(), ysort);

  set.clear();
  tmap.clear();

  for (int i = 0; i < n; i++) {
    int val = -1 * arr[i].a;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }

  set.clear();
  tmap.clear();
  for (int i = n - 1; i >= 0; i--) {
    int val = arr[i].a;
    auto it = set.upper_bound(val);
    if (it != set.end()) {
      int in = arr[tmap[*it]].k;
      merge(in, arr[i].k);
    }
    set.insert(val);
    tmap[val] = i;
  }
  tmap.clear();
  for (int i = 0; i < n; i++) {
    tmap[find(i)]++;
  }
  for (int i = 0; i < n; i++)
    cout << tmap[find(i)] << endl;
}