Problem p03790 (C++)

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

using namespace std;
typedef long long LL;

const int MX = 100005, MXT = 1 << 20 | 1;

int n, m, a[MX], b[MX], s[MX], L[MX], R[MX], x[MXT], l[MXT], r[MXT];
int p, q;
#define ll(i) (l[i] ? l[i] : (l[i] = ++q))
#define rr(i) (r[i] ? r[i] : (r[i] = ++q))
LL f[MX], ans;

inline void upd(int i, int li, int ri, int _l, int _r, int p) {
  if (_l <= li && ri <= _r)
    x[i] = p;
  else {
    if (x[i])
      x[ll(i)] = x[rr(i)] = x[i], x[i] = 0;
    int mid = (li + ri) >> 1;
    if (_l <= mid)
      upd(ll(i), li, mid, _l, _r, p);
    if (_r > mid)
      upd(rr(i), mid + 1, ri, _l, _r, p);
  }
}
inline int que(int i, int li, int ri, int p) {
  if (x[i])
    return x[i];
  if (!i || li == ri)
    return 0;
  int mid = (li + ri) >> 1;
  return p <= mid ? que(l[i], li, mid, p) : que(r[i], mid + 1, ri, p);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if (b[i] == 1 && a[i] * 2 > m) {
      cout << -1 << endl;
      return 0;
    }
    s[i] = (s[i - 1] - 2 * a[i] % m + m) % m;
  }
  for (int i = 1; i <= n; i++)
    if (b[i] == 2)
      L[i] = 1, R[i] = m;
    else
      L[i] = s[i - 1] + 1, R[i] = s[i] + 1;
  for (p = 1; p < m; p <<= 1)
    ;
  q = 1;
  for (int i = n, j; i >= 2; i--) {
    j = que(1, 1, p, L[i]);
    if (j)
      f[i] = f[j] + (L[j] - L[i] + m) % m;
    if (L[i] <= R[i]) {
      if (L[i] > 1)
        upd(1, 1, p, 1, L[i] - 1, i);
      if (R[i] < m)
        upd(1, 1, p, R[i] + 1, m, i);
    } else
      upd(1, 1, p, R[i] + 1, L[i] - 1, i);
  }
  ans = 1e18;
  for (int i = 2, j, k; i <= 2 * n + 1; i++) {
    if (i & 1)
      j = L[i >> 1];
    else
      j = R[i >> 1];
    if (L[1] <= R[1] && L[1] <= j && j <= R[1] ||
        L[1] > R[1] && (L[1] <= j || j <= R[1])) {
      k = que(1, 1, p, j);
      if (!k)
        ans = 0;
      else
        ans = min(ans, f[k] + (L[k] - j + m) % m);
    }
  }
  for (int i = 1; i <= n; i++)
    ans += a[i] << 1;
  cout << ans << endl;
  return 0;
}

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

using namespace std;
typedef long long LL;

const int MX = 100005, MXT = 1 << 23 | 1;

int n, m, a[MX], b[MX], s[MX], L[MX], R[MX], x[MXT], l[MXT], r[MXT];
int p, q;
#define ll(i) (l[i] ? l[i] : (l[i] = ++q))
#define rr(i) (r[i] ? r[i] : (r[i] = ++q))
LL f[MX], ans;

inline void upd(int i, int li, int ri, int _l, int _r, int p) {
  if (_l <= li && ri <= _r)
    x[i] = p;
  else {
    if (x[i])
      x[ll(i)] = x[rr(i)] = x[i], x[i] = 0;
    int mid = (li + ri) >> 1;
    if (_l <= mid)
      upd(ll(i), li, mid, _l, _r, p);
    if (_r > mid)
      upd(rr(i), mid + 1, ri, _l, _r, p);
  }
}
inline int que(int i, int li, int ri, int p) {
  if (x[i])
    return x[i];
  if (!i || li == ri)
    return 0;
  int mid = (li + ri) >> 1;
  return p <= mid ? que(l[i], li, mid, p) : que(r[i], mid + 1, ri, p);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if (b[i] == 1 && a[i] * 2 > m) {
      cout << -1 << endl;
      return 0;
    }
    s[i] = (s[i - 1] - 2 * a[i] % m + m) % m;
  }
  for (int i = 1; i <= n; i++)
    if (b[i] == 2)
      L[i] = 1, R[i] = m;
    else
      L[i] = s[i - 1] + 1, R[i] = s[i] + 1;
  for (p = 1; p < m; p <<= 1)
    ;
  q = 1;
  for (int i = n, j; i >= 2; i--) {
    j = que(1, 1, p, L[i]);
    if (j)
      f[i] = f[j] + (L[j] - L[i] + m) % m;
    if (L[i] <= R[i]) {
      if (L[i] > 1)
        upd(1, 1, p, 1, L[i] - 1, i);
      if (R[i] < m)
        upd(1, 1, p, R[i] + 1, m, i);
    } else
      upd(1, 1, p, R[i] + 1, L[i] - 1, i);
  }
  ans = 1e18;
  for (int i = 2, j, k; i <= 2 * n + 1; i++) {
    if (i & 1)
      j = L[i >> 1];
    else
      j = R[i >> 1];
    if (L[1] <= R[1] && L[1] <= j && j <= R[1] ||
        L[1] > R[1] && (L[1] <= j || j <= R[1])) {
      k = que(1, 1, p, j);
      if (!k)
        ans = 0;
      else
        ans = min(ans, f[k] + (L[k] - j + m) % m);
    }
  }
  for (int i = 1; i <= n; i++)
    ans += a[i] << 1;
  cout << ans << endl;
  return 0;
}