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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

const int maxn = 200005;
int n, a[maxn], x[maxn], y[maxn], mx[maxn], mn[maxn], p[maxn];

bool cmp(int b, int c) { return x[b] < x[c]; }
int f(int b) {
  if (p[b] < 0)
    return b;
  return p[b] = f(p[b]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    a[i] = i;
  }
  sort(a + 1, a + n + 1, cmp);
  stack<int> stk;
  for (int i = 1; i <= n; i++) {
    mn[a[i]] = y[a[i]];
    mx[a[i]] = y[a[i]];
    p[a[i]] = -1;
    while (!stk.empty() && mx[a[i]] > mn[stk.top()]) {
      mn[a[i]] = min(mn[a[i]], mn[stk.top()]);
      mx[a[i]] = max(mx[a[i]], mx[stk.top()]);
      p[a[i]] += p[stk.top()];
      p[stk.top()] = a[i];
      stk.pop();
    }
    stk.push(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    cout << -p[f(i)] << "\\n";
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

const int maxn = 15;
int n, a[maxn], x[maxn], y[maxn], mx[maxn], mn[maxn], p[maxn];

bool cmp(int b, int c) { return x[b] < x[c]; }
int f(int b) {
  if (p[b] < 0)
    return b;
  return p[b] = f(p[b]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    a[i] = i;
  }
  sort(a + 1, a + n + 1, cmp);
  stack<int> stk;
  for (int i = 1; i <= n; i++) {
    mn[a[i]] = y[a[i]];
    mx[a[i]] = y[a[i]];
    p[a[i]] = -1;
    while (!stk.empty() && mx[a[i]] > mn[stk.top()]) {
      mn[a[i]] = min(mn[a[i]], mn[stk.top()]);
      mx[a[i]] = max(mx[a[i]], mx[stk.top()]);
      p[a[i]] += p[stk.top()];
      p[stk.top()] = a[i];
      stk.pop();
    }
    stk.push(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    cout << -p[f(i)] << "\\n";
  }
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  struct P {
    int x, y, index;
  };
  vector<P> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i].x >> p[i].y;
    p[i].index = i;
  }
  sort(p.begin(), p.end(), [](const P a, const P b) { return a.x < b.x; });
  vector<int> ans(n);
  int pre = -1;
  int min_y = n + 1;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (min_y == n - i + 1) {
      for (int j = i - 1; j > pre; --j) {
        ans[p[j].index] = cnt;
      }
      pre = i - 1;
      cnt = 1;
    } else {
      ++cnt;
    }
    min_y = min(min_y, p[i].y);
  }
  for (int i = n - 1; i > pre; --i) {
    ans[p[i].index] = cnt;
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<int> d;

void build(int n) { d.resize(n, -1); }

int find(int x) {
  if (d[x] < 0)
    return x;
  return d[x] = find(d[x]);
}

void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (d[x] > d[y])
    swap(x, y);
  d[x] += d[y];
  d[y] = x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<pair<pair<int, int>, int>> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i].first.first >> p[i].first.second;
    p[i].second = i;
  }
  sort(p.begin(), p.end());
  stack<pair<int, int>> st;
  build(n);
  for (int i = 0; i < n; ++i) {
    stack<pair<int, int>> new_st;
    bool suc = false;
    while (!st.empty()) {
      auto t = st.top();
      st.pop();
      new_st.push(t);
      if (t.first < p[i].first.second) {
        merge(p[i].second, t.second);
        suc = true;
      }
    }
    if (!suc) {
      pair<int, int> tt = make_pair(p[i].first.second, p[i].second);
      new_st.push(tt);
    }
    swap(st, new_st);
  }
  vector<int> ans(n);
  for (int i = 0; i < n; ++i) {
    ans[p[i].second] = -d[find(p[i].second)];
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <numeric>
#include <set>
#include <vector>
using namespace std;

class UnionFind {
  vector<int> par, rank, size;

public:
  explicit UnionFind(int n) : par(n), rank(n, 0), size(n, 1) {
    iota(par.begin(), par.end(), 0);
  }

  int find(int i) { return (par[i] == i) ? i : (par[i] = find(par[i])); }

  void unite(int i, int j) {
    int x = find(i), y = find(j);
    if (x != y) {
      if (rank[x] < rank[y]) {
        par[x] = y;
        size[y] += size[x];
      } else {
        par[y] = x;
        size[x] += size[y];
        if (rank[x] == rank[y])
          rank[x]++;
      }
    }
  }

  int card(int i) { return size[find(i)]; }
};

int main() {
  int n;
  cin >> n;
  int xy[n], xinv[n];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xy[x - 1] = y - 1;
    xinv[x - 1] = i;
  }
  set<pair<int, int>> s; // (y, x)
  UnionFind uf(n);
  for (int x = 0; x < n; x++) {
    pair<int, int> p = {xy[x], x};
    for (auto it = s.begin(); it != s.end() && it->first < xy[x];) {
      uf.unite(xinv[x], xinv[it->second]);
      if (p.first > it->first)
        p = *it;
      it = s.erase(it);
    }
    s.insert(p);
  }
  for (int i = 0; i < n; i++)
    cout << uf.card(i) << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <numeric>
#include <set>
#include <vector>
using namespace std;

class UnionFind {
  vector<int> par, rank, size;

public:
  explicit UnionFind(int n) : par(n), rank(n, 0), size(n, 1) {
    iota(par.begin(), par.end(), 0);
  }

  int find(int i) { return (par[i] == i) ? i : (par[i] = find(par[i])); }

  void unite(int i, int j) {
    int x = find(i), y = find(j);
    if (x != y) {
      if (rank[x] < rank[y]) {
        par[x] = y;
        size[y] += size[x];
      } else {
        par[y] = x;
        size[x] += size[y];
        if (rank[x] == rank[y])
          rank[x]++;
      }
    }
  }

  int card(int i) { return size[find(i)]; }
};

int main() {
  int n;
  cin >> n;
  int xy[n], xinv[n];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xy[x - 1] = y - 1;
    xinv[x - 1] = i;
  }
  set<pair<int, int>> s; // (y, x)
  UnionFind uf(n);
  for (int x = 0; x < n; x++) {
    pair<int, int> p = {xy[x], x};
    for (auto it = s.begin(); it != s.end() && it->first < xy[x];) {
      uf.unite(xinv[x], xinv[it->second]);
      it = s.erase(it);
      if (p.second > it->first)
        p = *it;
    }
    s.insert(p);
  }
  for (int i = 0; i < n; i++)
    cout << uf.card(i) << endl;
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
#define sz(a) ll(a.size())
ll x[200005], y[200005], ind[200005], par[200005], sz[200005], suf[200005];
bool comp(ll i, ll j) { return x[i] < x[j]; }
ll find(ll i) {
  if (par[i] == i)
    return i;
  return par[i] = find(par[i]);
}
void merge(ll x, ll y) {
  x = find(x), y = find(y);
  if (x == y)
    return;
  if (sz[x] < sz[y])
    swap(x, y);
  par[y] = x;
  sz[x] += sz[y];
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    ind[i] = i;
    par[i] = i;
    sz[i] = 1;
  }
  sort(ind + 1, ind + n + 1, comp);
  for (ll qc = n; qc >= 1; qc--) {
    ll i = ind[qc];
    suf[qc] = max(y[i], suf[qc + 1]);
  }
  for (ll i = 1; i <= n; i++) {
    ll j = i, lo = y[ind[i]];
    while (j <= n && lo <= suf[j]) {
      lo = min(lo, y[ind[j]]);
      merge(ind[i], ind[j]);
      j++;
    }
    i = j - 1;
  }
  for (ll i = 1; i <= n; i++)
    cout << sz[find(i)] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  clock_t z = clock();
  ll qc = 1;
  // cin>>qc;
  for (ll i = 1; i <= qc; i++) {
    solve();
  }
  debug("Total Time:%.4Lf\\n", (ld)(clock() - z) / CLOCKS_PER_SEC);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> p2;
#define sz(a) ll(a.size())
ll x[200005], y[200005], ind[200005], par[200005], sz[200005];
bool comp(ll i, ll j) { return x[i] < x[j]; }
ll find(ll i) {
  if (par[i] == i)
    return i;
  return par[i] = find(par[i]);
}
void merge(ll x, ll y) {
  x = find(x), y = find(y);
  if (x == y)
    return;
  if (sz[x] < sz[y])
    swap(x, y);
  par[y] = x;
  sz[x] += sz[y];
}
void solve() {
  ll n;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    ind[i] = i;
    par[i] = i;
    sz[i] = 1;
  }
  sort(ind + 1, ind + n + 1, comp);
  set<p2> s;
  for (ll i = 1; i <= n; i++)
    s.insert({y[ind[i]], ind[i]});
  for (ll qc = 1; qc <= n; qc++) {
    ll i = ind[qc];
    auto it = s.find({y[i], i});
    if (it == s.end())
      continue;
    s.erase(it);
    it = s.lower_bound({y[i] + 1, 0});
    if (s.empty())
      continue;
    set<p2> dum;
    while (it != s.end()) {
      merge(i, it->second);
      it++;
    }
    for (auto ut = dum.begin(); ut != dum.end(); ut++)
      s.erase(*ut);
  }
  for (ll i = 1; i <= n; i++)
    cout << sz[find(i)] << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  clock_t z = clock();
  ll qc = 1;
  // cin>>qc;
  for (ll i = 1; i <= qc; i++) {
    solve();
  }
  debug("Total Time:%.4Lf\\n", (ld)(clock() - z) / CLOCKS_PER_SEC);
}
Problem p02540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
struct UnionFind {
  vector<int> r;
  int n;
  UnionFind(int a) {
    n = a;
    r.assign(a + 1, -1);
  }
  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }
  void unite(int a, int b) {
    int x = root(a);
    int y = root(b);
    if (x == y)
      return;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return;
  }
  int query(int x) { return -r[root(x)]; }
};
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> pos;
  vector<int> f(n + 1);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    f[y] = i + 1;
    pos.push_back(make_pair(x, y));
  }
  sort(pos.begin(), pos.end());
  UnionFind uf(n);
  set<int, greater<int>> se;
  for (pair<int, int> p : pos) {
    auto it = se.insert(p.second).first;
    int id1 = f[p.second];
    auto i = it;
    i++;
    for (i; i != se.end(); i++) {
      int id2 = f[(*i)];
      uf.unite(id1, id2);
      i = se.erase(it);
      it = i;
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << uf.query(i) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
struct UnionFind {
  vector<int> r;
  int n;
  UnionFind(int a) {
    n = a;
    r.assign(a + 1, -1);
  }
  int root(int x) {
    if (r[x] < 0)
      return x;
    return r[x] = root(r[x]);
  }
  void unite(int a, int b) {
    int x = root(a);
    int y = root(b);
    if (x == y)
      return;
    if (r[x] > r[y])
      swap(x, y);
    r[x] += r[y];
    r[y] = x;
    return;
  }
  int query(int x) { return -r[root(x)]; }
};
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> pos;
  vector<int> f(n + 1);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    f[y] = i + 1;
    pos.push_back(make_pair(x, y));
  }
  sort(pos.begin(), pos.end());
  UnionFind uf(n);
  set<int, greater<int>> se;
  for (pair<int, int> p : pos) {
    auto it = se.insert(p.second).first;
    int id1 = f[p.second];
    for (++it; it != se.end(); it++) {
      int id2 = f[(*it)];
      uf.unite(id1, id2);
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << uf.query(i) << endl;
  }
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;

template <typename _T> inline void read(_T &f) {
  f = 0;
  _T fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      fu = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= fu;
}

template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x < 10)
    putchar(x + 48);
  else
    print(x / 10), putchar(x % 10 + 48);
}

