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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

class sqrt_decomposition {
public:
  const int B = 1000;
  int n;
  vector<int> data, lazy, bmin;
  vector<bool> flag;
  sqrt_decomposition(int n, int init)
      : n(n), data(n, init), lazy((n + B - 1) / B), flag((n + B - 1) / B),
        bmin((n + B - 1) / B, init) {}

  void propagate(int i) {
    assert(flag[i]);
    flag[i] = false;
    rep(j, 0, B) {
      if (i * B + j >= n)
        break;
      data[i * B + j] = lazy[i];
    }
    bmin[i] = lazy[i];
  }

  //[s,t)???x????????´??????
  void update(int s, int t, int x) {
    {
      int i = s / B;
      bool updated = false;
      while (s < t and s % B != 0) {
        updated = true;
        if (flag[s / B])
          propagate(s / B);
        data[s] = x;
        ++s;
      }
      if (updated) {
        int tmp = numeric_limits<int>::max();
        rep(j, 0, B) {
          if (i * B + j >= n)
            break;
          tmp = min(tmp, data[i * B + j]);
        }
        bmin[i] = tmp;
      }
    }

    {
      int i = (t - 1) / B;
      bool updated = false;
      while (s < t and t % B != 0) {
        updated = true;
        --t;
        if (flag[t / B])
          propagate(t / B);
        data[t] = x;
      }
      if (updated) {
        int tmp = numeric_limits<int>::max();
        rep(j, 0, B) {
          if (i * B + j >= n)
            break;
          tmp = min(tmp, data[i * B + j]);
        }
        bmin[i] = tmp;
      }
    }

    while (s < t) {
      int i = s / B;
      flag[i] = true;
      lazy[i] = x;
      s += B;
    }
  }

  //[s,t)???????°?????????????
  int get_min(int s, int t) {
    int res = numeric_limits<int>::max();
    while (s < t and s % B != 0) {
      if (flag[s / B])
        propagate(s / B);
      res = min(res, data[s]);
      ++s;
    }
    while (s < t and t % B != 0) {
      --t;
      if (flag[t / B])
        propagate(t / B);
      res = min(res, data[t]);
    }

    while (s < t) {
      int i = s / B;
      if (flag[i])
        res = min(res, lazy[i]);
      else
        res = min(res, bmin[i]);
      s += B;
    }
    return res;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  sqrt_decomposition sd(n, (1LL << 31) - 1);

  rep(i, 0, q) {
    int k;
    cin >> k;
    if (k == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      ++t;
      sd.update(s, t, x);
    } else {
      int s, t;
      cin >> s >> t;
      ++t;
      cout << sd.get_min(s, t) << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

class sqrt_decomposition {
public:
  const int B = 1000;
  int n;
  vector<int> data, lazy, bmin;
  vector<bool> flag;
  sqrt_decomposition(int n, int init)
      : n(n), data(n, init), lazy((n + B - 1) / B), flag((n + B - 1) / B),
        bmin((n + B - 1) / B, init) {}

  void propagate(int i) {
    assert(flag[i]);
    flag[i] = false;
    rep(j, 0, B) {
      if (i * B + j >= n)
        break;
      data[i * B + j] = lazy[i];
    }
    bmin[i] = lazy[i];
  }
  //[s,t)???x????????´??????
  void update(int s, int t, int x) {
    {
      int id = s / B;
      while (s < t and s % B != 0) {
        if (flag[s / B])
          propagate(s / B);
        data[s] = x;
        ++s;
      }
      if (id != s / B)
        bmin[id] = get_min(id * B, min(id * B + B, n));
    }

    {
      int id = t / B;
      while (s < t and t % B != 0) {
        --t;
        if (flag[t / B])
          propagate(t / B);
        data[t] = x;
      }
      if (id != s / B)
        bmin[id] = get_min(id * B, min(id * B + B, n));
    }

    while (s < t) {
      int i = s / B;
      flag[i] = true;
      lazy[i] = x;
      s += B;
    }
  }

  //[s,t)???????°?????????????
  int get_min(int s, int t) {
    int res = numeric_limits<int>::max();
    while (s < t and s % B != 0) {
      if (flag[s / B])
        propagate(s / B);
      res = min(res, data[s]);
      ++s;
    }
    while (s < t and t % B != 0) {
      --t;
      if (flag[t / B])
        propagate(t / B);
      res = min(res, data[t]);
    }

    while (s < t) {
      int i = s / B;
      if (flag[i])
        res = min(res, lazy[i]);
      else
        res = min(res, bmin[i]);
      s += B;
    }
    return res;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  sqrt_decomposition sd(n, (1LL << 31) - 1);

  rep(i, 0, q) {
    int k;
    cin >> k;
    if (k == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      ++t;
      sd.update(s, t, x);
    } else {
      int s, t;
      cin >> s >> t;
      ++t;
      cout << sd.get_min(s, t) << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(16);
  solve();
  return 0;
}