Problem p03033 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
// BEGIN CUT HERE
struct RUP {
  int n;
  vector<int> dat, laz;
  const int def = INT_MAX;
  RUP() {}
  RUP(int n_) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, def);
    laz.clear();
    laz.resize(2 * n - 1, -1);
  }
  inline void eval(int len, int k) {
    if (laz[k] < 0)
      return;
    if (k * 2 + 1 < n * 2 - 1) {
      laz[k * 2 + 1] = laz[k];
      laz[k * 2 + 2] = laz[k];
    }
    dat[k] = laz[k];
    laz[k] = -1;
  }
  int update(int a, int b, int x, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l)
      return dat[k];
    if (a <= l && r <= b)
      return laz[k] = x;
    eval(r - l, k);
    dat[k] = min(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                 update(a, b, x, k * 2 + 2, (l + r) / 2, r));
    return dat[k];
  }
  int query(int a, int b, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  void update(int a, int b, int x) { update(a, b, x, 0, 0, n); }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};
// END CUT HERE
signed main() {
  int N, Q;
  cin >> N >> Q;

  vector<int> S(N), T(N), X(N);
  using P = pair<int, int>;
  using P3 = pair<int, P>;
  vector<P> xst(N);
  map<int, int> zip;
  vector<int> D(Q);
  {
    map<int, int> tmp;
    tmp[0] = 1;
    for (int i = 0; i < N; i++) {
      cin >> S[i] >> T[i] >> X[i];
      // tmp[S[i]] = tmp[T[i]] = 1;
      tmp[max(0LL, S[i] - X[i])] = tmp[max(0LL, T[i] - X[i])] = 1;
      xst[i].first = X[i];
      xst[i].second = i;
    }

    sort(xst.begin(), xst.end(), greater<P>());

    for (int i = 0; i < Q; i++) {
      cin >> D[i];
      tmp[D[i]] = 1;
    }
    tmp[INT_MAX] = 1;

    int cnt = 0;
    for (auto i : tmp) {
      zip[i.first] = cnt++;
    }
  }

  RUP dat(zip.size());
  for (int i = 0; i < N; i++) {
    int j = xst[i].second;
    int s = S[j], t = T[j], x = X[j];
    s = max(0LL, s - x);
    t = max(0LL, t - x);
    if (s == t)
      continue;
    dat.update(zip[s], zip[t], x);
  }

  for (int i = 0; i < Q; i++) {
    int res = dat.query(zip[D[i]], zip[D[i]] + 1);
    if (res == INT_MAX)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
// BEGIN CUT HERE
struct RUP {
  int n;
  vector<int> dat, laz;
  const int def = INT_MAX;
  RUP() {}
  RUP(int n_) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1, def);
    laz.clear();
    laz.resize(2 * n - 1, -1);
  }
  inline void eval(int len, int k) {
    if (laz[k] < 0)
      return;
    if (k * 2 + 1 < n * 2 - 1) {
      laz[k * 2 + 1] = laz[k];
      laz[k * 2 + 2] = laz[k];
    }
    dat[k] = laz[k];
    laz[k] = -1;
  }
  int update(int a, int b, int x, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l)
      return dat[k];
    if (a <= l && r <= b)
      return laz[k] = x;
    eval(r - l, k);
    dat[k] = min(update(a, b, x, k * 2 + 1, l, (l + r) / 2),
                 update(a, b, x, k * 2 + 2, (l + r) / 2, r));
    return dat[k];
  }
  int query(int a, int b, int k, int l, int r) {
    eval(r - l, k);
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return dat[k];
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  void update(int a, int b, int x) { update(a, b, x, 0, 0, n); }
  int query(int a, int b) { return query(a, b, 0, 0, n); }
};
// END CUT HERE
signed main() {
  int N, Q;
  cin >> N >> Q;

  vector<int> S(N), T(N), X(N);
  using P = pair<int, int>;
  using P3 = pair<int, P>;
  vector<P> xst(N);
  map<int, int> zip;
  vector<int> D(Q);
  {
    map<int, int> tmp;
    tmp[0] = 1;
    for (int i = 0; i < N; i++) {
      cin >> S[i] >> T[i] >> X[i];
      // tmp[S[i]] = tmp[T[i]] = 1;
      if (S[i] - X[i] > 0)
        tmp[S[i] - X[i]] = 1;
      if (T[i] - X[i] > 0)
        tmp[T[i] - X[i]] = 1;
      xst[i].first = X[i];
      xst[i].second = i;
    }

    sort(xst.begin(), xst.end(), greater<P>());

    for (int i = 0; i < Q; i++) {
      cin >> D[i];
      tmp[D[i]] = 1;
    }
    tmp[INT_MAX] = 1;

    int cnt = 0;
    for (auto i : tmp) {
      zip[i.first] = cnt++;
    }
  }

  RUP dat(zip.size());
  for (int i = 0; i < N; i++) {
    int j = xst[i].second;
    int s = S[j], t = T[j], x = X[j];
    s = max(0LL, s - x);
    t = max(0LL, t - x);
    if (s == t)
      continue;
    dat.update(zip[s], zip[t], x);
  }

  for (int i = 0; i < Q; i++) {
    int res = dat.query(zip[D[i]], zip[D[i]] + 1);
    if (res == INT_MAX)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
}