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

/*******************************************************************************************************************************
 * _______                   __ ________   __________   __________ ____________
 *__________   ____      __   ______________ * | *      |   /        |  |_____
 *| |   _______| |   _______| |   _________| |   _______| |    \\    |  | |_____
 *_____|* | | |\\ | |/ /_|_  *** |  |     |  | |  |         |  |         |  | |
 *|         |  |\\ \\   |  |       |  |      * | | | \\| |\\   |       |  |     |  |
 *|  |___      |  |_______  |  |           |  |___      |  | \\ \\  |  |       |
 *|      * |  |     |  | |   ___|     |_______   | |  |           |   ___|     |
 *|  \\ \\ |  |       |  |      * |  |     |  | |  |                 |  | |  | |
 *|         |  |   \\ \\|  |       |  |      * |  |_____|  | |  |_______ _______|
 *| |  |_________  |  |_______  |  |    \\    |       |  |      *
 *                          |__|________| |__________| |__________|
 *|____________| |__________| |__|     \\___|       |__|      *
 *                                                                                                                             *
 *******************************************************************************************************************************/
#include <bits/stdc++.h>
#define CC1                                                                    \\
  time_t baygio = time(0);                                                     \\
  tm *ltm = localtime(&baygio);                                                \\
  cerr << "Time has passed: ";
#define CC2                                                                    \\
  int hour_of_contest = (StArT[0] - '0') * 10 + (StArT[1] - '0'),              \\
      min_of_contest = (StArT[2] - '0') * 10 + (StArT[3] - '0'),               \\
      sec_of_contest = 00;
#define CC3                                                                    \\
  if ((ltm->tm_min) < min_of_contest)                                          \\
    cerr << (ltm->tm_hour) - (hour_of_contest + 1) << ":";                     \\
  else
#define CC4 cerr << (ltm->tm_hour) - (hour_of_contest) << ":";
#define CC5                                                                    \\
  if ((ltm->tm_min) < min_of_contest)                                          \\
    cerr << 60 + ltm->tm_min - min_of_contest << ":";                          \\
  else
#define CC6                                                                    \\
  cerr << ltm->tm_min - min_of_contest << ":";                                 \\
  cerr << ltm->tm_sec << endl;
