Problem p02575 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int maxn = 2e5 + 10;
const int inf = 1e9 + 10;

class segmentTree {
public:
  int tree[maxn * 8], tag[maxn * 8];
  void init() {
    for (int i = 0; i < maxn * 8; i++)
      tree[i] = inf, tag[i] = 0;
  }
  void pushdown(int x) {
    if (tag[x] == 0)
      return;
    tree[x << 1] = tree[x << 1 | 1] = tag[x];
    tag[x << 1] = tag[x << 1 | 1] = tag[x];
    tag[x] = 0;
  }
  void update(int x, int l, int r, int pos, int val) {
    if (l == r) {
      tree[x] = val;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      update(x << 1, l, mid, pos, val);
    else
      update(x << 1 | 1, mid + 1, r, pos, val);
    tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
  }
  void update(int x, int l, int r, int cl, int cr, int val) {
    if (cr < l || r < cl)
      return;
    if (cl <= l && r <= cr) {
      //	cout<<l<<" "<<r<<" "<<cl<<" "<<cr<<endl;
      pushdown(x);
      tree[x] = tag[x] = val;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    update(x << 1, l, mid, cl, cr, val);
    update(x << 1 | 1, mid + 1, r, cl, cr, val);
    tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
  }
  int getval(int x, int l, int r, int cl, int cr) {
    if (cr < l || r < cl)
      return inf;
    if (cl <= l && r <= cr)
      return tree[x];
    pushdown(x);
    int mid = (l + r) >> 1;
    int val1 = getval(x << 1, l, mid, cl, cr);
    int val2 = getval(x << 1 | 1, mid + 1, r, cl, cr);
    return min(val1, val2);
  }
  int getval(int x, int l, int r, int pos) {
    if (l == r)
      return tree[x];
    pushdown(x);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      return getval(x << 1, l, mid, pos);
    else
      return getval(x << 1 | 1, mid + 1, r, pos);
  }
} seg1, seg2;

int ans[maxn], n, m;
pair<int, int> p[maxn];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &p[i].fst, &p[i].snd), p[i].fst--, p[i].snd--;
  seg1.init();
  seg2.init();
  for (int i = 0; i < m; i++) {
    seg1.update(1, 0, m - 1, i, 0);
    seg2.update(1, 0, m - 1, i, -i);
  }
  for (int i = 0; i < n; i++) {
    int val =
        seg2.getval(1, 0, m - 1, 0, min(p[i].snd + 1, m - 1)) + p[i].snd + 1;
    seg1.update(1, 0, m - 1, p[i].fst, p[i].snd, inf);
    seg2.update(1, 0, m - 1, p[i].fst, p[i].snd, inf);
    if (p[i].snd + 1 < m)
      seg1.update(1, 0, m - 1, p[i].snd + 1, val);
    if (p[i].snd + 1 < m)
      seg2.update(1, 0, m - 1, p[i].snd + 1, val - p[i].snd - 1);
    ans[i] = seg1.getval(1, 0, m - 1, 0, m - 1) + i + 1;
    //	cout<<seg1.getval(1,0,m-1,0,m-1)<<endl;
    if (ans[i] >= n + m + 100)
      ans[i] = -1;
    //	for(int j=0;j<m;j++) cout<<seg1.getval(1,0,m-1,j)<<" "; cout<<endl;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

/*
5 6
1 6
2 4
3 4
3 4
3 4
*/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;

template <class T> inline void checkmin(T &a, const T &b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, const T &b) {
  if (b > a)
    a = b;
}

const int maxn = 2e5 + 10;
const int inf = 1e9 + 10;

class segmentTree {
public:
  int tree[maxn * 8], tag[maxn];
  void init() {
    for (int i = 0; i < maxn * 8; i++)
      tree[i] = inf;
  }
  void pushdown(int x) {
    if (tag[x] == 0)
      return;
    tree[x << 1] = tree[x << 1 | 1] = tag[x];
    tag[x] = 0;
  }
  void update(int x, int l, int r, int pos, int val) {
    if (l == r) {
      tree[x] = val;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      update(x << 1, l, mid, pos, val);
    else
      update(x << 1 | 1, mid + 1, r, pos, val);
    tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
  }
  void update(int x, int l, int r, int cl, int cr, int val) {
    if (r < cl || cr < l)
      return;
    if (cl <= l && r <= cr) {
      pushdown(x);
      tree[x] = tag[x] = val;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    update(x << 1, l, mid, cl, cr, val);
    update(x << 1 | 1, mid + 1, r, cl, cr, val);
    tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
  }
  int getval(int x, int l, int r, int cl, int cr) {
    if (r < cl || cr < l)
      return inf;
    if (cl <= l && r <= cr)
      return tree[x];
    pushdown(x);
    int mid = (l + r) >> 1;
    int val1 = getval(x << 1, l, mid, cl, cr);
    int val2 = getval(x << 1 | 1, mid + 1, r, cl, cr);
    return min(val1, val2);
  }
  int getval(int x, int l, int r, int pos) {
    if (l == r)
      return tree[x];
    pushdown(x);
    int mid = (l + r) >> 1;
    if (pos <= mid)
      return getval(x << 1, l, mid, pos);
    else
      return getval(x << 1 | 1, mid + 1, r, pos);
    tree[x] = min(tree[x << 1], tree[x << 1 | 1]);
  }
} seg1, seg2;

int ans[maxn], n, m;
pair<int, int> p[maxn];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &p[i].fst, &p[i].snd), p[i].fst--, p[i].snd--;
  seg1.init();
  seg2.init();
  for (int i = 0; i < n; i++) {
    seg1.update(1, 0, m - 1, i, 0);
    seg2.update(1, 0, m - 1, i, m - 1 - i);
  }
  for (int i = 0; i < n; i++) {
    int val = seg2.getval(1, 0, m - 1, 0, p[i].snd) - m + 1 + p[i].snd + 1;
    seg1.update(1, 0, m - 1, p[i].fst, p[i].snd, inf);
    seg2.update(1, 0, m - 1, p[i].fst, p[i].snd, inf);
    if (p[i].snd + 1 < m)
      seg1.update(1, 0, m - 1, p[i].snd + 1, val);
    if (p[i].snd + 1 < m)
      seg2.update(1, 0, m - 1, p[i].snd + 1, val + m - 1 - i);
    ans[i] = seg1.getval(1, 0, m - 1, 0, m - 1) + i + 1;
    if (ans[i] >= inf)
      ans[i] = -1;
  }
  for (int i = 0; i < n; i++)
    printf("%d\\n", ans[i]);
  return 0;
}