alexjercan/bugnet huggingface dataset:
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  ll h, w;
  cin >> h >> w;
  vector<pair<ll, ll>> ab(h);
  REP(i, h) {
    ll ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    ab.at(i) = {ai, bi};
  }

  vector<ll> ans(h, -1);

  set<pair<ll, ll>> es;
  multiset<ll> nx;
  REP(i, w) {
    es.emplace(i, i);
    nx.insert(0);
  }

  REP(i, h) {
    const ll ai = ab.at(i).first;
    const ll bi = ab.at(i).second;
    auto it = es.lower_bound(make_pair(ai, -1));
    ll max_s = -1;
    while (it != es.end() && ai <= it->first && it->first <= bi) {
      max_s = it->second;
      const ll d = it->first - it->second;
      nx.erase(nx.find(d));
      it = es.erase(it);
    }
    if (max_s != -1 && bi + 1 < w) {
      es.emplace(bi + 1, max_s);
      nx.insert(bi + 1 - max_s);
    }
    if (nx.size()) {
      ans.at(i) = *nx.begin() + i + 1;
    } else {
      break;
    }
  }

  for (ll ansi : ans) {
    cout << ansi << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T>
using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;

int main() {
  ll h, w;
  cin >> h >> w;
  vector<pair<ll, ll>> ab(h);
  REP(i, h) {
    ll ai, bi;
    cin >> ai >> bi;
    ai--;
    bi--;
    ab.at(i) = {ai, bi};
  }
  vector<ll> ans(h + 1, numeric_limits<ll>::max());
  for (ll s = 0; s < w; ++s) {
    ll cur = s;
    ll step = 0;
    ll cur_h = 0;
    while (cur_h < h) {
      const ll ai = ab.at(cur_h).first;
      const ll bi = ab.at(cur_h).second;
      if (ai <= cur && cur <= bi) {
        step += bi - cur + 1;
        cur = bi + 1;
      }
      if (cur >= w) {
        break;
      }
      step += 1;
      cur_h += 1;
      ans.at(cur_h) = min(ans.at(cur_h), step);
    }
  }

  for (ll i = 1; i <= h; ++i) {
    if (ans.at(i) == numeric_limits<ll>::max()) {
      cout << -1 << endl;
    } else {
      cout << ans.at(i) << endl;
    }
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define Rep(i, l, r) for (int(i) = (l); (i) <= (r); (i)++)
using namespace std;
map<int, ll>::iterator it;
map<int, ll> mp;
multiset<ll> values;
int n, m, l, r;

int main() {
  cin >> n >> m;
  Rep(i, 1, m) {
    mp[i] = 0;
    values.insert(0);
  }
  Rep(i, 1, n) {
    cin >> l >> r;
    if (r != m) {
      it = mp.upper_bound(r + 1);
      if (it != mp.begin()) {
        it--;
        if (it->first != r + 1) {
          ll v = it->second + (r + 1 - it->first);
          mp[r + 1] = v;
          values.insert(v);
        }
      }
    }
    it = mp.lower_bound(l);
    while (it != mp.end() && it->first <= r) {
      values.erase(values.find(it->second));
      it = mp.erase(it);
    }
    cout << ((values.size()) ? (i + (*values.begin())) : (-1)) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define mp make_pair
#define ll long long
#define pb push_back

void solve() {

  ll n;
  cin >> n;

  vector<ll> v(n);

  ll i, j;
  set<ll> s;
  for (i = 0; i < n; i++) {
    cin >> v[i];
    s.insert(v[i]);
  }

  map<int, int> m;

  for (auto it = s.begin(); it != s.end(); it++) {
    ll val = *it;

    if (val % 2 == 0) {
      m[2]++;
      while (val % 2 == 0)
        val = val / j;
    }

    for (j = 3; j * j <= val; j = j + 2) {
      if (val % j == 0) {
        m[j]++;
        while (val % j == 0)
          val = val / j;
      }
    }
  }
  bool f = 1;
  int x = s.size();
  if (x != n)
    f = 0;

  bool t = 1;
  for (auto it = m.begin(); it != m.end(); it++) {
    if (it->second > 1) {
      f = 0;
    }
    if (it->second == x)
      t = 0;
  }

  if (f == 1) {
    cout << "pairwise coprime";
    return;
  }

  if (t == 1) {
    cout << "setwise coprime";
    return;
  }
  cout << "not coprime";

  return;
}

int main() {

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

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

// hail to jwalaji
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli h, w, a, b, f, t, e;
set<pair<lli, lli>> st;
multiset<lli> ms;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> h >> w;
  for (lli i = 0; i < w; i++) {
    st.insert({i, i});
    ms.insert(0);
  }
  for (lli i = 0; i < h; i++) {
    cin >> a >> b;
    auto tl = st.lower_bound({a - 1, 0}), el = st.lower_bound({b, 0});
    e = tl != el;
    for (; tl != el; tl = st.erase(tl)) {
      tie(t, f) = *tl;
      ms.erase(ms.find(t - f));
    }
    if (e && b != w) {
      st.insert({b, f});
      ms.insert(b - f);
    }
    if (ms.empty()) {
      cout << -1 << endl;
    } else {
      cout << *ms.begin() + i + 1 << endl;
    }
  }
}


Error: TLE

Fixed submission:

// hail to jwalaji
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  lli h, w, steps = 0;
  cin >> h >> w;
  vector<lli> st(h + 1, 1e18);
  // bool arr[h][w];
  vector<pair<lli, lli>> store(h);
  for (lli i = 0; i < h; ++i) {
    lli a, b;
    cin >> a >> b;
    a--;
    b--;
    store[i].first = a;
    store[i].second = b;
  }
  lli j = 0;
  lli i;
  for (lli k = 0; k < w; k++) {
    steps = 0;
    j = k;
    for (i = 0; i < h; i++) {
      if (j >= store[i].first && j <= store[i].second) {
        steps += (store[i].second + 1 - j);
        j = store[i].second + 1;
      }
      steps++;
      if (j >= w) {
        break;
      } else {
        st[i] = min(st[i], steps);
      }
    }
  }
  for (lli i = 0; i < h; i++) {
    if (st[i] == 1e18) {
      cout << -1 << endl;
    } else {
      cout << st[i] << endl;
    }
  }
  cout << endl;
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int H, W;
  cin >> H >> W;
  map<int, int> s;
  multiset<int> values;

  for (int i = 0; i < W; ++i) {
    s[i] = i;
  }
  for (int i = 0; i < W; ++i) {
    values.insert(0);
  }
  for (int i = 0; i < H; ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    auto it = s.lower_bound(l);
    int mr = -1;
    while (it != s.end() && it->first <= r) {
      mr = max(mr, it->second);
      int x = it->first - it->second;
      values.erase(values.find(x));
      s.erase(it++);
    }
    if ((-1 != mr) && (r < W)) {
      values.insert(r - mr);
      s[r] = mr;
    }
    if (0 == values.size()) {
      cout << -1 << endl;
    } else {
      cout << *values.begin() + (i + 1) << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  set<pair<int, int>> S;
  for (int i = 0; i < H; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    for (int j = a; j <= b; ++j) {
      pair<int, int> p = make_pair(i, j);
      S.insert(p);
    }
  }
  vector<int> dp(W, 0);
  int left = 0;
  int r = W - 1;
  for (int i = 0; i < H; ++i) {
    int ans = 1e9;
    for (int j = left; j < W; ++j) {
      bool b = true;
      pair<int, int> p = make_pair(i, j);
      if (S.count(p)) {
        b = false;
      }
      if (0 == j) {
        if (b) {
          dp[j]++;
        } else {
          dp[j] = -1;
          left = j + 1;
        }
      } else {
        if (b && (-1 != dp[j - 1])) {
          dp[j] = min(dp[j] + 1, dp[j - 1] + 1);
        } else if ((!b) && (-1 != dp[j - 1])) {
          dp[j] = dp[j - 1] + 1;
        } else if ((b) && (-1 == dp[j - 1])) {
          dp[j]++;
        } else {
          dp[j] = -1;
          left = j + 1;
        }
      }
      // cout << dp[j] << endl;
      if (dp[j] != -1) {
        ans = min(ans, dp[j]);
      }
    }
    if (ans != 1e9) {
      cout << ans << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
    A[i]--, B[i]--;
  }
  map<int, int> mp;
  multiset<int> res;
  for (int i = 0; i < W; i++) {
    mp[i] = i;
    res.insert(0);
  }
  for (int i = 0; i < H; i++) {
    auto it = mp.lower_bound(A[i]);
    int n = -1;
    while (it != mp.end() && it->first < B[i] + 2) {
      int x = it->first - it->second;
      n = max(n, it->second);
      res.erase(res.find(x));
      mp.erase(it++);
    }
    if (n != -1 && B[i] + 1 < W) {
      res.insert(B[i] + 1 - n);
      mp[B[i] + 1] = n;
    }
    if (!res.empty()) {
      cout << *res.begin() + i + 1 << '\\n';
    } else {
      cout << -1 << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H, W;
  cin >> H >> W;
  vector<int> A(H), B(H);
  for (int i = 0; i < H; i++) {
    cin >> A[i] >> B[i];
    A[i]--, B[i]--;
  }
  vector<int> res(H, 1 << 28);
  res[0] = 0;
  queue<pair<int, pair<int, int>>> que;
  if (A[0] != 0)
    que.push(make_pair(0, make_pair(0, A[0] - 1)));
  if (B[0] != W - 1)
    que.push(make_pair(0, make_pair(B[0] + 1, W - 1)));
  for (int i = 0; i < H; i++) {
    while (!que.empty() && que.front().first == i) {
      auto q = que.front();
      que.pop();
      int h = q.first, left = q.second.first, right = q.second.second;
      if (B[i + 1] < left) {
        res[i + 1] = min(res[i + 1], 0);
        que.push(make_pair(i + 1, make_pair(left, right)));
      } else if (A[i + 1] > right) {
        res[i + 1] = min(res[i + 1], 0);
        que.push(make_pair(i + 1, make_pair(left, right)));
      } else if (left < A[i + 1] && B[i + 1] < right) {
        que.push(make_pair(i + 1, make_pair(left, A[i + 1] - 1)));
        que.push(make_pair(i + 1, make_pair(B[i + 1] + 1, right)));
        res[i + 1] = min(res[i + 1], 0);
      } else if (left < A[i + 1] && right <= B[i + 1]) {
        que.push(make_pair(i + 1, make_pair(left, A[i + 1] - 1)));
        res[i + 1] = min(res[i + 1], 0);
      } else if (A[i + 1] <= left && B[i + 1] < right) {
        que.push(make_pair(i + 1, make_pair(B[i + 1] + 1, right)));
        res[i + 1] = min(res[i + 1], 0);
      } else {
        if (B[i + 1] != W - 1) {
          que.push(make_pair(i + 1, make_pair(B[i + 1] + 1, B[i + 1] + 1)));
          res[i + 1] = min(res[i + 1], abs(right - B[i + 1]) + 1);
        }
      }
    }
  }
  int cur = 0;
  for (int i = 0; i < H; i++) {
    if (res[i] == (1 << 28)) {
      cout << -1 << '\\n';
      continue;
    }
    cur += res[i] + 1;
    cout << cur << '\\n';
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, m, t[N << 2], t1[N << 2], t2[N << 2];
void update(int l, int r, int k) {
  t1[k] = t[k] + r - l, t2[k] = t[k];
  if (l != r) {
    int m = l + r >> 1;
    t[k << 1] = t[k];
    t[k << 1 | 1] = t[k] + m + 1 - l;
  }
  t[k] = 0;
}
int query(int l, int r, int k, int x) {
  if (t[k])
    update(l, r, k);
  if (l > x)
    return m + 1;
  if (r <= x)
    return t1[k];
  int m = l + r >> 1;
  int a = query(l, m, k << 1, x), b = query(m + 1, r, k << 1 | 1, x);
  if (x <= r)
    a += x >= m ? x - m : 0;
  else
    a += r - m;
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
  return min(a, b);
}
void update(int l, int r, int k, int x, int y, int d) {
  if (t[k])
    update(l, r, k);
  if (r < x || l > y)
    return;
  if (l >= x && r <= y) {
    t[k] = d + l - x;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  update(l, m, k << 1, x, y, d);
  update(m + 1, r, k << 1 | 1, x, y, d);
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, d; i <= n; ++i) {
    scanf("%d%d", &a, &b);
    d = min(m + 1, query(1, m, 1, a - 1) + 1);
    update(1, m, 1, a, b, d);
    printf("%d\\n", t2[1] >= m ? -1 : t2[1] + i);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int n, m, t[N << 2], t1[N << 2], t2[N << 2];
void update(int l, int r, int k) {
  t1[k] = t[k] + r - l, t2[k] = t[k];
  if (l != r) {
    int m = l + r >> 1;
    t[k << 1] = t[k];
    t[k << 1 | 1] = t[k] + m + 1 - l;
  }
  t[k] = 0;
}
int query(int l, int r, int k, int x) {
  if (t[k])
    update(l, r, k);
  if (l > x)
    return m + 1;
  if (r <= x)
    return t1[k];
  int m = l + r >> 1;
  int a = query(1, m, k << 1, x), b = query(m + 1, r, k << 1 | 1, x);
  if (x <= r)
    a += x > m ? x - m : 0;
  else
    a += r - m;
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
  return min(a, b);
}
void update(int l, int r, int k, int x, int y, int d) {
  if (t[k])
    update(l, r, k);
  if (r < x || l > y)
    return;
  if (l >= x && r <= y) {
    t[k] = d + l - x;
    update(l, r, k);
    return;
  }
  int m = l + r >> 1;
  update(l, m, k << 1, x, y, d);
  update(m + 1, r, k << 1 | 1, x, y, d);
  t2[k] = min(t2[k << 1], t2[k << 1 | 1]);
  t1[k] = min(t1[k << 1] + r - m, t1[k << 1 | 1]);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, a, b, d; i <= n; ++i) {
    scanf("%d%d", &a, &b);
    d = min(m + 1, query(1, m, 1, a - 1) + 1);
    update(1, m, 1, a, b, d);
    printf("%d\\n", t2[1] >= m ? -1 : t2[i] + i);
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int h, w;
  cin >> h >> w;

  set<pair<int, int>> s;
  multiset<int> ms;

  int l[h], r[h];

  for (int i = 0; i < h; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = 1; i <= w; i++) {
    s.insert({i, i});
    ms.insert(0);
  }

  for (int i = 0; i < h; i++) {
    if (s.size() == 0) {
      cout << -1 << endl;
      continue;
    }

    auto itr = s.lower_bound({l[i], 0});
    vector<pair<int, int>> del;

    if (r[i] == w) {
      while (itr != s.end() && itr->first <= r[i]) {
        del.push_back(*itr);
        ++itr;
      }
    } else {
      int max_start = 0;

      while (itr != s.end() && itr->first <= r[i]) {
        del.push_back(*itr);

        max_start = max(max_start, itr->second);

        ++itr;
      }

      if (max_start != 0) {
        s.insert({r[i] + 1, max_start});
        ms.insert(r[i] + 1 - max_start);
      }
    }

    for (auto ele : del) {
      s.erase(ele);
      ms.erase(ms.find(ele.first - ele.second));
    }

    if (ms.size()) {
      cout << *ms.begin() + i + 1 << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int h, w;
  cin >> h >> w;

  set<pair<int, int>> s;
  multiset<int> ms;

  int l[h], r[h];

  for (int i = 0; i < h; i++) {
    cin >> l[i] >> r[i];
  }

  for (int i = 1; i <= w; i++) {
    s.insert({i, i});
    ms.insert(0);
  }

  for (int i = 0; i < h; i++) {
    if (s.size() == 0) {
      cout << -1 << endl;
      continue;
    }
    auto itr = s.lower_bound({l[i], 0});
    vector<pair<int, int>> del;

    if (r[i] == w) {
      while (itr != s.end() && itr->second <= r[i]) {
        del.push_back(*itr);
        ++itr;
      }
    } else {
      int max_start = 0;

      while (itr != s.end() && itr->second <= r[i]) {
        del.push_back(*itr);

        max_start = max(max_start, itr->second);

        ++itr;
      }

      s.insert({r[i] + 1, max_start});
      ms.insert(r[i] + 1 - max_start);
    }

    for (auto ele : del) {
      s.erase(ele);
      ms.erase(ms.find(ele.first - ele.second));
    }
    assert(s.size() == ms.size());
    if (ms.size()) {
      cout << *ms.begin() + i + 1 << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
using P = pair<int, int>;
signed main() {
  int H, W;
  cin >> H >> W;
  set<P> s;
  multiset<int> ans;
  for (int i = 0; i < W; i++) {
    s.insert({i, i});
    ans.insert(0);
  }
  int n = 0;
  for (int i = 0; i < H; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    int mx = -1;
    auto it = s.lower_bound({max(a, n), 0});
    for (; it != s.end() && (*it).first <= b; it = s.erase(it)) {
      mx = max(mx, (*it).second);
      ans.erase(ans.find((*it).first - (*it).second));
    }
    if (b != W) {
      if (mx != -1) {
        s.insert({b, mx});
        ans.insert(b - mx);
      }
    }
  home:;
    printf("%d\\n", ans.size() == 0 ? -1 : *ans.begin() + i + 1);
    if (n >= a)
      n = max(n, b);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int mp[10000000];
signed main() {
  int H, W;
  cin >> H >> W;
  multiset<int> st;
  for (int i = 0; i < W; i++) {
    st.insert(0);
    mp[i] = i;
  }
  int mx = 0;
  for (int j = 0; j < H; j++) {
    int a, b;
    cin >> a >> b;
    a--;
    int mx2 = -1;
    for (int i = max(mx, a); i <= b && i < W; i++) {
      if (mp[i] == -1)
        continue;
      mx2 = max(mx2, mp[i]);
      if (i != b)
        st.erase(st.find(i - mp[i]));
      if (a > mx) {
        mp[i] = mp[a - 1];
        st.insert(i - mp[a - 1]);
      } else
        mp[i] = -1;
    }
    if (mx >= a)
      mx = max(mx, b);
    mp[b] = mx2;
    if (mx >= W)
      puts("-1");
    else
      printf("%d\\n", *st.begin() + j + 1);
  }
}
Problem p02575 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int H, W;
  cin >> H >> W;
  map<int, int64_t> mp;
  multiset<int64_t> values;

  auto add = [&](int i, int64_t v) {
    if (mp.count(i))
      values.erase(values.find(mp[i]));
    mp[i] = v;
    values.insert(v);
  };
  auto del = [&](int i) {
    values.erase(values.find(mp[i]));
    return mp.erase(mp.find(i));
  };

  auto solve = [&](int l, int r) -> void {
    if (r != W) {
      auto it = mp.upper_bound(r + 1);
      if (it != mp.begin()) {
        it--;
        add(r + 1, it->second + (r + 1 - it->first));
      }
    }

    auto it1 = mp.lower_bound(l);
    while (it1 != mp.end() && it1->first <= r) {
      it1 = del(it1->first);
    }
    if (it1 == mp.begin())
      return;
    auto it2 = it1;
    it1--;
    while (it2 != mp.end()) {
      int d = it2->first - it1->first;
      if (it1->second + d <= it2->second) {
        it2 = del(it2->first);
      } else {
        break;
      }
    }
  };

  for (int i = 1; i <= W; i++)
    add(i, 0);
  int64_t fix = 0;
  while (H--) {
    int a, b;
    cin >> a >> b;
    solve(a, b);
    fix++;
    if (values.size()) {
      cout << fix + *values.begin() << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int H, W;
  cin >> H >> W;
  map<int, int64_t> mp;
  multiset<int64_t> values;

  auto add = [&](int i, int64_t v) {
    if (mp.count(i))
      values.erase(mp[i]);
    mp[i] = v;
    values.insert(v);
  };
  auto del = [&](int i) {
    values.erase(values.find(mp[i]));
    return mp.erase(mp.find(i));
  };

  const int64_t INF = 1e18;
  auto calculate = [&](int x) -> int64_t {
    auto it = mp.upper_bound(x);
    if (it == mp.begin())
      return INF;
    auto [x2, v2] = *prev(it);
    return v2 + (x - x2);
  };

  auto solve = [&](int l, int r) -> void {
    if (r != W)
      add(r + 1, calculate(r + 1));

    auto it1 = mp.lower_bound(l);
    while (it1 != mp.end() && it1->first <= r) {
      it1 = del(it1->first);
    }
    if (it1 == mp.begin())
      return;
    auto it2 = it1;
    it1--;
    while (it2 != mp.end()) {
      int d = it2->first - it1->first;
      if (it1->second + d <= it2->second) {
        it2 = del(it2->first);
      } else {
        break;
      }
    }
  };

  for (int i = 1; i <= W; i++)
    add(i, 0);
  int64_t fix = 0;
  while (H--) {
    int a, b;
    cin >> a >> b;
    solve(a, b);
    fix++;
    if (values.size()) {
      cout << fix + *values.begin() << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18;
int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int H, W;
  cin >> H >> W;
  map<int, int> Mp;
  multiset<int> st;
  for (int i = 0; i < W; i++) {
    Mp[i] = i;
    st.insert(0);
  }
  for (int i = 0; i < H; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    auto it = Mp.lower_bound(A);
    int mx = -1;
    while (it != Mp.end() && it->first <= B + 1) {
      st.erase(st.find(it->first - it->second));
      mx = max(mx, it->second);
      Mp.erase(it++);
    }
    if (B + 1 != W && mx != -1) {
      Mp[B + 1] = mx;
      st.insert(B + 1 - Mp[B + 1]);
    }
    if (st.size() == 0) {
      cout << -1 << endl;
    } else {
      cout << *st.begin() + i + 1 << endl;
    }
  }
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e18;
int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int H, W;
  cin >> H >> W;
  map<int, int> Mp;
  multiset<int> st;
  for (int i = 0; i < W; i++) {
    Mp[i] = i;
    st.insert(0);
  }
  for (int i = 0; i < H; i++) {
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    auto it = Mp.lower_bound(A);
    while (it != Mp.end() && it->first <= B + 1) {
      st.erase(st.find(it->first - it->second));
      Mp[B + 1] = max(Mp[B + 1], it->second);
      Mp.erase(it++);
    }
    if (B + 1 == W) {
      st.insert(INF);
    } else {
      st.insert(B + 1 - Mp[B + 1]);
    }
    if (*st.begin() == INF) {
      cout << -1 << endl;
    } else {
      cout << *st.begin() + i + 1 << endl;
    }
  }
}