alexjercan/bugnet huggingface dataset:
Problem p02537 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
const int N = 6e5 + 7;

int n, a[N], k, tree[4 * N];

#define m (l + r) / 2
#define lson 2 * node
#define rson 2 * node + 1

void update(int pos, int v, int l, int r, int node) {
  if (l == r) {
    tree[node] = v;
    return;
  }
  if (pos <= m)
    update(pos, v, l, m, lson);
  else
    update(pos, v, m + 1, r, rson);
  tree[node] = max(tree[lson], tree[rson]);
}

int query(int ql, int qr, int l, int r, int node) {
  if (ql <= l && qr >= r) {
    return tree[node];
  }
  int ans = 0;
  if (ql <= m)
    ans = max(ans, query(ql, qr, l, m, lson));
  if (qr > m)
    ans = max(ans, query(ql, qr, m + 1, r, rson));
  return ans;
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int res = query(max(a[i] - k, 0), a[i] + k, 0, N, 1) + 1;
    ans = max(ans, res);
    update(a[i], res, 0, N, 1);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
const ll maxn = 6e5 + 7;
ll num[maxn];
ll tree[4 * maxn] = {0};
ll query(ll ql, ll qr, ll l, ll r, ll root) {
  if (ql <= l && qr >= r) {
    return tree[root];
  }
  ll mid = (l + r) >> 1;
  ll ans = 0;
  if (ql <= mid)
    ans = max(ans, query(ql, qr, l, mid, root * 2));
  if (qr > mid)
    ans = max(ans, query(ql, qr, mid + 1, r, root * 2 + 1));
  return ans;
}
void update(ll pos, ll val, ll l, ll r, ll root) {
  if (l == r) {
    tree[root] = val;
    return;
  }
  ll mid = (l + r) >> 1;
  if (pos <= mid)
    update(pos, val, l, mid, root * 2);
  else
    update(pos, val, mid + 1, r, root * 2 + 1);
  tree[root] = max(tree[root * 2], tree[root * 2 + 1]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, k;
  cin >> n >> k;
  for (ll i = 1; i <= n; i++) {
    cin >> num[i];
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll temp = query(max(0ll, num[i] - k), num[i] + k, 0, n, 1) + 1;
    ans = max(ans, temp);
    update(num[i], temp, 0, n, 1);
  }
  cout << ans << endl;
}
Problem p02537 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const long long INF = 0;
long long n, node[100000000];
void init(long long N_) {
  n = 1;
  while (n < N_)
    n *= 2;
  for (long long i = 0; i < 2 * n - 1; i++)
    node[i] = INF;
}
void update(long long a, long long x) {
  a += n - 1;
  node[a] = x;
  while (a > 0) {
    a = (a - 1) / 2;
    node[a] = max(node[2 * a + 1], node[2 * a + 2]);
  }
}
long long query(long long a, long long b, long long k, long long l,
                long long r) {
  if (b <= l || r <= a)
    return INF;
  if (a <= l && r <= b)
    return node[k];
  long long vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
  long long vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return max(vl, vr);
}

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  init(300001);
  for (long long i = 0; i < N; i++) {
    long long premin = max((long long)0, A[i] - K);
    long long premax = min((long long)300000, A[i] + K);
    long long num = query(premin, premax + 1, 0, 0, n);
    update(A[i], num + 1);
  }
  long long ans = query(0, n - 1, 0, 0, n);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  for (long long i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<long long> count(300001, 0);
  for (long long i = 0; i < N; i++) {
    long long precount = 0;
    for (long long j = A[i] - K; j <= A[i] + K; j++) {
      if (j > 0 && j <= 300000) {
        if (precount < count[j]) {
          precount = count[j];
        }
      }
    }
    count[A[i]] = precount + 1;
  }
  long long ans = 0;
  for (long long i = 1; i < 300001; i++) {
    if (ans < count[i]) {
      ans = count[i];
    }
  }
  cout << ans << endl;
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

int main() {
  ll N;
  cin >> N;
  vector<P> xy(N);
  vector<ll> original(N), xPlaceAtY(N);
  set<ll> yset;
  rep(i, N) {
    cin >> xy[i].first >> xy[i].second;
    --xy[i].first;
    --xy[i].second;
    yset.insert(xy[i].second);
    xPlaceAtY[xy[i].second] = xy[i].first;
    original[i] = xy[i].first;
  }

  sort(xy.begin(), xy.end());
  UnionFind uf = UnionFind(N);
  rep(i, N) {
    yset.erase(xy[i].second);
    if (uf.size(i) > 1) {
      continue;
    }
    // cerr << "i=" << i << "xy[i].first=" << xy[i].first << "xy[i].second=" <<
    // xy[i].second << endl;
    for (auto itr = yset.upper_bound(xy[i].second); itr != yset.end(); ++itr) {
      uf.unite(i, xPlaceAtY[*itr]);
    }
  }

  rep(i, N) { cout << uf.size(original[i]) << "\\n"; }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

int main() {
  ll N;
  cin >> N;
  vector<P> xy(N);
  vector<ll> original(N), xPlaceAtY(N);
  set<ll> yset;
  rep(i, N) {
    cin >> xy[i].first >> xy[i].second;
    --xy[i].first;
    --xy[i].second;
    yset.insert(xy[i].second);
    xPlaceAtY[xy[i].second] = xy[i].first;
    original[i] = xy[i].first;
  }

  sort(xy.begin(), xy.end());
  UnionFind uf = UnionFind(N);
  rep(i, N) {
    for (auto itr = yset.upper_bound(xy[i].second); itr != yset.end(); ++itr) {
      uf.unite(i, xPlaceAtY[*itr]);
    }
    yset.erase(xy[i].second);
  }

  rep(i, N) { cout << uf.size(original[i]) << "\\n"; }

  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;

const int N = 2e5 + 5;

int n, x, y;
vector<pair<int, int>> P;
int A[N], tree[N], X[N], Y[N];
int sz[N], par[N];

int find_set(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find_set(par[x]);
}

void union_set(int x, int y) {
  if (sz[x] < sz[y]) {
    swap(x, y);
  }
  sz[x] += sz[y];
  par[y] = x;
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    P.pb({x, i});
    X[i] = x;
    Y[i] = y;
    A[y] = i;
    par[i] = i;
    sz[i] = 1;
  }

  sort(P.begin(), P.end());
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int ind = P[i].ss;
    int y = Y[ind];
    if (v.size() == 0) {
      v.pb(y);
    } else {
      int x = v.back();
      while (v.size() && v.back() < y) {
        int a = find_set(A[y]);
        int b = find_set(A[v.back()]);
        if (a != b)
          union_set(a, b);
        v.pop_back();
      }
      x = min(x, y);
      v.pb(x);
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << sz[find_set(i)] << " ";
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;

const int N = 2e5 + 5;

int n, x, y;
vector<pair<int, int>> P;
int A[N], tree[N], X[N], Y[N];
int sz[N], par[N];

int find_set(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find_set(par[x]);
}

void union_set(int x, int y) {
  if (sz[x] < sz[y]) {
    swap(x, y);
  }
  sz[x] += sz[y];
  par[y] = x;
}

int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    P.pb({x, i});
    X[i] = x;
    Y[i] = y;
    A[y] = i;
    par[i] = i;
    sz[i] = 1;
  }

  sort(P.begin(), P.end());
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int ind = P[i].ss;
    int y = Y[ind];
    if (v.size() == 0) {
      v.pb(y);
    } else if (v.back() >= y) {
      v.pb(y);
    } else {
      int x = v.back();
      while (v.back() < y) {
        int a = find_set(A[y]);
        int b = find_set(A[v.back()]);
        if (a != b)
          union_set(a, b);
        v.pop_back();
      }
      v.pb(x);
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << sz[find_set(i)] << " ";
  }
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int N = 200010;
int n;
int tr[N];
int res[N];
struct node {
  int x;
  int y;
  int id;
  bool operator<(const node &W) const {
    if (x != W.x)
      return x < W.x;
    return y < W.y;
  }
} q[N];
int p[N];
int sz[N];
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  return p[x];
}
set<pair<int, int>> s;
set<pair<int, int>> S;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &q[i].x, &q[i].y), q[i].id = i, p[i] = i, sz[i] = 1;
  sort(q + 1, q + n + 1);
  for (int i = 1; i <= n; i++) {
    if (s.empty())
      s.insert({q[i].y, q[i].id});
    auto it = s.lower_bound({q[i].y, 0});
    int b = q[i].id;
    b = find(b);
    bool flag = false;
    for (set<pair<int, int>>::iterator x = s.begin(); x != it; x++) {
      flag = true;
      int a = x->second;
      a = find(a);
      if (a != b) {
        p[a] = b;
        sz[b] += sz[a];
        //  break;
      }
    }
    if (!flag)
      s.insert({q[i].y, q[i].id});
  }
  for (int i = 1; i <= n; i++)
    printf("%d\\n", sz[find(i)]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
using namespace std;
const int N = 200010;
int n;
int tr[N];
int res[N];
struct node {
  int x;
  int y;
  int id;
  bool operator<(const node &W) const {
    if (x != W.x)
      return x < W.x;
    return y < W.y;
  }
} q[N];
int p[N];
int sz[N];
int find(int x) {
  if (p[x] != x)
    p[x] = find(p[x]);
  return p[x];
}
set<pair<int, int>> s;
set<pair<int, int>> S;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &q[i].x, &q[i].y), q[i].id = i, p[i] = i, sz[i] = 1;
  sort(q + 1, q + n + 1);
  for (int i = 1; i <= n; i++) {
    s.insert({q[i].y, q[i].id});
    auto it = s.lower_bound({q[i].y, 0});
    int b = q[i].id;
    b = find(b);
    for (set<pair<int, int>>::iterator x = s.begin(); x != it; x++) {
      int a = x->second;
      a = find(a);
      if (a != b) {
        p[a] = b;
        sz[b] += sz[a];
        //  break;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    printf("%d\\n", sz[find(i)]);
  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define maxn 200005
#define pii pair<int, int>
#define mk make_pair
using namespace std;

int n;

struct node {
  int x, y, id;
} poi[maxn];

bool cmp1(node i, node j) { return i.x < j.x; }

int ans[maxn], siz[maxn];

set<pii> sets;
set<pii>::iterator it;

int fa[maxn];

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> poi[i].x >> poi[i].y, poi[i].id = fa[i] = i, siz[i] = 1;
  sort(poi + 1, poi + n + 1, cmp1);
  sets.insert(mk(9999999, 9999999));
  for (int i = 1; i <= n; i++) {
    it = sets.begin();
    if ((*it).first < poi[i].y) {
      while ((*it).first < poi[i].y) {
        int fx = find(poi[i].id), fy = find((*it).second);
        if (fx != fy) {
          fa[fx] = fy;
          siz[fy] += siz[fx];
        }
        it++;
      }
    } else
      sets.insert(mk(poi[i].y, poi[i].id));
  }
  for (int i = 1; i <= n; i++)
    cout << siz[find(i)] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define maxn 200005
#define pii pair<int, int>
#define mk make_pair
using namespace std;

int n;

struct node {
  int x, y, id;
} poi[maxn];

bool cmp1(node i, node j) { return i.x < j.x; }

int ans[maxn], siz[maxn];

set<pii> sets;
set<pii>::iterator it;

int fa[maxn];

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> poi[i].x >> poi[i].y, poi[i].id = fa[i] = i, siz[i] = 1;
  sort(poi + 1, poi + n + 1, cmp1);
  sets.insert(mk(9999999, 9999999));
  for (int i = 1; i <= n; i++) {
    it = sets.begin();
    while ((*it).first < poi[i].y) {

      int fx = find(poi[i].id), fy = find((*it).second);
      if (fx != fy) {
        fa[fx] = fy;
        siz[fy] += siz[fx];
      }
      it++;
    }
    sets.insert(mk(poi[i].y, poi[i].id));
  }
  for (int i = 1; i <= n; i++)
    cout << siz[find(i)] << endl;
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

#define PII pair<int, int>

const int N = 200010, INF = 0x3f3f3f3f;

typedef struct {
  int x, y, pos;
} state;

state a[N];
int cnt[N], p[N];
int n;
PII stk[N];
int tt = -1;

int cmp(const state &a, const state &b) { return a.x < b.x; }

int find(int x) {
  if (x != p[x])
    p[x] = find(p[x]);
  return p[x];
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;

    a[i] = {x, y, i};
  }

  for (int i = 0; i < n; i++)
    p[i] = i, cnt[i] = 1;

  sort(a, a + n, cmp);

  for (int i = 0; i < n; i++) {
    if (tt == -1)
      stk[++tt] = {a[i].pos, a[i].y};
    else {
      int minv = a[i].y;
      while (~tt && a[i].y > stk[tt].second) {
        minv = min(minv, stk[tt].second);
        int x = find(a[i].pos), y = find(stk[tt].first);
        if (x != y) {
          p[x] = y;
          cnt[y] += cnt[x];
        }
        tt--;
      }
      stk[++tt] = {a[i].pos, minv};
    }
  }

  for (int i = 0; i < n; i++)
    cout << cnt[find(i)] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define PII pair<int, int>
#define x first
#define y second

const int N = 200010;

PII a[N];
int cnt[N], p[N];
int n;

int find(int x) {
  if (x != p[x])
    x = find(p[x]);
  return p[x];
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[i] = {x, y};
  }

  for (int i = 0; i < n; i++)
    cnt[i] = 1, p[i] = i;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i].x < a[j].x && a[i].y < a[j].y ||
          a[i].x > a[j].x && a[i].y > a[j].y) {
        int x = find(i), y = find(j);
        if (a[x] != a[y]) {
          p[y] = x;
          cnt[x] += cnt[y];
        }
      }
    }
  }

  for (int i = 0; i < n; i++)
    cout << cnt[find(i)] << endl;

  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>
int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[200001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int ufind(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = ufind(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void umerge(int x, int y) {
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}

int main() {
  int x, y, flag;
  cin >> n;
  vector<int> ans(n + 1), dp(n + 5);
  set<pair<int, int>> s;
  vector<pair<pair<int, int>, int>> v;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back({{x, y}, i});
  }
  sort(v.begin(), v.end());
  init();
  s.insert({v[0].first.second, v[0].second});
  for (int i = 1; i < n; i++) {
    y = min(v[i].first.second, s.begin()->first);
    while (!s.empty() && s.begin()->first < v[i].first.second) {
      umerge(v[i].second, s.begin()->second);
      s.erase(s.begin());
    }
    s.insert({y, v[i].second});
  }
  for (int i = 0; i < n; i++) {
    x = ufind(i);
    cout << g[x].node << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>
int n, m;

struct unionfind {
  int parent;
  int node;
  int edge;
};
unionfind g[100001];

void init() {
  for (int i = 0; i <= n; i++) {
    g[i].parent = i;
    g[i].node = 1;
    g[i].edge = 0;
  }
}

int ufind(int x) {
  if (x == g[x].parent) {
    return x;
  }
  int res = ufind(g[x].parent);
  g[x].node = g[res].node;
  g[x].edge = g[res].edge;
  g[x].parent = res;
  return res;
}

void umerge(int x, int y) {
  g[x].node += g[y].node;
  g[x].edge += g[y].edge + 1;
  g[y].parent = x;
}

int main() {
  int x, y, flag;
  cin >> n;
  vector<int> ans(n + 1), dp(n + 5);
  set<pair<int, int>> s;
  vector<pair<pair<int, int>, int>> v;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back({{x, y}, i});
  }
  sort(v.begin(), v.end());
  init();
  s.insert({v[0].first.second, v[0].second});
  for (int i = 1; i < n; i++) {
    y = min(v[i].first.second, s.begin()->first);
    while (!s.empty() && s.begin()->first < v[i].first.second) {
      umerge(v[i].second, s.begin()->second);
      s.erase(s.begin());
    }
    s.insert({y, v[i].second});
  }
  for (int i = 0; i < n; i++) {
    x = ufind(i);
    cout << g[x].node << endl;
  }
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\\n';

struct UnionFind {
  // 各要素が属する集合の代表(根)を管理する
  // もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
  vector<int> data;

  UnionFind(int sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    bool is_union = (x != y);
    if (is_union) {
      if (data[x] > data[y])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return is_union;
  }

  int find(int x) {
    if (data[x] < 0) { // 要素xが根である
      return x;
    } else {
      data[x] = find(
          data[x]); // data[x]がxの属する集合の根でない場合、根になるよう更新される
      return data[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }

  int size(int x) { return -data[find(x)]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<int> xtoi(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    --x[i];
    --y[i];
    xtoi[x[i]] = i;
  }

  map<int, int> memo;
  UnionFind uf(n);
  for (int xx = n - 1; xx >= 0; --xx) {
    int i = xtoi[xx];
    int lasti = i;
    for (auto it = memo.lower_bound(y[i]); it != memo.end();) {
      uf.unite(i, it->second);
      lasti = it->second;
      it = memo.erase(it);
    }
    memo[y[lasti]] = lasti;
  }

  for (int i = 0; i < n; i++) {
    cout << uf.size(i) << newl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\\n';

struct UnionFind {
  // 各要素が属する集合の代表(根)を管理する
  // もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
  vector<int> data;

  UnionFind(int sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    bool is_union = (x != y);
    if (is_union) {
      if (data[x] > data[y])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return is_union;
  }

  int find(int x) {
    if (data[x] < 0) { // 要素xが根である
      return x;
    } else {
      data[x] = find(
          data[x]); // data[x]がxの属する集合の根でない場合、根になるよう更新される
      return data[x];
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }

  int size(int x) { return -data[find(x)]; }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<int> x(n), y(n);
  vector<int> xtoi(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    --x[i];
    --y[i];
    xtoi[x[i]] = i;
  }

  map<int, int> memo;
  UnionFind uf(n);
  for (int xx = n - 1; xx >= 0; --xx) {
    int i = xtoi[xx];
    for (auto it = memo.lower_bound(y[i]); it != memo.end(); ++it) {
      uf.unite(i, it->second);
    }
    memo[y[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    cout << uf.size(i) << newl;
  }

  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
/*#define LL long long
#define DB long double*/
using namespace std;
// #define P 998244353
// inline int ad(int x,int y){return (x+y>=P)?(x+y-P):(x+y);}
// inline int mt(int x,int y){return 1ll*x*y%P;}
// inline void mo(int &x){x=(x>=P)?(x-P):x; x=(x<0)?(x+P):x;}
// template<typename T>inline void ckmx(T &x,T y){x=max(x,y);}
// template<typename T>inline void ckmi(T &x,T y){x=min(x,y);}
template <typename T> void rd(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  x *= f;
}
int n;
int x[200010], y[200010];
int id[200010];
int fa[200010], cnt[200010];
int gtf(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = gtf(fa[x]);
}
void mg(int x, int y) {
  x = gtf(x);
  y = gtf(y);
  fa[x] = y;
  cnt[y] += cnt[x];
}
int q[200010], tp = 0, mi;
int main() {
  rd(n);
  for (int i = 1; i <= n; ++i)
    rd(x[i]), rd(y[i]), id[x[i]] = i, fa[i] = i, cnt[i]++;
  mi = id[1];
  for (int i = 2; i <= n; ++i) {
    int nw = id[i];
    if (y[nw] < y[mi])
      q[++tp] = mi, mi = nw;
    else {
      while (tp > 0 && y[nw] > y[q[tp]])
        mg(nw, q[tp]), --tp;
      mg(nw, mi);
    }
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\\n", cnt[gtf(i)]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define DB long double
using namespace std;
// #define P 998244353
// inline int ad(int x,int y){return (x+y>=P)?(x+y-P):(x+y);}
// inline int mt(int x,int y){return 1ll*x*y%P;}
// inline void mo(int &x){x=(x>=P)?(x-P):x; x=(x<0)?(x+P):x;}
template <typename T> inline void ckmx(T &x, T y) { x = max(x, y); }
template <typename T> inline void ckmi(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - 48;
  x *= f;
}
int n;
int x[200010], y[200010];
int id[200010];
int fa[200010], cnt[200010];
int gtf(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = gtf(fa[x]);
}
int mg(int x, int y) {
  x = gtf(x);
  y = gtf(y);
  fa[x] = y;
  cnt[y] += cnt[x];
}
int q[200010], top, mi;
int main() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(x[i]), read(y[i]), id[x[i]] = i, fa[i] = i, cnt[i]++;
  mi = id[1];
  for (int i = 2; i <= n; ++i) {
    int nw = id[i];
    if (y[nw] < y[mi])
      q[++top] = mi, mi = nw;
    else {
      while (top && y[nw] > y[q[top]])
        mg(nw, q[top]), --top;
      mg(nw, mi);
    }
  }
  for (int i = 1; i <= n; ++i)
    printf("%d\\n", cnt[gtf(i)]);
  return 0;
}