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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

using Int = long long;

template <typename T, typename E> struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {}

  void init(int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }
  void build(const vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }
  inline void eval(int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(int k) {
    for (int i = height; i; i--)
      eval(k >> i);
  }
  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }
  void update(int a, int b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }
  T query(int a, int b) {
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
};

int main() {
  map<ll, ll> M;
  ll n, m, q;
  cin >> n >> m >> q;
  vector<pair<ll, pll>> A(m);
  for (auto &I : A) {
    cin >> I.S.F >> I.S.S >> I.F;
    I.S.F--;
    M[I.S.F] = M[I.S.S] = -1;
  }
  sort(A.begin(), A.end());
  vector<pair<ll, pll>> B(q);
  for (auto &I : B) {
    cin >> I.S.F >> I.S.S >> I.F;
    I.S.F--;
    M[I.S.F] = M[I.S.S] = -1;
  }
  M[m] = -1;
  ll num = 0;
  ll left = 0;
  vector<pll> K(M.size(), {0, 0});
  for (auto I = M.begin(); I != M.end(); ++I) {
    I->second = num;
    if (num != 0) {
      K[num - 1].S = I->first - left;
    }
    left = I->first;
    num++;
  }
  for (auto &I : A) {
    I.S.F = M[I.S.F];
    I.S.S = M[I.S.S];
  }
  for (auto &I : B) {
    I.S.F = M[I.S.F];
    I.S.S = M[I.S.S];
  }
  vector<pll> C(q);
  for (auto &I : C) {
    I.F = 0;
    I.S = m;
  }
  const ll D = 25;
  vector<pll> Q(q);

  using P = pair<Int, Int>;
  auto f = [](P a, P b) { return P(a.first + b.first, a.second + b.second); };
  auto g = [](P a, Int b) { return P(a.first + b * a.second, a.second); };
  auto h = [](Int a, Int b) { return a + b; };

  SegmentTree<P, Int> T(f, g, h, P(0, 0), 0);
  T.build(K);

  for (int time = 0; time < D; time++) {
    for (int i = 0; i < q; i++) {
      Q[i] = {C[i].F + (C[i].S - C[i].F) / 2, i};
    }
    sort(Q.begin(), Q.end());
    ll l = 0;
    for (int i = 0; i < m; i++) {
      while (l < q && Q[l].F == i) {
        ll cnt = T.query(B[Q[l].S].S.F, B[Q[l].S].S.S).F;
        if (cnt < B[Q[l].S].F) {
          C[Q[l].S].F = i;
        } else {
          C[Q[l].S].S = i;
        }
        l++;
      }
      T.update(A[i].S.F, A[i].S.S, 1);
    }
    for (auto &I : A) {
      T.update(I.S.F, I.S.S, -1);
    }
  }
  for (auto &I : C) {
    assert(I.F + 1 == I.S);
    cout << A[I.F].F << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

using Int = long long;

template <typename T, typename E> struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(F f, G g, H h, T ti, E ei) : f(f), g(g), h(h), ti(ti), ei(ei) {}

  void init(int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }
  void build(const vector<T> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }
  inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }
  inline void eval(int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(int k) {
    for (int i = height; i; i--)
      eval(k >> i);
  }
  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }
  void update(int a, int b, E x) {
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }
  T query(int a, int b) {
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }
};

int main() {
  ll n, m, q;
  cin >> n >> m >> q;
  vector<pair<ll, pll>> A(m);
  for (auto &I : A) {
    cin >> I.S.F >> I.S.S >> I.F;
    I.S.F--;
  }
  sort(A.begin(), A.end());
  vector<pair<ll, pll>> B(q);
  for (auto &I : B) {
    cin >> I.S.F >> I.S.S >> I.F;
    I.S.F--;
  }
  vector<pll> C(q);
  for (auto &I : C) {
    I.F = 0;
    I.S = m;
  }
  const ll D = 25;
  vector<pll> Q(q);

  using P = pair<Int, Int>;
  auto f = [](P a, P b) { return P(a.first + b.first, a.second + b.second); };
  auto g = [](P a, Int b) { return P(a.first + b * a.second, a.second); };
  auto h = [](Int a, Int b) { return a + b; };

  SegmentTree<P, Int> T(f, g, h, P(0, 0), 0);

  for (int time = 0; time < D; time++) {
    for (int i = 0; i < q; i++) {
      Q[i] = {C[i].F + (C[i].S - C[i].F) / 2, i};
    }
    sort(Q.begin(), Q.end());
    T.build(vector<P>(n, P(0, 1)));
    ll l = 0;
    for (int i = 0; i < m; i++) {
      while (l < q && Q[l].F == i) {
        ll cnt = T.query(B[Q[l].S].S.F, B[Q[l].S].S.S).F;
        if (cnt < B[Q[l].S].F) {
          C[Q[l].S].F = i;
        } else {
          C[Q[l].S].S = i;
        }
        l++;
      }
      T.update(A[i].S.F, A[i].S.S, 1);
    }
  }
  for (auto &I : C) {
    cout << A[I.F].F << endl;
  }

  return 0;
}