Problem p02350 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N (1 << 10) //(1 << 18)
#define LV 10       // 18

class SegmentTree {
  const ll inf = (1LL << 31) - 1;

  int n0, n;
  ll data[4 * N], lazy[4 * N];
  int ids[2 * LV], cur = 0;

  void update_ids(int l, int r) {
    int l0 = (l + n0), r0 = (r + n0);
    int lb = (l0 & -l0) >> 1, rb = (r0 & -r0) >> 1;
    l0 >>= 1;
    r0 >>= 1;
    cur = 0;
    while (l0 > 0 && l0 < r0) {
      if (!rb)
        ids[cur++] = r0;
      if (!lb)
        ids[cur++] = l0;
      lb >>= 1;
      rb >>= 1;
      l0 >>= 1;
      r0 >>= 1;
    }
    while (l0 > 0) {
      ids[cur++] = l0;
      l0 >>= 1;
    }
  }

  void propagates() {
    repr(i, cur) {
      int k = ids[i];

      ll v = lazy[k - 1];
      if (v == -1)
        continue;

      lazy[2 * k - 1] = data[2 * k - 1] = v;
      lazy[2 * k] = data[2 * k] = v;
      lazy[k - 1] = -1;
    }
  }

public:
  SegmentTree(int n) : n(n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;
    rep(i, 2 * n0) data[i] = inf, lazy[i] = -1;
  }

  void update(int l, int r, ll x) {
    update_ids(l, r);
    propagates();

    int l0 = l + n0, r0 = r + n0;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        lazy[r0 - 1] = data[r0 - 1] = x;
      }
      if (l0 & 1) {
        lazy[l0 - 1] = data[l0 - 1] = x;
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }

    rep(i, cur) {
      int k = ids[i];
      data[k - 1] = min(data[2 * k - 1], data[2 * k]);
    }
  }

  ll query(int l, int r) {
    update_ids(l, r);
    propagates();

    int l0 = l + n0, r0 = r + n0;

    ll s = inf;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        s = min(s, data[r0 - 1]);
      }
      if (l0 & 1) {
        s = min(s, data[l0 - 1]);
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }
    return s;
  }
};

int n, q;

int main() {
  cin >> n >> q;
  SegmentTree st(n);
  rep(i, q) {
    int c, s, t, x;
    cin >> c;
    switch (c) {
    case 0:
      cin >> s >> t >> x;
      st.update(s, t + 1, x);
      break;
    case 1:
      cin >> s >> t;
      cout << st.query(s, t + 1) << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \
  for (int i = 0; i < n; ++i)                                                  \
    cout << val << " ";                                                        \
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N (1 << 18)
#define LV 18

class SegmentTree {
  const ll inf = (1LL << 31) - 1;

  int n0, n;
  ll data[4 * N], lazy[4 * N];
  int ids[2 * LV], cur = 0;

  void update_ids(int l, int r) {
    int l0 = (l + n0), r0 = (r + n0);
    int lb = (l0 & -l0) >> 1, rb = (r0 & -r0) >> 1;
    l0 >>= 1;
    r0 >>= 1;
    cur = 0;
    while (l0 > 0 && l0 < r0) {
      if (!rb)
        ids[cur++] = r0;
      if (!lb)
        ids[cur++] = l0;
      lb >>= 1;
      rb >>= 1;
      l0 >>= 1;
      r0 >>= 1;
    }
    while (l0 > 0) {
      ids[cur++] = l0;
      l0 >>= 1;
    }
  }

  void propagates() {
    repr(i, cur) {
      int k = ids[i];

      ll v = lazy[k - 1];
      if (v == -1)
        continue;

      lazy[2 * k - 1] = data[2 * k - 1] = v;
      lazy[2 * k] = data[2 * k] = v;
      lazy[k - 1] = -1;
    }
  }

public:
  SegmentTree(int n) : n(n) {
    n0 = 1;
    while (n0 < n)
      n0 <<= 1;
    rep(i, 2 * n0) data[i] = inf, lazy[i] = -1;
  }

  void update(int l, int r, ll x) {
    update_ids(l, r);
    propagates();

    int l0 = l + n0, r0 = r + n0;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        lazy[r0 - 1] = data[r0 - 1] = x;
      }
      if (l0 & 1) {
        lazy[l0 - 1] = data[l0 - 1] = x;
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }

    rep(i, cur) {
      int k = ids[i];
      data[k - 1] = min(data[2 * k - 1], data[2 * k]);
    }
  }

  ll query(int l, int r) {
    update_ids(l, r);
    propagates();

    int l0 = l + n0, r0 = r + n0;

    ll s = inf;
    while (l0 < r0) {
      if (r0 & 1) {
        --r0;
        s = min(s, data[r0 - 1]);
      }
      if (l0 & 1) {
        s = min(s, data[l0 - 1]);
        ++l0;
      }
      l0 >>= 1;
      r0 >>= 1;
    }
    return s;
  }
};

int n, q;

int main() {
  cin >> n >> q;
  SegmentTree st(n);
  rep(i, q) {
    int c, s, t, x;
    cin >> c;
    switch (c) {
    case 0:
      cin >> s >> t >> x;
      st.update(s, t + 1, x);
      break;
    case 1:
      cin >> s >> t;
      cout << st.query(s, t + 1) << endl;
      break;
    }
  }
  return 0;
}