prithivMLmods/Coder-Stat huggingface dataset:
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

struct UnionFind {
  vector<int> d;
  // n要素で初期化
  UnionFind(int n) : d(n, -1) {}
  // 木の根を決める
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  // xとy属する集合を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  // xとyが同じ集合に属するか否か
  bool same(int x, int y) { return root(x) == root(y); }
  // xの属する集合のサイズ
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;

  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!uf.same(i, i + 1)) {
      uf.unite(i, i + 1);
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

struct UnionFind {
  vector<int> d;
  // n要素で初期化
  UnionFind(int n) : d(n, -1) {}
  // 木の根を決める
  int root(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = root(d[x]);
  }
  // xとy属する集合を併合
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  // xとyが同じ集合に属するか否か
  bool same(int x, int y) { return root(x) == root(y); }
  // xの属する集合のサイズ
  int size(int x) { return -d[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;

  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b);
  }

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!uf.same(i, i + 1)) {
      uf.unite(i, i + 1);
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02536 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7:合同式の法

int main() {

  ll N, M;
  cin >> N >> M;

  Graph G(N);
  for (ll i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  // BFS のためのデータ構造
  vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
  // queue<ll> que;
  ll total = 0;

  // BFS 開始 (キューが空になるまで探索を行う)
  REP(i, N) {
    if (dist[i] == -1) {
      dist[i] = total;
      // cout << dist[i] << endl;
      total++;
      queue<ll> que;
      que.push(i);
      while (!que.empty()) {
        ll v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (ll nv : G[v]) {
          if (dist[nv] != -1)
            continue; // すでに発見済みの頂点は探索しない

          // 新たな白色頂点 nv について距離情報を更新してキューに追加する
          dist[nv] = dist[v];
          que.push(nv);
        }
      }
    }
  }
  cout << total - 1 << endl;

  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
using Graph = vector<vector<ll>>;

#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())

#define INF 1000000000000 // 10^12
#define MOD 1000000007    // 10^9+7:合同式の法

int main() {

  ll N, M;
  cin >> N >> M;

  Graph G(N);
  for (ll i = 0; i < M; ++i) {
    ll a, b;
    cin >> a >> b;
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }

  // BFS のためのデータ構造
  vector<ll> dist(N, -1); // 全頂点を「未訪問」に初期化
  // queue<ll> que;
  ll total = 0;

  // BFS 開始 (キューが空になるまで探索を行う)
  REP(i, N) {
    if (dist[i] == -1) {
      dist[i] = total;
      // cout << dist[i] << endl;
      total++;
      queue<ll> que;
      que.push(i);
      while (!que.empty()) {
        ll v = que.front(); // キューから先頭頂点を取り出す
        que.pop();

        // v から辿れる頂点をすべて調べる
        for (ll nv : G[v]) {
          if (dist[nv] != -1)
            continue; // すでに発見済みの頂点は探索しない

          // 新たな白色頂点 nv について距離情報を更新してキューに追加する
          dist[nv] = dist[v];
          que.push(nv);
        }
      }
    }
  }
  cout << total - 1 << endl;

  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> T[20];

int query(int h, int k, int i, int j) {
  int m = (k << h) + (1 << h - 1);
  if (i == (k << h) && j == (k + 1 << h) - 1)
    return T[h][k];
  if (j < m)
    return query(h - 1, k << 1, i, j);
  if (i >= m)
    return query(h - 1, k << 1 | 1, i, j);
  return max(query(h - 1, k << 1, i, m - 1), query(h - 1, k << 1 | 1, m, j));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int d = ceil(log2(n + 5));
  for (int i = 0; i <= d; i++) {
    T[i].clear();
    T[i].resize(1 << (d - i), 0);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    int q = query(d, 0, max(a - k, 0), min(a + k, 300000));
    ans = max(ans, q + 1);

    int p = a;
    T[0][p] = max(T[0][p], q + 1);
    p >>= 1;
    for (int j = 1; j <= d; j++) {
      T[j][p] = max(T[j - 1][p << 1], T[j - 1][p << 1 | 1]);
      p >>= 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> T[20];

int query(int h, int k, int i, int j) {
  int m = (k << h) + (1 << h - 1);
  if (i == (k << h) && j == (k + 1 << h) - 1)
    return T[h][k];
  if (j < m)
    return query(h - 1, k << 1, i, j);
  if (i >= m)
    return query(h - 1, k << 1 | 1, i, j);
  return max(query(h - 1, k << 1, i, m - 1), query(h - 1, k << 1 | 1, m, j));
}

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int d = 19;
  for (int i = 0; i <= d; i++) {
    T[i].clear();
    T[i].resize(1 << (d - i), 0);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    int q = query(d, 0, max(a - k, 0), min(a + k, 300000));
    ans = max(ans, q + 1);

    int p = a;
    T[0][p] = max(T[0][p], q + 1);
    p >>= 1;
    for (int j = 1; j <= d; j++) {
      T[j][p] = max(T[j - 1][p << 1], T[j - 1][p << 1 | 1]);
      p >>= 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
#define println std::cout << std::endl;
using LL = long long;
#define lrt rt << 1
#define rrt rt << 1 | 1
#define lson l, m, lrt
#define rson m + 1, r, rrt
const int N = 3e5 + 2;
int mx[N * 3];

void update(int p, int val, int l, int r, int rt) {
  mx[rt] = std::max(mx[rt], val);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (p > m)
    update(p, val, rson);
  else
    update(p, val, lson);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && R >= r)
    return mx[rt];
  int m = (l + r) >> 1;
  int ans = 0;
  if (L <= m)
    ans = std::max(ans, query(L, R, lson));
  if (R > m)
    ans = std::max(ans, query(L, R, rson));
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  for (int i = 0, x; i < n; ++i) {
    std::cin >> x;
    ++x;
    int now = query(x - k, x + k, 1, N, 1);
    update(x, now + 1, 1, N, 1);
  }
  print(mx[1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
#define print(x) std::cout << (x) << std::endl
#define println std::cout << std::endl;
using LL = long long;
#define lrt rt << 1
#define rrt rt << 1 | 1
#define lson l, m, lrt
#define rson m + 1, r, rrt
const int N = 3e5 + 2;
int mx[N * 5];

void update(int p, int val, int l, int r, int rt) {
  mx[rt] = std::max(mx[rt], val);
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (p > m)
    update(p, val, rson);
  else
    update(p, val, lson);
}
int query(int L, int R, int l, int r, int rt) {
  if (L <= l && R >= r)
    return mx[rt];
  int m = (l + r) >> 1;
  int ans = 0;
  if (L <= m)
    ans = std::max(ans, query(L, R, lson));
  if (R > m)
    ans = std::max(ans, query(L, R, rson));
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n, k;
  std::cin >> n >> k;
  for (int i = 0, x; i < n; ++i) {
    std::cin >> x;
    ++x;
    int now = query(x - k, x + k, 1, N, 1);
    update(x, now + 1, 1, N, 1);
  }
  print(mx[1]);
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

#define N 100010

struct Data {
  LL val, idx;
};

LL a[N], c[N], q[N];
LL Maxn[N * 4];
Data b[N];
int n;
LL ans, d;

bool cmp(Data x, Data y) { return (x.val < y.val); }

void update(int o, int l, int r, int x, LL v) {
  if (l == x && r == x) {
    Maxn[o] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(o * 2, l, mid, x, v);
  if (x > mid)
    update(o * 2 + 1, mid + 1, r, x, v);
  Maxn[o] = max(Maxn[o * 2], Maxn[o * 2 + 1]);
}

void query(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    ans = max(Maxn[o], ans);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(o * 2, l, mid, x, y);
  if (y > mid)
    query(o * 2 + 1, mid + 1, r, x, y);
}

int main() {
  cin >> n >> d;
  memset(Maxn, 0, sizeof(Maxn));
  memset(q, 0, sizeof(q));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i].val = a[i];
    b[i].idx = i;
  }

  sort(b, b + n, cmp);

  for (int i = 0; i < n; i++) {
    c[i] = b[i].val;
    q[b[i].idx] = i;
  }
  LL tot = 1;
  for (int i = 0; i < n; i++) {
    LL l = a[i] - d, r = a[i] + d;
    int ll = lower_bound(c, c + n, l) - c;
    int rr = upper_bound(c, c + n, r) - c - 1;
    ans = 0;
    query(1, 0, n - 1, ll, rr);
    if (ans == 0)
      update(1, 0, n - 1, q[i], 1);
    else {
      update(1, 0, n - 1, q[i], ans + 1);
      if (ans + 1 > tot)
        tot = ans + 1;
    }
  }
  cout << tot << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long LL;

#define N 1000010

struct Data {
  LL val, idx;
};

LL a[N], c[N], q[N];
LL Maxn[N * 4];
Data b[N];
int n;
LL ans, d;

bool cmp(Data x, Data y) { return (x.val < y.val); }

void update(int o, int l, int r, int x, LL v) {
  if (l == x && r == x) {
    Maxn[o] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(o * 2, l, mid, x, v);
  if (x > mid)
    update(o * 2 + 1, mid + 1, r, x, v);
  Maxn[o] = max(Maxn[o * 2], Maxn[o * 2 + 1]);
}

void query(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    ans = max(Maxn[o], ans);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    query(o * 2, l, mid, x, y);
  if (y > mid)
    query(o * 2 + 1, mid + 1, r, x, y);
}

int main() {
  cin >> n >> d;
  memset(Maxn, 0, sizeof(Maxn));
  memset(q, 0, sizeof(q));
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i].val = a[i];
    b[i].idx = i;
  }

  sort(b, b + n, cmp);

  for (int i = 0; i < n; i++) {
    c[i] = b[i].val;
    q[b[i].idx] = i;
  }
  LL tot = 1;
  for (int i = 0; i < n; i++) {
    LL l = a[i] - d, r = a[i] + d;
    int ll = lower_bound(c, c + n, l) - c;
    int rr = upper_bound(c, c + n, r) - c - 1;
    ans = 0;
    query(1, 0, n - 1, ll, rr);
    if (ans == 0)
      update(1, 0, n - 1, q[i], 1);
    else {
      update(1, 0, n - 1, q[i], ans + 1);
      if (ans + 1 > tot)
        tot = ans + 1;
    }
  }
  cout << tot << endl;
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
ll c[600005] = {0}, tree[1200005] = {0};
ll query(ll ql, ll qr, ll s, ll e, ll in) {
  if (ql > e || qr < s || s > e)
    return -1;
  if (ql <= s && qr >= e) {
    return tree[in];
  }

  int mid = (s + e) / 2;
  // cout<<"Okay";
  return max(query(ql, qr, s, mid, 2 * in + 1),
             query(ql, qr, mid + 1, e, 2 * in + 2));
}
void update(ll s, ll e, ll in, ll key, ll up) {
  if (s > e)
    return;
  if (s == e && s == key) {
    tree[in] = up;
    return;
  } else if (s == e)
    return;
  ll mid = (s + e) / 2;

  update(s, mid, 2 * in + 1, key, up);

  update(mid + 1, e, 2 * in + 2, key, up);
  tree[in] = max(tree[2 * in + 1], tree[2 * in + 2]);
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll curr = query(max(0ll, a[i] - k), min(300000ll, a[i] + k), 0, 300000, 0);
    // cout<<curr<<endl;
    ans = max(ans, curr + 1);
    update(0, 300000, 0, a[i], curr + 1);
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
ll c[600005] = {0}, tree[1200005] = {0};
ll query(ll ql, ll qr, ll s, ll e, ll in) {
  if (ql > e || qr < s || s > e)
    return -1;
  if (ql <= s && qr >= e) {
    return tree[in];
  }

  int mid = (s + e) / 2;
  // cout<<"Okay";
  return max(query(ql, qr, s, mid, 2 * in + 1),
             query(ql, qr, mid + 1, e, 2 * in + 2));
}
void update(ll s, ll e, ll in, ll key, ll up) {
  if (s > e)
    return;
  if (s == e && s == key) {
    tree[in] = up;
    return;
  } else if (s == e)
    return;
  ll mid = (s + e) / 2;

  if (mid >= key)
    update(s, mid, 2 * in + 1, key, up);
  else
    update(mid + 1, e, 2 * in + 2, key, up);
  tree[in] = max(tree[2 * in + 1], tree[2 * in + 2]);
}
int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    ll curr = query(max(0ll, a[i] - k), min(300000ll, a[i] + k), 0, 300000, 0);
    // cout<<curr<<endl;
    ans = max(ans, curr + 1);
    update(0, 300000, 0, a[i], curr + 1);
  }
  cout << ans;
}
Problem p02537 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define NMAX 300005
using namespace std;
typedef long long ll;
ll segmTree[NMAX * 4], l[NMAX * 4], r[NMAX * 4], leaves[NMAX];
void init(size_t nd) {
  if (nd == 0)
    l[0] = 0, r[0] = NMAX - 1;
  else {
    if (nd & 1)
      l[nd] = l[nd / 2], r[nd] = (l[nd / 2] + r[nd / 2]) / 2;
    else
      l[nd] = r[nd - 1] + 1, r[nd] = r[nd / 2 - 1];
  }
  if (l[nd] == r[nd])
    leaves[l[nd]] = nd;
  else {
    init(nd * 2 + 1);
    init(nd * 2 + 2);
  }
}
void upd(ll p, ll x) {
  ll nd = leaves[p];
  if (segmTree[nd] >= x)
    return;
  segmTree[nd] = x;
  do {
    nd = (nd - 1) / 2;
    segmTree[nd] = max(segmTree[nd * 2 + 1], segmTree[nd * 2 + 2]);
  } while (nd > 0);
}
ll query(ll L, ll R) {
  ll ans = 0;
  while (L <= R) {
    ll nd = leaves[L];
    while (nd > 0 && l[(nd - 1) / 2] >= L && r[(nd - 1) / 2] <= R)
      nd = (nd - 1) / 2;
    ans = max(ans, segmTree[nd]);
    L = r[nd] + 1;
  }
  return ans;
}
ll dp[NMAX], v[NMAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  for (ll i = n - 1; i >= 0; i--) {
    dp[i] = query(max(0ll, v[i] - k), v[i] + k) + 1;
    upd(v[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define NMAX 300005
using namespace std;
typedef long long ll;
ll segmTree[NMAX * 4], l[NMAX * 4], r[NMAX * 4], leaves[NMAX];
void init(size_t nd) {
  if (nd == 0)
    l[0] = 0, r[0] = NMAX - 1;
  else {
    if (nd & 1)
      l[nd] = l[nd / 2], r[nd] = (l[nd / 2] + r[nd / 2]) / 2;
    else
      l[nd] = r[nd - 1] + 1, r[nd] = r[nd / 2 - 1];
  }
  if (l[nd] == r[nd])
    leaves[l[nd]] = nd;
  else {
    init(nd * 2 + 1);
    init(nd * 2 + 2);
  }
}
void upd(ll p, ll x) {
  ll nd = leaves[p];
  if (segmTree[nd] >= x)
    return;
  segmTree[nd] = x;
  do {
    nd = (nd - 1) / 2;
    segmTree[nd] = max(segmTree[nd * 2 + 1], segmTree[nd * 2 + 2]);
  } while (nd > 0);
}
ll query(ll L, ll R) {
  ll ans = 0;
  while (L <= R) {
    ll nd = leaves[L];
    while (nd > 0 && l[(nd - 1) / 2] >= L && r[(nd - 1) / 2] <= R)
      nd = (nd - 1) / 2;
    ans = max(ans, segmTree[nd]);
    L = r[nd] + 1;
  }
  return ans;
}
ll dp[NMAX], v[NMAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init(0);
  ll n, k, ans = 0;
  cin >> n >> k;
  for (ll i = 0; i < n; i++)
    cin >> v[i];
  for (ll i = n - 1; i >= 0; i--) {
    dp[i] = query(max(0ll, v[i] - k), min((ll)NMAX - 1, v[i] + k)) + 1;
    upd(v[i], dp[i]);
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

vector<int> sgt;
int sz{1};

void g() {
  while (sz < 3e4)
    sz *= 2;
  sgt.resize(2 * sz);
}

int f(int l, int r) {
  int ans{};
  l += sz, r += sz;
  while (l <= r) {
    if (l & 1)
      ans = max(ans, sgt[l++]);
    if (!(r & 1))
      ans = max(ans, sgt[r--]);
    l /= 2, r /= 2;
  }
  return ans;
}

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

  g();
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int z = f(max(0, a[i] - k), min(300000, a[i] + k));
    //	cout << z << endl;
    a[i] += sz;
    sgt[a[i]] = z + 1;
    a[i] /= 2;
    while (a[i]) {
      sgt[a[i]] = max(sgt[2 * a[i]], sgt[2 * a[i] + 1]);
      a[i] /= 2;
    }
  }
  cout << sgt[1];
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

vector<int> sgt;
int sz{1};

void g() {
  while (sz < 3e5)
    sz *= 2;
  sgt.resize(2 * sz);
}

int f(int l, int r) {
  int ans{};
  l += sz, r += sz;
  while (l <= r) {
    if (l & 1)
      ans = max(ans, sgt[l++]);
    if (!(r & 1))
      ans = max(ans, sgt[r--]);
    l /= 2, r /= 2;
  }
  return ans;
}

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

  g();
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < n; i++) {
    int z = f(max(0, a[i] - k), min(300000, a[i] + k));
    //	cout << z << endl;
    a[i] += sz;
    sgt[a[i]] = z + 1;
    a[i] /= 2;
    while (a[i]) {
      sgt[a[i]] = max(sgt[2 * a[i]], sgt[2 * a[i] + 1]);
      a[i] /= 2;
    }
  }
  cout << sgt[1];
}
Problem p02537 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;

int n, k, a[N], f[N << 2];
void pushup(int o) { f[o] = max(f[o << 1], f[o << 1 | 1]); }
int ask(int l, int r, int o, int ld, int rd) {
  if (l == r)
    return f[o];
  int mid = l + r >> 1, ans = 0;
  if (rd > mid)
    ans = max(ans, ask(mid + 1, r, o << 1 | 1, ld, rd));
  if (ld <= mid)
    ans = max(ans, ask(l, mid, o << 1, ld, rd));
  return ans;
}
void modify(int l, int r, int o, int x, int y) {
  if (l == r) {
    f[o] = y;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    modify(l, mid, o << 1, x, y);
  else
    modify(mid + 1, r, o << 1 | 1, x, y);
  pushup(o);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]++;
  for (int i = 1; i <= n; i++) {
    int x = ask(1, 300001, 1, max(1, a[i] - k), min(300001, a[i] + k)) + 1;
    modify(1, 300001, 1, a[i], x);
  }
  printf("%d\n", f[1]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;

int n, k, a[N], f[N << 2];
void pushup(int o) { f[o] = max(f[o << 1], f[o << 1 | 1]); }
int ask(int l, int r, int o, int ld, int rd) {
  if (l == r)
    return f[o];
  if (ld <= l && rd >= r)
    return f[o];
  int mid = l + r >> 1, ans = 0;
  if (rd > mid)
    ans = max(ans, ask(mid + 1, r, o << 1 | 1, ld, rd));
  if (ld <= mid)
    ans = max(ans, ask(l, mid, o << 1, ld, rd));
  return ans;
}
void modify(int l, int r, int o, int x, int y) {
  if (l == r) {
    f[o] = y;
    return;
  }
  int mid = l + r >> 1;
  if (x <= mid)
    modify(l, mid, o << 1, x, y);
  else
    modify(mid + 1, r, o << 1 | 1, x, y);
  pushup(o);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[i]++;
  for (int i = 1; i <= n; i++) {
    int x = ask(1, 300001, 1, max(1, a[i] - k), min(300001, a[i] + k)) + 1;
    modify(1, 300001, 1, a[i], x);
  }
  printf("%d\n", f[1]);
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ("Ofast")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
ll n, m, a, b, c, k, temp, x, y;
const int MAXN = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
vll read(int n) {
  vll v(n);
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
  return v;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int tree[4 * MAXN];
int qfst(int l, int r) {
  int res = 0;
  for (l += 300001, r += 300001; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(tree[l++], res);
    if (r & 1)
      res = max(tree[--r], res);
  }
  return res;
}
void modify(int p, int value) {
  for (tree[p += 300001] = value; p > 1; p >>= 1)
    tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
void sol(void) {
  cin >> n >> k;
  vll v = read(n);
  for (int i = 0; i < n; i++) {
    int mx = qfst(v[i], v[i] + 1);
    int ending = qfst(max(0, v[i] - k), min(v[i] + k + 1, 300001)) + 1;
    modify(v[i], max(mx, ending));
  }
  cout << qfst(0, 300001);
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  // cin>>test;
  for (int i = 1; i <= test; i++)
    sol();
}


Fixed submission:
// #pragma GCC optimize ("Ofast")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define INF 1e18
#define pll pair<ll, ll>
#define vll vector<ll>
ll n, m, a, b, c, k, temp, x, y;
const int MAXN = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
vll read(int n) {
  vll v(n);
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
  return v;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int tree[6 * MAXN];
int qfst(int l, int r) {
  int res = 0;
  for (l += 300001, r += 300001; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      res = max(tree[l++], res);
    if (r & 1)
      res = max(tree[--r], res);
  }
  return res;
}
void modify(int p, int value) {
  for (tree[p += 300001] = value; p > 1; p >>= 1)
    tree[p >> 1] = max(tree[p], tree[p ^ 1]);
}
void sol(void) {
  cin >> n >> k;
  vll v = read(n);
  for (int i = 0; i < n; i++) {
    int mx = qfst(v[i], v[i] + 1);
    int ending = qfst(max(0, v[i] - k), min(v[i] + k + 1, 300001)) + 1;
    modify(v[i], max(mx, ending));
  }
  cout << qfst(0, 300001);
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  // cin>>test;
  for (int i = 1; i <= test; i++)
    sol();
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll mod2 = 998244353;
ll mod = 1e9 + 7;
vector<int> seg;
vector<int> dp;
void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    seg[v] = val;
  else {
    int mid = (tl + tr) / 2;
    if (pos <= mid)
      update(2 * v, tl, mid, pos, val);
    else {
      update(2 * v + 1, mid + 1, tr, pos, val);
    }
    seg[v] = max(seg[2 * v], seg[2 * v + 1]);
  }
}
int get(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return seg[v];
  int mid = (tl + tr) / 2;
  return max(get(2 * v, tl, mid, l, r), get(2 * v + 1, mid + 1, tr, l, r));
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int p = *max_element(begin(a), end(a));
  seg.resize(p + 6, 0);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int val = get(1, 1, p, max(0, a[i] - k), min(p, a[i] + k));
    update(1, 1, p, a[i], val + 1);
    ans = max(ans, val + 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
ll mod2 = 998244353;
ll mod = 1e9 + 7;
vector<int> seg;
vector<int> dp;
void update(int v, int tl, int tr, int pos, int val) {
  if (tl == tr)
    seg[v] = val;
  else {
    int mid = (tl + tr) / 2;
    if (pos <= mid)
      update(2 * v, tl, mid, pos, val);
    else {
      update(2 * v + 1, mid + 1, tr, pos, val);
    }
    seg[v] = max(seg[2 * v], seg[2 * v + 1]);
  }
}
int get(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return seg[v];
  int mid = (tl + tr) / 2;
  return max(get(2 * v, tl, mid, l, r), get(2 * v + 1, mid + 1, tr, l, r));
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int p = 3e5;
  seg.resize(4 * p, 0);
  int ans = 1;
  for (int i = 1; i <= n; i++) {
    int val = get(1, 1, p, max(0, a[i] - k), min(p, a[i] + k));
    update(1, 1, p, a[i], val + 1);
    ans = max(ans, val + 1);
  }
  cout << ans << endl;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  vector<int> dp(N, 1);

  for (int i(0); i < N; i++) {
    for (int j(i + 1); j < i + 1000; j++) {
      if (abs(A[i] - A[j]) <= K) {
        dp[j] = max(dp[j], dp[i] + 1);
      }
    }
  }
  int ans(0);
  for (int i(0); i < N; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i(0); i < N; i++) {
    cin >> A[i];
  }
  vector<int> dp(N, 1);

  for (int i(0); i < N; i++) {
    for (int j(i + 1); j < min(i + 1000, N); j++) {
      if (abs(A[i] - A[j]) <= K) {
        dp[j] = max(dp[j], dp[i] + 1);
      }
    }
  }
  int ans(0);
  for (int i(0); i < N; i++) {
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
int n, k;
int a[100010];
int DT[100010];
int ans;
int max_a;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct seg {
  int tree[2000010], lim;
  void init(int n) {
    for (lim = 1; lim <= n; lim <<= 1)
      ;
    for (int i = 0; i < 2 * lim; i++) {
      tree[i] = 0;
    }
  }
  void add(int x, int v) {
    x += lim;
    tree[x] = v;
    while (x > 1) {
      x >>= 1;
      tree[x] = max(tree[2 * x], tree[2 * x + 1]);
    }
  }
  int query(int s, int e) {
    s += lim;
    e += lim;
    int ret = 0;
    while (s < e) {
      if (s % 2 == 1)
        ret = max(ret, tree[s++]);
      if (e % 2 == 0)
        ret = max(ret, tree[e--]);
      s >>= 1;
      e >>= 1;
    }
    if (s == e)
      ret = max(ret, tree[s]);
    return ret;
  }
} seg;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    max_a = max_a > a[i] ? max_a : a[i];
  }
  seg.init(max_a);
  for (int i = n - 1; i >= 0; i--) {
    int t = seg.query(max(0, a[i] - k), min(max_a, a[i] + k)) + 1;
    ans = max(ans, t);
    seg.add(a[i], t);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
int n, k;
int a[300010];
int ans;
int max_a;
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
struct seg {
  int tree[2000010], lim;
  void init(int n) {
    for (lim = 1; lim <= n; lim <<= 1)
      ;
    for (int i = 0; i < 2 * lim; i++) {
      tree[i] = 0;
    }
  }
  void add(int x, int v) {
    x += lim;
    tree[x] = v;
    while (x > 1) {
      x >>= 1;
      tree[x] = max(tree[2 * x], tree[2 * x + 1]);
    }
  }
  int query(int s, int e) {
    s += lim;
    e += lim;
    int ret = 0;
    while (s < e) {
      if (s % 2 == 1)
        ret = max(ret, tree[s++]);
      if (e % 2 == 0)
        ret = max(ret, tree[e--]);
      s >>= 1;
      e >>= 1;
    }
    if (s == e)
      ret = max(ret, tree[s]);
    return ret;
  }
} seg;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    max_a = max_a > a[i] ? max_a : a[i];
  }
  seg.init(max_a);
  for (int i = n - 1; i >= 0; i--) {
    int t = seg.query(max(0, a[i] - k), min(max_a, a[i] + k)) + 1;
    ans = max(ans, t);
    seg.add(a[i], t);
  }
  printf("%d\n", ans);
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define ll long long
using namespace std;

inline ll read() {
  ll num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}

int n, k, tree[300010 << 2], a[300010], f[300010];

inline void push_up(int x) { tree[x] = max(tree[x << 1], tree[x << 1 | 1]); }
inline void update(int u, int l, int r, int x, int k) {
  if (l == r) {
    tree[x] = max(tree[x], k);
    return;
  }
  int mid = l + r >> 1;
  if (u <= mid)
    update(u, l, mid, x << 1, k);
  else
    update(u, mid + 1, r, x << 1 | 1, k);
  push_up(x);
  return;
}
inline int query(int ql, int qr, int l, int r, int x) {
  if (ql <= l && qr >= r)
    return tree[x];
  int mid = l + r >> 1, res = 0;
  if (ql <= mid)
    res = max(res, query(ql, qr, l, mid, x << 1));
  if (qr > mid)
    res = max(res, query(ql, qr, mid + 1, r, x << 1 | 1));
  return res;
}

int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    f[i] = query(max(a[i] - k, 0), min(300000, a[i] + k), 1, n, 1) + 1;
    update(a[i], 1, n, 1, f[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(f[i], ans);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
#define ll long long
using namespace std;

inline ll read() {
  ll num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}

int n, k, tree[300010 << 2], a[300010], f[300010];

inline void push_up(int x) { tree[x] = max(tree[x << 1], tree[x << 1 | 1]); }
inline void update(int u, int l, int r, int x, int k) {
  if (l == r) {
    tree[x] = max(tree[x], k);
    return;
  }
  int mid = l + r >> 1;
  if (u <= mid)
    update(u, l, mid, x << 1, k);
  else
    update(u, mid + 1, r, x << 1 | 1, k);
  push_up(x);
  return;
}
inline int query(int ql, int qr, int l, int r, int x) {
  if (ql <= l && qr >= r)
    return tree[x];
  int mid = l + r >> 1, res = 0;
  if (ql <= mid)
    res = max(res, query(ql, qr, l, mid, x << 1));
  if (qr > mid)
    res = max(res, query(ql, qr, mid + 1, r, x << 1 | 1));
  return res;
}

int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    f[i] = query(max(a[i] - k, 0), min(300000, a[i] + k), 1, 300000, 1) + 1;
    update(a[i], 1, 300000, 1, f[i]);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans = max(f[i], ans);
  cout << ans;
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) REP(j, i) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  REP(i, n) cin >> a[i];
  vector<int> dp(n, 0);
  REP(i, n) for (int j = max(0, i - 100); j < i; j++) {
    if (abs(a[i] - a[j]) <= k)
      dp[i] = max(dp[i], dp[j] + 1);
  }
  int res = 0;
  REP(i, n)
  res = max(res, dp[i]);
  cout << res + 1 << '\n';
  return 0;
}
Problem p02537 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll N = 3e5, mod = 1e9 + 7, inf = 1e18;
ll seg[3 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define _IO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);

const ll N = 3e5 + 5, mod = 1e9 + 7, inf = 1e18;
ll seg[4 * N];

void update(ll rt, ll l, ll r, ll id, ll v) {
  if (l > id || r < id)
    return;
  if (l == r) {
    seg[rt] = max(seg[rt], v);
    return;
  }
  ll md = (l + r) / 2;
  update(rt * 2, l, md, id, v);
  update(rt * 2 + 1, md + 1, r, id, v);
  seg[rt] = max(seg[rt * 2], seg[rt * 2 + 1]);
}

ll query(ll rt, ll l, ll r, ll i, ll j) {
  if (l > j || r < i)
    return -inf;
  if (l >= i && r <= j)
    return seg[rt];
  ll md = (l + r) / 2;
  return max(query(rt * 2, l, md, i, j), query(rt * 2 + 1, md + 1, r, i, j));
}

int main() {
  _IO;
  ll n, k;
  cin >> n >> k;
  ll mx = 0;
  while (n--) {
    ll x;
    cin >> x;
    ll p = query(1, 0, N, max(0LL, x - k), min(N, x + k));
    update(1, 0, N, x, p + 1);
    mx = max(mx, p + 1);
  }
  cout << mx << "\n";
}
Problem p02538 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rp1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define rp0(i, n) for ((i) = 0; (i) < (n); (i)++)
using namespace std;
const int mod = 998244353;
int n, q, f[800005], tag[800005], s[800005], lst = 1;
void build(int x, int l, int r) {
  tag[x] = 0;
  if (l == r) {
    f[x] = s[x] = lst;
    lst = 10ll * lst % mod;
    return;
  }
  int mid = (l + r) / 2;
  build(x + x + 1, mid + 1, r);
  build(x + x, l, mid);
  s[x] = f[x] = (f[x + x] + f[x + x + 1]) % mod;
}
void update(int x, int l, int r, int ll, int rr, int c) {
  if (l == r) {
    tag[x] = c;
    s[x] = 1ll * f[x] * c % mod;
    return;
  }
  if (tag[x]) {
    tag[x + x] = tag[x + x + 1] = tag[x];
    s[x + x] = 1ll * f[x + x] * tag[x] % mod;
    s[x + x + 1] = 1ll * f[x + x + 1] * tag[x] % mod;
    tag[x] = 0;
  }
  int mid = (l + r) / 2;
  if (ll <= mid)
    update(x + x, l, mid, ll, rr, c);
  if (rr > mid)
    update(x + x + 1, mid + 1, r, ll, rr, c);
  s[x] = (s[x + x] + s[x + x + 1]) % mod;
}
int main() {
  cin >> n >> q;
  int i;
  build(1, 1, n);
  rp0(i, q) {
    int l, r, d;
    cin >> l >> r >> d;
    update(1, 1, n, l, r, d);
    cout << s[1] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rp1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define rp0(i, n) for ((i) = 0; (i) < (n); (i)++)
using namespace std;
const int mod = 998244353;
int n, q, f[800005], tag[800005], s[800005], lst = 1;
void build(int x, int l, int r) {
  tag[x] = 0;
  if (l == r) {
    f[x] = s[x] = lst;
    lst = 10ll * lst % mod;
    return;
  }
  int mid = (l + r) / 2;
  build(x + x + 1, mid + 1, r);
  build(x + x, l, mid);
  s[x] = f[x] = (f[x + x] + f[x + x + 1]) % mod;
}
void update(int x, int l, int r, int ll, int rr, int c) {
  if (ll <= l && r <= rr) {
    tag[x] = c;
    s[x] = 1ll * f[x] * c % mod;
    return;
  }
  if (tag[x]) {
    tag[x + x] = tag[x + x + 1] = tag[x];
    s[x + x] = 1ll * f[x + x] * tag[x] % mod;
    s[x + x + 1] = 1ll * f[x + x + 1] * tag[x] % mod;
    tag[x] = 0;
  }
  int mid = (l + r) / 2;
  if (ll <= mid)
    update(x + x, l, mid, ll, rr, c);
  if (rr > mid)
    update(x + x + 1, mid + 1, r, ll, rr, c);
  s[x] = (s[x + x] + s[x + x + 1]) % mod;
}
int main() {
  cin >> n >> q;
  int i;
  build(1, 1, n);
  rp0(i, q) {
    int l, r, d;
    cin >> l >> r >> d;
    update(1, 1, n, l, r, d);
    cout << s[1] << endl;
  }
  return 0;
}
Problem p02540 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02540 (C++)

Buggy submission (status: Runtime Error):
#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";
  }
}


Fixed submission:
#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";
  }
}
Problem p02540 (C++)

Buggy submission (status: 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[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;
  }
}

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[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;
  }
}