#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template bool chmax(T &m, const T q) { return m < q ? (m = q, true) : false; } template bool chmin(T &m, const T q) { return m > q ? (m = q, true) : false; } const std::vector> grid_dxs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int floor_lg(long long x) { return x <= 0 ? -1 : 63 - __builtin_clzll(x); } template T1 floor_div(T1 num, T2 den) { return (num > 0 ? num / den : -((-num + den - 1) / den)); } template std::pair operator+(const std::pair &l, const std::pair &r) { return std::make_pair(l.first + r.first, l.second + r.second); } template std::pair operator-(const std::pair &l, const std::pair &r) { return std::make_pair(l.first - r.first, l.second - r.second); } template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template int arglb(const std::vector &v, const T &x) { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } template int argub(const std::vector &v, const T &x) { return std::distance(v.begin(), std::upper_bound(v.begin(), v.end(), x)); } template IStream &operator>>(IStream &is, std::vector &vec) { for (auto &v : vec) is >> v; return is; } template OStream &operator<<(OStream &os, const std::vector &vec); template OStream &operator<<(OStream &os, const std::array &arr); template OStream &operator<<(OStream &os, const std::unordered_set &vec); template OStream &operator<<(OStream &os, const pair &pa); template OStream &operator<<(OStream &os, const std::deque &vec); template OStream &operator<<(OStream &os, const std::set &vec); template OStream &operator<<(OStream &os, const std::multiset &vec); template OStream &operator<<(OStream &os, const std::unordered_multiset &vec); template OStream &operator<<(OStream &os, const std::pair &pa); template OStream &operator<<(OStream &os, const std::map &mp); template OStream &operator<<(OStream &os, const std::unordered_map &mp); template OStream &operator<<(OStream &os, const std::tuple &tpl); template OStream &operator<<(OStream &os, const std::vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::array &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; } template std::istream &operator>>(std::istream &is, std::tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template OStream &operator<<(OStream &os, const std::tuple &tpl) { os << '('; std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os << ')'; } template OStream &operator<<(OStream &os, const std::unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template OStream &operator<<(OStream &os, const std::set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::pair &pa) { return os << '(' << pa.first << ',' << pa.second << ')'; } template OStream &operator<<(OStream &os, const std::map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template OStream &operator<<(OStream &os, const std::unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define dbg(x) std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl #define dbgif(cond, x) ((cond) ? std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET << std::endl : std::cerr) #else #define dbg(x) ((void)0) #define dbgif(cond, x) ((void)0) #endif #ifndef ATCODER_INTERNAL_BITOP_HPP #define ATCODER_INTERNAL_BITOP_HPP 1 #ifdef _MSC_VER #include #endif #if __cplusplus >= 202002L #include #endif namespace atcoder { namespace internal { #if __cplusplus >= 202002L using std::bit_ceil; #else // @return same with std::bit::bit_ceil unsigned int bit_ceil(unsigned int n) { unsigned int x = 1; while (x < (unsigned int)(n)) x *= 2; return x; } #endif // @param n `1 <= n` // @return same with std::bit::countr_zero int countr_zero(unsigned int n) { #ifdef _MSC_VER unsigned long index; _BitScanForward(&index, n); return index; #else return __builtin_ctz(n); #endif } // @param n `1 <= n` // @return same with std::bit::countr_zero constexpr int countr_zero_constexpr(unsigned int n) { int x = 0; while (!(n & (1 << x))) x++; return x; } } // namespace internal } // namespace atcoder #endif // ATCODER_INTERNAL_BITOP_HPP #ifndef ATCODER_LAZYSEGTREE_HPP #define ATCODER_LAZYSEGTREE_HPP 1 #include #include #include // #include "atcoder/internal_bit" namespace atcoder { template struct lazy_segtree { static_assert(std::is_convertible_v>, "op must work as S(S, S)"); static_assert(std::is_convertible_v>, "e must work as S()"); static_assert(std::is_convertible_v>, "mapping must work as S(F, S)"); static_assert(std::is_convertible_v>, "composition must work as F(F, F)"); static_assert(std::is_convertible_v>, "id must work as F()"); public: lazy_segtree() : lazy_segtree(0) {} explicit lazy_segtree(int n) : lazy_segtree(std::vector(n, e())) {} explicit lazy_segtree(const std::vector &v) : _n(int(v.size())) { size = (int)internal::bit_ceil((unsigned int)(_n)); log = internal::countr_zero((unsigned int)size); d = std::vector(2 * size, e()); lz = std::vector(size, id()); for (int i = 0; i < _n; i++) d[size + i] = v[i]; for (int i = size - 1; i >= 1; i--) { update(i); } } void set(int p, S x) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = x; for (int i = 1; i <= log; i++) update(p >> i); } S get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return d[p]; } S prod(int l, int r) { assert(0 <= l && l <= r && r <= _n); if (l == r) return e(); l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } S sml = e(), smr = e(); while (l < r) { if (l & 1) sml = op(sml, d[l++]); if (r & 1) smr = op(d[--r], smr); l >>= 1; r >>= 1; } return op(sml, smr); } S all_prod() { return d[1]; } void apply(int p, F f) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); d[p] = mapping(f, d[p]); for (int i = 1; i <= log; i++) update(p >> i); } void apply(int l, int r, F f) { assert(0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--) { if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } { int l2 = l, r2 = r; while (l < r) { if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } l = l2; r = r2; } for (int i = 1; i <= log; i++) { if (((l >> i) << i) != l) update(l >> i); if (((r >> i) << i) != r) update((r - 1) >> i); } } template int max_right(int l) { return max_right(l, [](S x) { return g(x); }); } template int max_right(int l, G g) { assert(0 <= l && l <= _n); assert(g(e())); if (l == _n) return _n; l += size; for (int i = log; i >= 1; i--) push(l >> i); S sm = e(); do { while (l % 2 == 0) l >>= 1; if (!g(op(sm, d[l]))) { while (l < size) { push(l); l = (2 * l); if (g(op(sm, d[l]))) { sm = op(sm, d[l]); l++; } } return l - size; } sm = op(sm, d[l]); l++; } while ((l & -l) != l); return _n; } template int min_left(int r) { return min_left(r, [](S x) { return g(x); }); } template int min_left(int r, G g) { assert(0 <= r && r <= _n); assert(g(e())); if (r == 0) return 0; r += size; for (int i = log; i >= 1; i--) push((r - 1) >> i); S sm = e(); do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!g(op(d[r], sm))) { while (r < size) { push(r); r = (2 * r + 1); if (g(op(d[r], sm))) { sm = op(d[r], sm); r--; } } return r + 1 - size; } sm = op(d[r], sm); } while ((r & -r) != r); return 0; } protected: int _n, size, log; std::vector d; std::vector lz; void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); } virtual void all_apply(int k, F f) { d[k] = mapping(f, d[k]); if (k < size) lz[k] = composition(f, lz[k]); } void push(int k) { all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; } // namespace atcoder #endif // ATCODER_LAZYSEGTREE_HPP // Reference: https://atcoder.github.io/ac-library/production/document_ja/lazysegtree.html // https://betrue12.hateblo.jp/entry/2020/09/22/194541 // https://betrue12.hateblo.jp/entry/2020/09/23/005940 /* struct S {}; S op(S l, S r) { return {}; } S e() { return {}; }; using F = bool; S mp(F f, S x) { return x; } F composition(F fnew, F gold) { return fnew ^ gold; } F id() { return false; } vector A; atcoder::lazy_segtree seg(A); */ #include #include template class segtree_beats : public atcoder::lazy_segtree { using Base = atcoder::lazy_segtree; using Base::lazy_segtree; void all_apply(int k, F f) override { Base::d[k] = mapping(f, Base::d[k]); if (k < Base::size) { Base::lz[k] = composition(f, Base::lz[k]); if (Base::d[k].fail) Base::push(k), Base::update(k); } } }; namespace RangeChMinMaxAddSum { template inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { assert(a <= a2); // a < a2 or a == a2 == INF assert(c <= c2); // c < c2 or c == c2 == -INF return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c); } template inline Num second_highest(Num a, Num a2, Num b, Num b2) noexcept { assert(a >= a2); // a > a2 or a == a2 == -INF assert(b >= b2); // b > b2 or b == b2 == INF return a == b ? std::max(a2, b2) : a2 >= b ? a2 : b2 >= a ? b2 : std::min(a, b); } using BNum = long long; constexpr BNum BINF = 1LL << 61; struct S { BNum lo, hi, lo2, hi2, sum; unsigned sz, nlo, nhi; bool fail; S() : lo(BINF), hi(-BINF), lo2(BINF), hi2(-BINF), sum(0), sz(0), nlo(0), nhi(0), fail(0) {} S(BNum x, unsigned sz_) : lo(x), hi(x), lo2(BINF), hi2(-BINF), sum(x * sz_), sz(sz_), nlo(sz_), nhi(sz_), fail(0) {} friend std::ostream &operator<<(std::ostream &os, const S s) { return os << "[(" << s.lo << "x" << s.nlo << ", " << s.lo2 << ", " << s.hi2 << ", " << s.hi << "x" << s.nhi << "), sz=" << s.sz << ", sum=" << s.sum << "]"; } }; S e() { return S(); } S op(S l, S r) { if (l.lo > l.hi) return r; if (r.lo > r.hi) return l; S ret; ret.lo = std::min(l.lo, r.lo); ret.hi = std::max(l.hi, r.hi); ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2), ret.hi2 = second_highest(l.hi, l.hi2, r.hi, r.hi2); ret.sum = l.sum + r.sum; ret.sz = l.sz + r.sz; ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo); ret.nhi = l.nhi * (l.hi >= r.hi) + r.nhi * (r.hi >= l.hi); return ret; } struct F { BNum lb, ub, bias; F() : lb(-BINF), ub(BINF), bias(0) {} F(BNum chmax_, BNum chmin_, BNum add) : lb(chmax_), ub(chmin_), bias(add) {} static F chmin(BNum x) noexcept { return F(-BINF, x, BNum(0)); } static F chmax(BNum x) noexcept { return F(x, BINF, BNum(0)); } static F add(BNum x) noexcept { return F(-BINF, BINF, x); }; }; F composition(F fnew, F fold) { F ret; ret.lb = std::max(std::min(fold.lb + fold.bias, fnew.ub), fnew.lb) - fold.bias; ret.ub = std::min(std::max(fold.ub + fold.bias, fnew.lb), fnew.ub) - fold.bias; ret.bias = fold.bias + fnew.bias; return ret; } F id() { return F(); } S mapping(F f, S x) { if (x.sz == 0) return e(); // f の作用後 x の要素が 1 種類だけになるケース if (x.lo == x.hi or f.lb == f.ub or f.lb >= x.hi or f.ub <= x.lo) { return S(std::min(std::max(x.lo, f.lb), f.ub) + f.bias, x.sz); } // 2 種類 -> 1 種類 if (x.lo2 == x.hi) { x.lo = x.hi2 = std::max(x.lo, f.lb) + f.bias; x.hi = x.lo2 = std::min(x.hi, f.ub) + f.bias; x.sum = x.lo * x.nlo + x.hi * x.nhi; return x; } // lo と lo2, hi と hi2 が潰れないケース if (f.lb < x.lo2 and f.ub > x.hi2) { BNum nxt_lo = std::max(x.lo, f.lb), nxt_hi = std::min(x.hi, f.ub); x.sum += (nxt_lo - x.lo) * x.nlo - (x.hi - nxt_hi) * x.nhi + f.bias * x.sz; x.lo = nxt_lo + f.bias, x.hi = nxt_hi + f.bias, x.lo2 += f.bias, x.hi2 += f.bias; return x; } x.fail = 1; return x; } using segtree = segtree_beats; } // namespace RangeChMinMaxAddSum // Verified: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0427 namespace RangeAddChmaxRangeSum { #include template inline Num second_lowest(Num a, Num a2, Num c, Num c2) noexcept { // a < a2, c < c2 return a == c ? std::min(a2, c2) : a2 <= c ? a2 : c2 <= a ? c2 : std::max(a, c); } using Num = lint; constexpr Num INF = 1LL << 60; struct S { Num lo, lo2, sum; unsigned sz, nlo; bool fail; S() : lo(INF), lo2(INF), sum(0), sz(0), nlo(0), fail(false) {} S(Num x, unsigned sz_ = 1) : lo(x), lo2(INF), sum(Num(x) * sz_), sz(sz_), nlo(sz_), fail(false) {} }; S e() { return S(); } S op(S l, S r) { S ret; ret.lo = std::min(l.lo, r.lo); ret.lo2 = second_lowest(l.lo, l.lo2, r.lo, r.lo2); ret.sum = l.sum + r.sum, ret.sz = l.sz + r.sz; ret.nlo = l.nlo * (l.lo <= r.lo) + r.nlo * (r.lo <= l.lo); return ret; } struct F { Num lb, bias; F() : lb(-INF), bias(0) {} F(Num chmax_, Num add) : lb(chmax_), bias(add) {} static F chmax(Num x) noexcept { return F(x, Num(0)); } static F add(Num x) noexcept { return F(-INF, x); }; }; F composition(F fnew, F fold) { F ret; ret.lb = std::max(fold.lb + fold.bias, fnew.lb) - fold.bias; ret.bias = fold.bias + fnew.bias; return ret; } F id() { return F(); } S mapping(F f, S x) { if (x.sz == 0) return e(); if (f.lb < x.lo2) { Num nxt_lo = std::max(x.lo, f.lb); x.sum += (nxt_lo - x.lo) * x.nlo + f.bias * x.sz; x.lo = nxt_lo + f.bias, x.lo2 += f.bias; return x; } x.fail = 1; return x; } using segtree = segtree_beats; } // namespace RangeAddChmaxRangeSum int main() { int N, K, Q; cin >> N >> K >> Q; vector A(N); cin >> A; vector init; for (auto a : A) init.push_back(RangeAddChmaxRangeSum::S{a, 1}); vector> updates(K); for (auto &[l, r, x] : updates) cin >> l >> r >> x, --l; vector> query(Q); for (auto &[l, r, x] : query) cin >> l >> r >> x, --l; vector ok(Q, K + 1), ng(Q, -1); REP(_, 16) { vector> t2qs(K + 1); REP(q, Q) { const int t = (ok.at(q) + ng.at(q)) / 2; t2qs.at(t).push_back(q); } RangeAddChmaxRangeSum::segtree tree(init); REP(t, K + 1) { if (t) { auto [l, r, x] = updates.at(t - 1); tree.apply(l, r, RangeAddChmaxRangeSum::F::chmax(x)); } for (int q : t2qs.at(t)) { auto [l, r, x] = query.at(q); auto p = tree.prod(l, r); (p.sum >= x ? ok : ng).at(q) = t; } } } for (auto t : ok) { if (t == K + 1) t = -1; cout << t << '\n'; } }