template <typename T> void print(T x, char t) {
  print(x);
  putchar(t);
}

const int N = 2e5 + 5;

int a[N], b[N], ida[N], idb[N], vis[N], ans[N];
int n, cnt;

bool cmpa(int x, int y) { return a[x] < a[y]; }
bool cmpb(int x, int y) { return b[x] < b[y]; }

int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), read(b[i]), ida[i] = idb[i] = i;
  sort(ida + 1, ida + n + 1, cmpa);
  sort(idb + 1, idb + n + 1, cmpb);
  int las = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[ida[i]])
      vis[ida[i]] = 1, ++cnt;
    if (!vis[idb[n - i + 1]])
      vis[idb[n - i + 1]] = 1, ++cnt;
    if (cnt == i) {
      for (int j = las + 1; j <= i; j++)
        ans[ida[j]] = i - las;
      las = i;
    }
  }
  for (int i = 1; i <= n; i++)
    print(ans[i], '\\n');
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef long long ll;

template <typename _T> inline void read(_T &f) {
  f = 0;
  _T fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') {
      fu = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  f *= fu;
}

template <typename T> void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x < 10)
    putchar(x + 48);
  else
    print(x / 10), putchar(x % 10 + 48);
}

template <typename T> void print(T x, char t) {
  print(x);
  putchar(t);
}

