Problem p03033 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

const int N = 200005;
int d[N];
int s[N], t[N], x[N];

template <typename T> class SquareRootDecomposition {
  int div;
  vector<T> a, b;
  function<T(T, T)> func;

public:
  SquareRootDecomposition<T>(const int n, T def, function<T(T, T)> _func) {
    LL ok = n;
    LL ng = 1;
    while (ok - ng > 1) {
      LL mid = (ok + ng) / 2;
      if (mid * mid >= n)
        ok = mid;
      else
        ng = mid;
    }
    div = (int)ok;
    a = vector<T>((n - div + 1) / div, def);
    b = vector<T>(n, def);
    func = _func;
  }
  void update(int l, int r, T v) { // update [l, r)
    int as = (l + div - 1) / div;
    int ae = r / div;
    FOR(i, as, ae) a[i] = func(a[i], v);
    FOR(i, l, min(as * div, r)) b[i] = func(b[i], v);
    FOR(i, max(ae * div, l), r) b[i] = func(b[i], v);
  }
  T query(int idx) { return func(a[idx / div], b[idx]); }
};

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  REP(i, n) scanf("%d%d%d", &s[i], &t[i], &x[i]);
  REP(i, q) scanf("%d", &d[i]);
  SquareRootDecomposition<int> srd(q, 0x7f7f7f7f,
                                   [](int a, int b) { return min(a, b); });
  REP(i, n) {
    int l = s[i] - x[i];
    int r = t[i] - x[i];
    int lidx = lower_bound(d, d + q, l) - d;
    int ridx = upper_bound(d, d + q, r - 1) - d;
    srd.update(lidx, ridx, x[i]);
  }
  REP(i, q) {
    int ans = srd.query(i);
    if (ans > 1e9 + 5)
      ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define M_PI 3.14159265358979323846

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define FILL(a, x) memset(a, x, sizeof(a))

// repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

const int N = 200005;
int d[N];
int s[N], t[N], x[N];

template <typename T> class SquareRootDecomposition {
  int div;
  vector<T> a, b;
  function<T(T, T)> func;

public:
  SquareRootDecomposition<T>(const int n, T def, function<T(T, T)> _func) {
    LL ok = n;
    LL ng = 1;
    while (ok - ng > 1) {
      LL mid = (ok + ng) / 2;
      if (mid * mid >= n)
        ok = mid;
      else
        ng = mid;
    }
    div = (int)ok;
    a = vector<T>((n + div - 1) / div, def);
    b = vector<T>(n, def);
    func = _func;
  }
  void update(int l, int r, T v) { // update [l, r)
    int as = (l + div - 1) / div;
    int ae = r / div;
    FOR(i, as, ae) a[i] = func(a[i], v);
    FOR(i, l, min(as * div, r)) b[i] = func(b[i], v);
    FOR(i, max(ae * div, l), r) b[i] = func(b[i], v);
  }
  T query(int idx) { return func(a[idx / div], b[idx]); }
};

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  REP(i, n) scanf("%d%d%d", &s[i], &t[i], &x[i]);
  REP(i, q) scanf("%d", &d[i]);
  SquareRootDecomposition<int> srd(q, 0x7f7f7f7f,
                                   [](int a, int b) { return min(a, b); });
  REP(i, n) {
    int l = s[i] - x[i];
    int r = t[i] - x[i];
    int lidx = lower_bound(d, d + q, l) - d;
    int ridx = upper_bound(d, d + q, r - 1) - d;
    srd.update(lidx, ridx, x[i]);
  }
  REP(i, q) {
    int ans = srd.query(i);
    if (ans > 1e9 + 5)
      ans = -1;
    printf("%d\n", ans);
  }
  return 0;
}