#ifndef LOCAL #define FAST_IO #endif // ============ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define OVERRIDE(a, b, c, d, ...) d #define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i) #define REP3(i, m, n) for (i32 i = (i32)(m); i < (i32)(n); ++i) #define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__) #define PER(i, n) for (i32 i = (i32)(n) - 1; i >= 0; --i) #define ALL(x) begin(x), end(x) using namespace std; using u32 = unsigned int; using u64 = unsigned long long; using i32 = signed int; using i64 = signed long long; using f64 = double; using f80 = long double; template using Vec = vector; template bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template Vec> runlength(const Vec &a) { if (a.empty()) { return Vec>(); } Vec> ret; i32 prv = 0; REP(i, 1, a.size()) { if (a[i - 1] != a[i]) { ret.emplace_back(prv, i, a[i - 1]); prv = i; } } ret.emplace_back(prv, (i32)a.size(), a.back()); return ret; } #ifdef INT128 using u128 = __uint128_t; using i128 = __int128_t; istream &operator>>(istream &is, i128 &x) { i64 v; is >> v; x = v; return is; } ostream &operator<<(ostream &os, i128 x) { os << (i64)x; return os; } istream &operator>>(istream &is, u128 &x) { u64 v; is >> v; x = v; return is; } ostream &operator<<(ostream &os, u128 x) { os << (u64)x; return os; } #endif [[maybe_unused]] constexpr i32 INF = 1000000100; [[maybe_unused]] constexpr i64 INF64 = 3000000000000000100; struct SetUpIO { SetUpIO() { #ifdef FAST_IO ios::sync_with_stdio(false); cin.tie(nullptr); #endif cout << fixed << setprecision(15); } } set_up_io; // ============ #ifdef DEBUGF #else #define DBG(x) (void)0 #endif struct Val { i64 sum, mx, lmx, rmx, mx1; }; struct Ops { using Value = Val; static Val id() { return Val{0, -INF64, -INF64, -INF64, -INF64}; } static Val op(Val l, Val r) { return Val{l.sum + r.sum, max({l.mx, r.mx, l.rmx + r.lmx}), max(l.lmx, l.sum + r.lmx), max(r.rmx, r.sum + l.rmx), max(l.mx1, r.mx1)}; } }; // ============ #include #include #include // ============ #include #include template struct Add { using Value = T; static Value id() { return T(0); } static Value op(const Value &lhs, const Value &rhs) { return lhs + rhs; } static Value inv(const Value &x) { return -x; } }; template struct Mul { using Value = T; static Value id() { return Value(1); } static Value op(const Value &lhs, const Value &rhs) { return lhs * rhs; } static Value inv(const Value &x) { return Value(1) / x; } }; template struct Min { using Value = T; static Value id() { return std::numeric_limits::max(); } static Value op(const Value &lhs, const Value &rhs) { return std::min(lhs, rhs); } }; template struct Max { using Value = T; static Value id() { return std::numeric_limits::min(); } static Value op(const Value &lhs, const Value &rhs) { return std::max(lhs, rhs); } }; template struct Xor { using Value = T; static Value id() { return T(0); } static Value op(const Value &lhs, const Value &rhs) { return lhs ^ rhs; } static Value inv(const Value &x) { return x; } }; template struct Reversible { using Value = std::pair; static Value id() { return Value(Monoid::id(), Monoid::id()); } static Value op(const Value &v1, const Value &v2) { return Value( Monoid::op(v1.first, v2.first), Monoid::op(v2.second, v1.second)); } }; // ============ template class SegmentTree { public: using Value = typename Monoid::Value; private: int old_length; int length; std::vector node; static int ceil2(int n) { int l = 1; while (l < n) { l <<= 1; } return l; } public: SegmentTree(int n) : old_length(n), length(ceil2(old_length)), node(length << 1, Monoid::id()) { assert(n >= 0); } SegmentTree(const std::vector &v) : old_length((int) v.size()), length(ceil2(old_length)), node(length << 1, Monoid::id()) { for (int i = 0; i < old_length; ++i) { node[i + length] = v[i]; } for (int i = length - 1; i > 0; --i) { node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]); } } template SegmentTree(int n, const F &f) : old_length(n), length(ceil2(n)), node(length << 1, Monoid::id()) { assert(n >= 0); for (int i = 0; i < old_length; ++i) { node[i + length] = f(i); } for (int i = length - 1; i > 0; --i) { node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]); } } const Value &operator[](int idx) const { assert(idx >= 0 && idx < old_length); return node[idx + length]; } void update(int idx, Value val) { assert(idx >= 0 && idx < old_length); idx += length; node[idx] = std::move(val); while (idx != 1) { idx >>= 1; node[idx] = Monoid::op(node[idx << 1], node[idx << 1 | 1]); } } Value prod(int l, int r) const { assert(l >= 0 && l <= r && r <= old_length); Value prodl = Monoid::id(); Value prodr = Monoid::id(); l += length; r += length; while (l != r) { if (l & 1) { prodl = Monoid::op(prodl, node[l++]); } if (r & 1) { prodr = Monoid::op(node[--r], prodr); } l >>= 1; r >>= 1; } return Monoid::op(prodl, prodr); } Value all_prod() const { return node[1]; } }; // ============ int main() { i32 n; cin >> n; Vec a(n), b(n); REP(i, n) { cin >> a[i] >> b[i]; } i32 q; cin >> q; Vec> queries(q); for (auto &[a, b, c] : queries) { cin >> a >> b >> c; --a; --b; } Vec orgsps(n + 1, 0); REP(i, n) { orgsps[i + 1] = orgsps[i] + b[i]; } Vec sps = orgsps; for (auto [a, b, c] : queries) { if (a == 0) { sps.push_back(b); } else { sps.push_back(b); sps.push_back(c); } } sort(ALL(sps)); sps.erase(unique(ALL(sps)), sps.end()); Vec vals(sps.size() - 1, Ops::id()); REP(i, sps.size() - 1) { i32 idx = (i32)(upper_bound(ALL(orgsps), sps[i]) - orgsps.begin() - 1); i64 pw = a[idx]; i64 len = sps[i + 1] - sps[i]; DBG(sps[i]); DBG(pw); DBG(len); vals[i] = Val{pw * len, max(0LL, pw * len), max(0LL, pw * len), max(0LL, pw * len), pw}; } SegmentTree seg(vals); for (auto [a, b, c] : queries) { if (a == 0) { i32 idx = (i32)(lower_bound(ALL(sps), b) - sps.begin()); seg.update(idx, {c, max(0LL, c), max(0LL, c), max(0LL, c), c}); } else { i32 l = (i32)(lower_bound(ALL(sps), b) - sps.begin()); i32 r = (i32)(lower_bound(ALL(sps), c) - sps.begin()); Val prod = seg.prod(l, r); if (prod.mx1 < 0) { cout << prod.mx1 << '\n'; } else { cout << prod.mx << '\n'; } } } }