const int N = 1e5 + 5;

int a[N], b[N], ida[N], idb[N], vis[N], ans[N];
int n, cnt;

bool cmpa(int x, int y) { return a[x] < a[y]; }
bool cmpb(int x, int y) { return b[x] < b[y]; }

int main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]), read(b[i]), ida[i] = idb[i] = i;
  sort(ida + 1, ida + n + 1, cmpa);
  sort(idb + 1, idb + n + 1, cmpb);
  int las = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[ida[i]])
      vis[ida[i]] = 1, ++cnt;
    if (!vis[idb[n - i + 1]])
      vis[idb[n - i + 1]] = 1, ++cnt;
    if (cnt == i) {
      for (int j = las + 1; j <= i; j++)
        ans[ida[j]] = i - las;
      las = i;
    }
  }
  for (int i = 1; i <= n; i++)
    print(ans[i], '\\n');
  return 0;
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 3e5 + 10;
const int mod = 998244353;
int n;
int k, m;
int a[N];
int b[N];

map<int, int> mp;
struct pp {
  int x;
  int y;
  int index;
} p[N];

int cmp(pp a, pp b) { return a.x < b.x; }
int num[N];
int ans[N];
int parent[N];
set<int> s;
void Init() {
  for (int i = 1; i <= n + 10; i++) {
    parent[i] = i;
    num[i] = 1;
  }
  return;
}

