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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

const int sz = 256, MX = 60000;
int q, t[MX], a[MX], b[MX], y[MX];
int vs[MX * 2], init[MX * 2], m;
int z[MX * 2], mx[1000];
bool eq[1000], initialized[1000];

inline void takemax(int i, int j, int x) {
  bool flag = eq[i / sz];
  while (i < j && i % sz) {
    if (eq[i / sz])
      rep(k, sz) z[i / sz * sz + k] = mx[i / sz];
    eq[i / sz] = 0;
    z[i] = max(flag ? mx[i / sz] : z[i], x);
    i++;
  }
  flag = eq[(j - 1) / sz];
  while (i < j && j % sz) {
    if (eq[--j / sz])
      rep(k, sz) z[j / sz * sz + k] = mx[j / sz];
    eq[j / sz] = 0;
    z[j] = max(flag ? mx[j / sz] : z[j], x);
  }
  i /= sz;
  j /= sz;
  if (i && !eq[i - 1]) {
    mx[i - 1] = inf;
    rep(k, sz) mx[i - 1] = min(mx[i - 1], z[(i - 1) * sz + k]);
  }
  if (!eq[j]) {
    mx[j] = inf;
    rep(k, sz) mx[j] = min(mx[j], z[j * sz + k]);
  }
  while (i < j) {
    if (eq[i] || mx[i] >= x)
      mx[i] = max(mx[i], x);
    else {
      mx[i] = inf;
      rep(k, sz) z[i * sz + k] = max(z[i * sz + k], x),
                            mx[i] = min(mx[i], z[i * sz + k]);
    }
    i++;
  }
}
inline void set(int i, int j, int x, bool ini = 0) {
  bool flag = eq[i / sz];
  while (i < j && i % sz) {
    if (ini && !initialized[i / sz] && init[i] == -inf)
      init[i] = flag ? mx[i / sz] : z[i];
    if (eq[i / sz])
      rep(k, sz) z[i / sz * sz + k] = mx[i / sz];
    eq[i / sz] = 0;
    z[i++] = x;
  }
  flag = eq[(j - 1) / sz];
  while (i < j && j % sz) {
    j--;
    if (ini && !initialized[j / sz] && init[j] == -inf)
      init[j] = flag ? mx[j / sz] : z[j];
    if (eq[j / sz])
      rep(k, sz) z[j / sz * sz + k] = mx[j / sz];
    eq[j / sz] = 0;
    z[j] = x;
  }
  i /= sz;
  j /= sz;
  if (i && !eq[i - 1]) {
    mx[i - 1] = inf;
    rep(k, sz) mx[i - 1] = min(mx[i - 1], z[(i - 1) * sz + k]);
  }
  if (!eq[j]) {
    mx[j] = inf;
    rep(k, sz) mx[j] = min(mx[j], z[j * sz + k]);
  }
  while (i < j) {
    if (ini && !initialized[i]) {
      rep(k, sz) if (init[i * sz + k] == -inf) init[i * sz + k] =
          eq[i] ? mx[i] : z[i * sz + k];
      initialized[i] = 1;
    }
    eq[i] = 1;
    mx[i++] = x;
  }
}
inline int get(int i, int j) {
  int res = inf;
  while (i < j && i % sz)
    res = min(res, eq[i / sz] ? mx[i / sz] : z[i]), i++;
  while (i < j && j % sz)
    res = min(res, eq[--j / sz] ? mx[j / sz] : z[j]);
  i /= sz;
  j /= sz;
  while (i < j)
    res = min(res, mx[i++]);
  return res;
}

bool func(bool ck) {
  rep(i, q) {
    if (t[i] == 0) {
      if (!ck)
        takemax(a[i], b[i], y[i]);
      else if (get(a[i], b[i]) != y[i])
        return 0;
    } else
      set(a[i], b[i], y[i], !ck);
  }
  if (!ck)
    rep(i, m) if (init[i] == -inf) init[i] = eq[i / sz] ? mx[i / sz] : z[i];
  return 1;
}