#define start_timing()                                                         \\
  cerr << endl << endl << "---------------------" << endl;                     \\
  CC1;                                                                         \\
  CC2;                                                                         \\
  CC3 CC4;                                                                     \\
  CC5 CC6;                                                                     \\
  return 0;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define foru(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, b, a) for (int i = (b); i >= (a); i--)
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define ok cout << "YES" << endl
#define kok cout << "NO" << endl
#define all(X) X.begin(), X.end()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && c != '-')
    ;
  if (c == '-') {
    c = getchar();
    nega = 1;
  }
  x = c - 48;
  while (isdigit(c = getchar())) {
    x = x * 10 + c - 48;
  }
  if (nega)
    x = -x;
}
template <typename T> void Write(T x) {
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  Write(x);
}
template <typename T> void make_unique(vector<T> &a) {
  sort(all(a));
  a.erase(unique(all(a)), a.end());
}
const int N = 2e5 + 5;
const int mod = 998244353;
const ld PI = acosl(-1.0);
const ld rad = PI / 180.000000000000000;
const string StArT = "2135";
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
inline ll bit(ll x, ll y) { return (x & (1ll << y)); }
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
inline int ranint(int l, int r) { return l + (rnd()) % (r - l + 1); }
inline ll ranll(ll l, ll r) { return l + (rnd64()) % (r - l + 1); }
inline ll lca(ll a, ll b) { return (a * b) / (__gcd(a, b)); }
ll powmod(ll a, ll b) {
  ll res = 1ll;
  a %= mod;
  while (b) {
    if (b & 1ll)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1ll;
  }
  return res % mod;
}
ll d[100];
void init() {
  ll cur = 1;
  d[1] = 1;
  foru(i, 2, 32) {
    cur = cur * 2 + 1;
    // cout <<
    // if((cur*(cur+1))/2<0) {cout << i ;break;};
    d[i] = (cur * (cur + 1)) / 2;
    d[i] += d[i - 1];
    //  cout << (cur*(cur+1))/2 << ' ';
  }
}
struct IT {
  vector<ll> tree, lazy;
  void fmat(int n) {
    tree.assign(4 * n, 0);
    lazy.assign(4 * n, 0);
  }
  void update(int node, int STart, int ENd, int l, int r, ll val) {
    if (lazy[node] != 0) {
      tree[node] += (ENd - STart + 1) * lazy[node];
      if (STart != ENd) {
        lazy[(node << 1)] += lazy[node];
        lazy[((node << 1) | (1))] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (STart > ENd or STart > r or ENd < l)
      return;
    if (STart >= l and ENd <= r) {
      tree[node] += (ENd - STart + 1) * val;
      if (STart != ENd) {
        // Not leaf node
        lazy[(node << 1)] += val;
        lazy[((node << 1) | (1))] += val;
      }
      return;
    }
    int mid = (STart + ENd) / 2;
    update((node << 1), STart, mid, l, r, val);
    update(((node << 1) | (1)), mid + 1, ENd, l, r, val);
    tree[node] = tree[(node << 1)] + tree[((node << 1) | (1))];
  }

  ll get(int node, int STart, int ENd, int l, int r) {
    if (STart > ENd or STart > r or ENd < l)
      return 0;
    if (lazy[node] != 0) {

      tree[node] += (ENd - STart + 1) * lazy[node];
      if (STart != ENd) {
        lazy[(node << 1)] += lazy[node];
        lazy[((node << 1) | (1))] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (STart >= l and ENd <= r)
      return tree[node];
    int mid = (STart + ENd) / 2;
    ll p1 = get((node << 1), STart, mid, l, r);
    ll p2 = get(((node << 1) | (1)), mid + 1, ENd, l, r);
    return (p1 + p2);
  }
};
int par[200005];
int dd[200005];
int ddd[200005];
int root(int u) {
  if (u == par[u])
    return u;
  return par[u] = root(par[u]);
}
void Merge(int u, int v) {
  int uu = root(u);
  int vv = root(v);
  if (uu == vv)
    return;
  if (uu > vv)
    swap(uu, vv);
  par[vv] = uu;
  dd[uu] += dd[vv];
  dd[vv] = 0;
  return;
}
int main() {
  fastio();
  int n;
  cin >> n;
  vector<pii> b(n + 5);
  vector<pair<int, pii>> a(n + 1);
  vector<int> kq(n + 5, 1);
  a[0] = {-INFINITY, {-INFINITY, 0}};
  foru(i, 1, n) {
    int x, y;
    cin >> x >> y;
    a[i] = {x, {y, i}};
    par[i] = i;
    dd[i] = 1;
  }
  sort(all(a));
  set<pii> s;
  s.insert({a[1].se.fi, a[1].se.se});
  foru(i, 2, n) {
    bool kt = true;
    for (auto x : s) {
      if (x.fi > a[i].se.fi) {
        //   kt = false;
        break;
      } else {
        Merge(x.se, a[i].se.se);
        kt = false;
      }
    }
    if (kt)
      s.insert({a[i].se.fi, a[i].se.se});
  }
  foru(i, 1, n) cout << dd[root(i)] << endl;
  /* ------------------------------------------------------------------------------------*/
  start_timing();
}


Error: TLE

Fixed submission:

/*******************************************************************************************************************************
 * _______                   __ ________   __________   __________ ____________
 *__________   ____      __   ______________ * | *      |   /        |  |_____
 *| |   _______| |   _______| |   _________| |   _______| |    \\    |  | |_____
 *_____|* | | |\\ | |/ /_|_  *** |  |     |  | |  |         |  |         |  | |
 *|         |  |\\ \\   |  |       |  |      * | | | \\| |\\   |       |  |     |  |
 *|  |___      |  |_______  |  |           |  |___      |  | \\ \\  |  |       |
 *|      * |  |     |  | |   ___|     |_______   | |  |           |   ___|     |
 *|  \\ \\ |  |       |  |      * |  |     |  | |  |                 |  | |  | |
 *|         |  |   \\ \\|  |       |  |      * |  |_____|  | |  |_______ _______|
 *| |  |_________  |  |_______  |  |    \\    |       |  |      *
 *                          |__|________| |__________| |__________|
 *|____________| |__________| |__|     \\___|       |__|      *
 *                                                                                                                             *
 *******************************************************************************************************************************/
#include <bits/stdc++.h>
#define CC1                                                                    \\
  time_t baygio = time(0);                                                     \\
  tm *ltm = localtime(&baygio);                                                \\
  cerr << "Time has passed: ";
#define CC2                                                                    \\
  int hour_of_contest = (StArT[0] - '0') * 10 + (StArT[1] - '0'),              \\
      min_of_contest = (StArT[2] - '0') * 10 + (StArT[3] - '0'),               \\
      sec_of_contest = 00;
#define CC3                                                                    \\
  if ((ltm->tm_min) < min_of_contest)                                          \\
    cerr << (ltm->tm_hour) - (hour_of_contest + 1) << ":";                     \\
  else
#define CC4 cerr << (ltm->tm_hour) - (hour_of_contest) << ":";
#define CC5                                                                    \\
  if ((ltm->tm_min) < min_of_contest)                                          \\
    cerr << 60 + ltm->tm_min - min_of_contest << ":";                          \\
  else
#define CC6                                                                    \\
  cerr << ltm->tm_min - min_of_contest << ":";                                 \\
  cerr << ltm->tm_sec << endl;
#define start_timing()                                                         \\
  cerr << endl << endl << "---------------------" << endl;                     \\
  CC1;                                                                         \\
  CC2;                                                                         \\
  CC3 CC4;                                                                     \\
  CC5 CC6;                                                                     \\
  return 0;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define foru(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, b, a) for (int i = (b); i >= (a); i--)
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define ok cout << "YES" << endl
#define kok cout << "NO" << endl
#define all(X) X.begin(), X.end()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define fastio()                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && c != '-')
    ;
  if (c == '-') {
    c = getchar();
    nega = 1;
  }
  x = c - 48;
  while (isdigit(c = getchar())) {
    x = x * 10 + c - 48;
  }
  if (nega)
    x = -x;
}
template <typename T> void Write(T x) {
  if (x > 9)
    Write(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  Write(x);
}
template <typename T> void make_unique(vector<T> &a) {
  sort(all(a));
  a.erase(unique(all(a)), a.end());
}
const int N = 2e5 + 5;
const int mod = 998244353;
const ld PI = acosl(-1.0);
const ld rad = PI / 180.000000000000000;
const string StArT = "2135";
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
inline ll bit(ll x, ll y) { return (x & (1ll << y)); }
mt19937 rnd(time(NULL));
mt19937_64 rnd64(time(NULL));
inline int ranint(int l, int r) { return l + (rnd()) % (r - l + 1); }
inline ll ranll(ll l, ll r) { return l + (rnd64()) % (r - l + 1); }
inline ll lca(ll a, ll b) { return (a * b) / (__gcd(a, b)); }
ll powmod(ll a, ll b) {
  ll res = 1ll;
  a %= mod;
  while (b) {
    if (b & 1ll)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1ll;
  }
  return res % mod;
}
ll d[100];
void init() {
  ll cur = 1;
  d[1] = 1;
  foru(i, 2, 32) {
    cur = cur * 2 + 1;
    // cout <<
    // if((cur*(cur+1))/2<0) {cout << i ;break;};
    d[i] = (cur * (cur + 1)) / 2;
    d[i] += d[i - 1];
    //  cout << (cur*(cur+1))/2 << ' ';
  }
}
struct IT {
  vector<ll> tree, lazy;
  void fmat(int n) {
    tree.assign(4 * n, 0);
    lazy.assign(4 * n, 0);
  }
  void update(int node, int STart, int ENd, int l, int r, ll val) {
    if (lazy[node] != 0) {
      tree[node] += (ENd - STart + 1) * lazy[node];
      if (STart != ENd) {
        lazy[(node << 1)] += lazy[node];
        lazy[((node << 1) | (1))] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (STart > ENd or STart > r or ENd < l)
      return;
    if (STart >= l and ENd <= r) {
      tree[node] += (ENd - STart + 1) * val;
      if (STart != ENd) {
        // Not leaf node
        lazy[(node << 1)] += val;
        lazy[((node << 1) | (1))] += val;
      }
      return;
    }
    int mid = (STart + ENd) / 2;
    update((node << 1), STart, mid, l, r, val);
    update(((node << 1) | (1)), mid + 1, ENd, l, r, val);
    tree[node] = tree[(node << 1)] + tree[((node << 1) | (1))];
  }

  ll get(int node, int STart, int ENd, int l, int r) {
    if (STart > ENd or STart > r or ENd < l)
      return 0;
    if (lazy[node] != 0) {

      tree[node] += (ENd - STart + 1) * lazy[node];
      if (STart != ENd) {
        lazy[(node << 1)] += lazy[node];
        lazy[((node << 1) | (1))] += lazy[node];
      }
      lazy[node] = 0;
    }
    if (STart >= l and ENd <= r)
      return tree[node];
    int mid = (STart + ENd) / 2;
    ll p1 = get((node << 1), STart, mid, l, r);
    ll p2 = get(((node << 1) | (1)), mid + 1, ENd, l, r);
    return (p1 + p2);
  }
};
int par[200005];
int dd[200005];
int ddd[200005];
int root(int u) {
  if (u == par[u])
    return u;
  return par[u] = root(par[u]);
}
void Merge(int u, int v) {
  int uu = root(u);
  int vv = root(v);
  if (uu == vv)
    return;
  if (uu > vv)
    swap(uu, vv);
  par[vv] = uu;
  dd[uu] += dd[vv];
  dd[vv] = 0;
  return;
}
int main() {
  fastio();
  int n;
  cin >> n;
  vector<pii> b(n + 5);
  vector<pair<int, pii>> a(n + 1);
  vector<int> kq(n + 5, 1);
  a[0] = {-INFINITY, {-INFINITY, 0}};
  foru(i, 1, n) {
    int x, y;
    cin >> x >> y;
    a[i] = {x, {y, i}};
    par[i] = i;
    dd[i] = 1;
  }
  sort(all(a));
  set<pii> s;
  s.insert({a[1].se.fi, a[1].se.se});
  foru(i, 2, n) {
    for (auto x : s) {
      if (x.fi > a[i].se.fi)
        break;
      else
        Merge(x.se, a[i].se.se);
    }
    s.insert({a[i].se.fi, a[i].se.se});
  }
  foru(i, 1, n) cout << dd[root(i)] << endl;
  /* ------------------------------------------------------------------------------------*/
  start_timing();
}