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

/*
Dont BLUNDER
*/

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


 */
// #define d long double
#define int long long
#define pii pair<int, int>
#define pb push_back
#define endl '\\n'
#define pi acos(-1)

#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
int const mod1 = 998244353;
int const mod = 1000000007;
#define bitcount(x) __builtin_popcountll(x)
#define l first
#define r second
const int inf = LLONG_MAX;
int toint(const string &s) {
  stringstream ss;
  ss << s;
  int x;
  ss >> x;
  return x;
}
string tostring(int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define haha                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

struct cmp {
  bool operator()(pii a, pii b) { return (a.l < b.l); }
};

int modexp(int x, int n) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2) % mod);
  }
}

int modinv(int n) { return modexp(n, mod - 2); }

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

bool comp(pair<pii, int> a, pair<pii, int> b) { return a.l.l < b.l.l; }
vector<int> adj[200001];
int vis[200001];
vector<int> cursz;

void dfs(int x) {
  vis[x] = 1;
  cursz.pb(x);
  for (auto i : adj[x]) {
    if (vis[i] == 0) {
      vis[i] = 1;
      dfs(i);
    }
  }
}

signed main() {
  haha
#ifndef ONLINE_JUDGE

      freopen("inp.txt", "r", stdin);

  freopen("out1.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  pair<pii, int> arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i].l.l >> arr[i].l.r;
    arr[i].r = i;
  }
  sort(arr, arr + n, comp);
  stack<pair<pii, int>> st;
  st.push({{arr[0].l.l, arr[0].l.r}, arr[0].r});
  // vis[arr[0].r]=1;
  vector<int> ans(n);
  for (int i = 1; i < n; i++) {
    int x = arr[i].l.l;
    int y = arr[i].l.r;
    int cind = arr[i].r;
    int y1 = y;
    while (st.size() && (y > st.top().l.r)) {
      int ind = st.top().r;
      y1 = min(y1, st.top().l.r);
      st.pop();
      adj[ind].pb(cind);
      adj[cind].pb(ind);
    }
    st.push({{x, y1}, cind});
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      dfs(i);
      int val = cursz.size();
      for (auto x : cursz) {
        ans[x] = val;
      }
      cursz.clear();
    }
  }
  for (auto x : ans)
    cout << x << " ";
}


Error: TLE

Fixed submission:

/*
Dont BLUNDER
*/

// #pragma GCC optimize("O3")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimization ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;


 */
// #define d long double
#define int long long
#define pii pair<int, int>
#define pb push_back
#define endl '\\n'
#define pi acos(-1)

#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
int const mod1 = 998244353;
int const mod = 1000000007;
#define bitcount(x) __builtin_popcountll(x)
#define l first
#define r second
const int inf = LLONG_MAX;
int toint(const string &s) {
  stringstream ss;
  ss << s;
  int x;
  ss >> x;
  return x;
}
string tostring(int number) {
  stringstream ss;
  ss << number;
  return ss.str();
}
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define haha                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

struct cmp {
  bool operator()(pii a, pii b) { return (a.l < b.l); }
};

int modexp(int x, int n) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2) % mod);
  }
}

int modinv(int n) { return modexp(n, mod - 2); }

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

bool comp(pair<pii, int> a, pair<pii, int> b) { return a.l.l < b.l.l; }
vector<int> adj[200001];
int vis[200001];
vector<int> cursz;

void dfs(int x) {
  vis[x] = 1;
  cursz.pb(x);
  for (auto i : adj[x]) {
    if (vis[i] == 0) {
      vis[i] = 1;
      dfs(i);
    }
  }
}

signed main() {
  haha
#ifndef ONLINE_JUDGE

      freopen("inp.txt", "r", stdin);

  freopen("out1.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  pair<pii, int> arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i].l.l >> arr[i].l.r;
    arr[i].r = i;
  }
  sort(arr, arr + n, comp);
  stack<pair<pii, int>> st;
  st.push({{arr[0].l.l, arr[0].l.r}, arr[0].r});
  // vis[arr[0].r]=1;
  vector<int> ans(n);
  for (int i = 1; i < n; i++) {
    int x = arr[i].l.l;
    int y = arr[i].l.r;
    int cind = arr[i].r;

    while (st.size() && (y > st.top().l.r)) {
      int ind = st.top().r;
      st.pop();
      adj[ind].pb(cind);
      adj[cind].pb(ind);
    }
    st.push({{x, y}, cind});
  }
  vector<int> check;
  while (st.size()) {
    int t1 = st.top().l.r;
    st.pop();
    check.pb(t1);
  }
  for (int i = 1; i < check.size(); i++) {
    if (check[i] > check[i - 1])
      while (1)
        ;
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      dfs(i);
      int val = cursz.size();
      for (auto x : cursz) {
        ans[x] = val;
      }
      cursz.clear();
    }
  }
  for (auto x : ans)
    cout << x << " ";
}