int get_boss(int v) {
  if (parent[v] == v)
    return v;
  else {
    // 路径压缩(使搜索祖宗的过程中，路径中搜索的结点的父亲的变为祖宗)
    parent[v] = get_boss(parent[v]);
    return parent[v];
  }
}

void Merge(int v, int u) // u合并到v
{
  int t1 = get_boss(v);
  int t2 = get_boss(u);
  if (t1 == t2)
    return;
  if (num[t1] < num[t2])
    swap(t1, t2);
  parent[t2] = t1;
  num[t1] += num[t2];

  return;
}

int main() {
  cin >> n;
  Init();
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].index = i;
  }

  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    mp[p[i].y] = i;
  }

  for (int i = 1; i <= n; i++) {
    auto it1 = s.lower_bound(p[i].y);
    // int stop=*it1;
    auto it2 = s.begin();
    int start = *it2;

    if (it1 != it2) {
      Merge(mp[start], mp[p[i].y]);
      it2++;
      for (; it2 != it1;) {
        Merge(mp[start], mp[*it2]);
        it2 = s.erase(it2);
      }
    } else {
      s.insert(p[i].y);
    }
  }

  for (int i = 1; i <= n; i++) {
    ans[p[i].index] = num[get_boss(mp[p[i].y])];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 3e5 + 10;
const int mod = 998244353;
int n;
int k, m;
int a[N];
int b[N];

map<int, int> mp;
struct pp {
  int x;
  int y;
  int index;
} p[N];

int cmp(pp a, pp b) { return a.x < b.x; }
int num[N];
int ans[N];
int parent[N];
set<int> s;
void Init() {
  for (int i = 1; i <= n + 10; i++) {
    parent[i] = i;
    num[i] = 1;
  }
  return;
}

int get_boss(int v) {
  if (parent[v] == v)
    return v;
  else {
    // 路径压缩(使搜索祖宗的过程中，路径中搜索的结点的父亲的变为祖宗)
    parent[v] = get_boss(parent[v]);
    return parent[v];
  }
}

void Merge(int v, int u) // u合并到v
{
  int t1 = get_boss(v);
  int t2 = get_boss(u);
  if (t1 == t2)
    return;
  if (num[t1] < num[t2])
    swap(t1, t2);
  parent[t2] = t1;
  num[t1] += num[t2];

  return;
}

int main() {
  cin >> n;
  Init();
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].index = i;
  }

  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    mp[p[i].y] = i;
  }

  for (int i = 1; i <= n; i++) {
    auto it1 = s.lower_bound(p[i].y);
    int stop = *it1;
    auto it2 = s.begin();
    int start = *it2;

    if (it1 != it2) {
      Merge(mp[start], mp[p[i].y]);
      it2++;
      for (; *it2 != stop;) {
        Merge(mp[start], mp[*it2]);
        it2 = s.erase(it2);
      }
    } else {
      s.insert(p[i].y);
    }
  }

  for (int i = 1; i <= n; i++) {
    ans[p[i].index] = num[get_boss(mp[p[i].y])];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 3e5 + 10;
const int mod = 998244353;
int n;
int k, m;
int a[N];
int b[N];

map<int, int> mp;
struct pp {
  int x;
  int y;
  int index;
} p[N];

int cmp(pp a, pp b) { return a.x < b.x; }
int num[N];
int ans[N];
int parent[N];
stack<int> s;
void Init() {
  for (int i = 1; i <= n + 10; i++) {
    parent[i] = i;
    num[i] = 1;
  }
  return;
}

int get_boss(int v) {
  if (parent[v] == v)
    return v;
  else {
    // 路径压缩(使搜索祖宗的过程中，路径中搜索的结点的父亲的变为祖宗)
    parent[v] = get_boss(parent[v]);
    return parent[v];
  }
}

void Merge(int v, int u) // u合并到v
{
  int t1 = get_boss(v);
  int t2 = get_boss(u);
  parent[t2] = t1;
  num[t1] += num[t2];
  return;
}

int main() {
  cin >> n;
  Init();
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].index = i;
  }

  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    mp[p[i].y] = i;
  }

  for (int i = 1; i <= n; i++) {
    int mn = p[i].y;
    while (!s.empty() && s.top() < p[i].y) {
      Merge(mp[s.top()], mp[p[i].y]);
      mn = min(mn, s.top());
      s.pop();
    }
    p[i].y = mn;
    s.push(mn);
  }

  for (int i = 1; i <= n; i++) {
    ans[p[i].index] = num[get_boss(mp[p[i].y])];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 3e5 + 10;
const int mod = 998244353;
int n;
int k, m;
int a[N];
int b[N];

map<int, int> mp;
struct pp {
  int x;
  int y;
  int index;
} p[N];

int cmp(pp a, pp b) { return a.x < b.x; }
int num[N];
int ans[N];
int parent[N];
set<int> s;
void Init() {
  for (int i = 1; i <= n + 10; i++) {
    parent[i] = i;
    num[i] = 1;
  }
  return;
}

int get_boss(int v) {
  if (parent[v] == v)
    return v;
  else {
    // 路径压缩(使搜索祖宗的过程中，路径中搜索的结点的父亲的变为祖宗)
    parent[v] = get_boss(parent[v]);
    return parent[v];
  }
}

void Merge(int v, int u) // u合并到v
{
  int t1 = get_boss(v);
  int t2 = get_boss(u);
  parent[t2] = t1;
  num[t1] += num[t2];
  return;
}

int main() {
  cin >> n;
  Init();
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
    p[i].index = i;
  }

  sort(p + 1, p + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    mp[p[i].y] = i;
  }

  for (int i = 1; i <= n; i++) {
    auto it1 = s.lower_bound(p[i].y);
    int stop = *it1;
    auto it2 = s.begin();
    int start = *it2;

    if (it1 != it2) {
      Merge(mp[start], mp[p[i].y]);
      it2++;
      for (; *it2 != stop;) {
        Merge(mp[start], mp[*it2]);
        it2 = s.erase(it2);
      }
    } else {
      s.insert(p[i].y);
    }
  }

  for (int i = 1; i <= n; i++) {
    ans[p[i].index] = num[get_boss(mp[p[i].y])];
  }
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}
Problem p02546 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  std::string s;
  cin >> s;
  int l = s.length();
  if (s[l - 1] == 's') {
    s.append("es");
  } else {
    s.append("s");
  }
  cout << s << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  int l = s.length();
  if (s[l - 1] == 's') {
    s.erase(l - 2, 1);
    s.append("es");
  } else {
    s.append("s");
  }
  cout << s << endl;

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

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

int main() {
  string X;
  cin >> X;
  char a = X[X.size() - 1];

  if (a == 's') {
    cout << X << "es" << endl;
  } else {
    cout << X << "s" << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 18446744073709551615) >= this->size() (which is 5)


Fixed submission:

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

int main() {
  string X;
  cin >> X;
  if (X.at(-1) == 's') {
    cout << X << "es" << endl;
  } else {
    cout << X << "s" << endl;
  }
}