int main() {
  scanf("%d", &q);
  rep(i, q) {
    scanf("%d%d%d%d", t + i, a + i, b + i, y + i);
    vs[2 * i] = a[i];
    vs[2 * i + 1] = ++b[i];
  }
  sort(vs, vs + 2 * q);
  m = unique(vs, vs + 2 * q) - vs;
  rep(i, q) {
    a[i] = lower_bound(vs, vs + m, a[i]) - vs;
    b[i] = lower_bound(vs, vs + m, b[i]) - vs;
  }

  rep(i, m) z[i] = init[i] = -inf;
  rep(i, (m + sz - 1) / sz) mx[i] = -inf, eq[i] = 1;
  func(0);
  rep(i, m) z[i] = init[i];
  rep(i, (m + sz - 1) / sz) {
    eq[i] = 1;
    mx[i] = inf;
    rep(j, sz) if (i * sz + j < m) {
      mx[i] = min(mx[i], z[i * sz + j]);
      if (z[i * sz] != z[i * sz + j])
        eq[i] = 0;
    }
  }
  puts(func(1) ? "YES" : "NO");

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define fr(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)2e9;
const double EPS = 1e-9, INF = 1e12;

const int sz = 256, MX = 60000;
int q, t[MX], a[MX], b[MX], y[MX];
int vs[MX * 2], init[MX * 2], m;
int z[MX * 2], mx[1000];
bool eq[1000], initialized[1000];

inline void takemax(int i, int j, int x) {
  bool flag = eq[i / sz];
  while (i < j && i % sz) {
    if (eq[i / sz])
      rep(k, sz) z[i / sz * sz + k] = mx[i / sz];
    eq[i / sz] = 0;
    z[i] = max(flag ? mx[i / sz] : z[i], x);
    mx[i / sz] = min(mx[i / sz], x);
    i++;
  }
  flag = eq[(j - 1) / sz];
  while (i < j && j % sz) {
    if (eq[--j / sz])
      rep(k, sz) z[j / sz * sz + k] = mx[j / sz];
    eq[j / sz] = 0;
    z[j] = max(flag ? mx[j / sz] : z[j], x);
    mx[j / sz] = min(mx[j / sz], x);
  }
  i /= sz;
  j /= sz;
  while (i < j) {
    if (eq[i] || mx[i] >= x)
      mx[i] = max(mx[i], x);
    else
      rep(k, sz) z[i * sz + k] = max(z[i * sz + k], x);
    i++;
  }
}
inline void set(int i, int j, int x, bool ini = 0) {
  bool flag = eq[i / sz];
  while (i < j && i % sz) {
    if (ini && !initialized[i / sz] && init[i] == -inf)
      init[i] = flag ? mx[i / sz] : z[i];
    if (eq[i / sz])
      rep(k, sz) z[i / sz * sz + k] = mx[i / sz];
    eq[i / sz] = 0;
    z[i] = x;
    mx[i / sz] = min(mx[i / sz], x);
    i++;
  }
  flag = eq[(j - 1) / sz];
  while (i < j && j % sz) {
    j--;
    if (ini && !initialized[j / sz] && init[j] == -inf)
      init[j] = flag ? mx[j / sz] : z[j];
    if (eq[j / sz])
      rep(k, sz) z[j / sz * sz + k] = mx[j / sz];
    eq[j / sz] = 0;
    z[j] = x;
    mx[j / sz] = min(mx[j / sz], x);
  }
  i /= sz;
  j /= sz;
  while (i < j) {
    if (ini && !initialized[i]) {
      rep(k, sz) if (init[i * sz + k] == -inf) init[i * sz + k] =
          eq[i] ? mx[i] : z[i * sz + k];
      initialized[i] = 1;
    }
    eq[i] = 1;
    mx[i++] = x;
  }
}
inline int get(int i, int j) {
  int res = inf;
  while (i < j && i % sz)
    res = min(res, z[i++]);
  while (i < j && j % sz)
    res = min(res, z[--j]);
  i /= sz;
  j /= sz;
  while (i < j)
    res = min(res, mx[i++]);
  return res;
}

bool func(bool ck) {
  rep(i, q) {
    if (t[i] == 0) {
      if (!ck)
        takemax(a[i], b[i], y[i]);
      else if (get(a[i], b[i]) != y[i])
        return 0;
    } else
      set(a[i], b[i], y[i], !ck);
  }
  if (!ck)
    rep(i, m) if (init[i] == -inf) init[i] = eq[i / sz] ? mx[i] : z[i];
  return 1;
}

int main() {
  scanf("%d", &q);
  rep(i, q) {
    scanf("%d%d%d%d", t + i, a + i, b + i, y + i);
    vs[2 * i] = a[i];
    vs[2 * i + 1] = ++b[i];
  }
  sort(vs, vs + 2 * q);
  m = unique(vs, vs + 2 * q) - vs;
  rep(i, q) {
    a[i] = lower_bound(vs, vs + m, a[i]) - vs;
    b[i] = lower_bound(vs, vs + m, b[i]) - vs;
  }

  rep(i, m) z[i] = init[i] = -inf;
  rep(i, (m + sz - 1) / sz) mx[i] = -inf, eq[i] = 1;
  func(0);
  rep(i, m) z[i] = init[i];
  rep(i, (m + sz - 1) / sz) {
    eq[i] = 1;
    rep(j, sz) if (i * sz + j < m && z[i * sz] != z[i * sz + j]) eq[i] = 0;
  }
  puts(func(1) ? "YES" : "NO");

  return 0;
}