#line 1 "other/b.cpp" #include #line 7 "Library/alias.hpp" namespace workspace { constexpr char eol = '\n'; using namespace std; using i32 = int_least32_t; using i64 = int_least64_t; using i128 = __int128_t; using u32 = uint_least32_t; using u64 = uint_least64_t; using u128 = __uint128_t; template > using priority_queue = std::priority_queue, Comp>; template using stack = std::stack>; constexpr i32 clz32(const u32 &n) noexcept { return __builtin_clz(n); } constexpr i32 clz64(const u64 &n) noexcept { return __builtin_clzll(n); } constexpr i32 ctz(const u64 &n) noexcept { return __builtin_ctzll(n); } constexpr i32 popcnt(const u64 &n) noexcept { return __builtin_popcountll(n); } } // namespace workspace #line 5 "Library/config.hpp" namespace config { const auto start_time{std::chrono::system_clock::now()}; int64_t elapsed() { using namespace std::chrono; const auto end_time{system_clock::now()}; return duration_cast(end_time - start_time).count(); } __attribute__((constructor)) void setup() { using namespace std; ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); #ifdef _buffer_check atexit([] { char bufc; if (cin >> bufc) cerr << "\n\033[43m\033[30mwarning: buffer not empty.\033[0m\n\n"; }); #endif } unsigned cases(void), caseid = 1; template void main() { for (const unsigned total = cases(); caseid <= total; ++caseid) C(); } } // namespace config #line 2 "Library/option.hpp" #ifdef ONLINE_JUDGE #pragma GCC optimize("O3") #pragma GCC target("avx,avx2") #pragma GCC optimize("unroll-loops") #endif #line 5 "Library/utils/binary_search.hpp" namespace workspace { // binary search on discrete range. template std::enable_if_t< std::is_convertible_v, bool>, iter_type> binary_search(iter_type ok, iter_type ng, pred_type pred) { assert(ok != ng); __int128_t dist(ng - ok); while (dist > 1 || dist < -1) { iter_type mid(ok + dist / 2); if (pred(mid)) ok = mid, dist -= dist / 2; else ng = mid, dist /= 2; } return ok; } // parallel binary search on discrete range. template std::enable_if_t>, std::vector>, std::vector> binary_search(std::vector> ends, pred_type pred) { std::vector mids(ends.size()); for (;;) { bool all_found = true; for (size_t i{}; i != ends.size(); ++i) { auto [ok, ng] = ends[i]; iter_type mid(ok + (ng - ok) / 2); if (mids[i] != mid) { all_found = false; mids[i] = mid; } } if (all_found) break; auto res = pred(mids); for (size_t i{}; i != ends.size(); ++i) { (res[i] ? ends[i].first : ends[i].second) = mids[i]; } } return mids; } // binary search on real numbers. template std::enable_if_t< std::is_convertible_v, bool>, real_type> binary_search(real_type ok, real_type ng, const real_type eps, pred_type pred) { assert(ok != ng); while (ok + eps < ng || ng + eps < ok) { real_type mid{(ok + ng) / 2}; (pred(mid) ? ok : ng) = mid; } return ok; } // parallel binary search on real numbers. template std::enable_if_t>, std::vector>, std::vector> binary_search(std::vector> ends, const real_type eps, pred_type pred) { std::vector mids(ends.size()); for (;;) { bool all_found = true; for (size_t i{}; i != ends.size(); ++i) { auto [ok, ng] = ends[i]; if (ok + eps < ng || ng + eps < ok) { all_found = false; mids[i] = (ok + ng) / 2; } } if (all_found) break; auto res = pred(mids); for (size_t i{}; i != ends.size(); ++i) { (res[i] ? ends[i].first : ends[i].second) = mids[i]; } } return mids; } } // namespace workspace #line 3 "Library/utils/casefmt.hpp" namespace workspace { std::ostream &casefmt(std::ostream& os) { return os << "Case #" << config::caseid << ": "; } } // namespace workspace #line 3 "Library/utils/chval.hpp" namespace workspace { template > bool chle(T &x, const T &y, Comp comp = Comp()) { return comp(y, x) ? x = y, true : false; } template > bool chge(T &x, const T &y, Comp comp = Comp()) { return comp(x, y) ? x = y, true : false; } } // namespace workspace #line 4 "Library/utils/coordinate_compression.hpp" template class coordinate_compression { std::vector uniquely; std::vector compressed; public: coordinate_compression(const std::vector &raw) : uniquely(raw), compressed(raw.size()) { std::sort(uniquely.begin(), uniquely.end()); uniquely.erase(std::unique(uniquely.begin(), uniquely.end()), uniquely.end()); for(size_t i = 0; i != size(); ++i) compressed[i] = std::lower_bound(uniquely.begin(), uniquely.end(), raw[i]) - uniquely.begin(); } size_t operator[](const size_t idx) const { assert(idx < size()); return compressed[idx]; } size_t size() const { return compressed.size(); } size_t count() const { return uniquely.size(); } T value_of(const size_t ord) const { assert(ord < count()); return uniquely[ord]; } size_t order_of(const T &val) const { return std::lower_bound(uniquely.begin(), uniquely.end(), val) - uniquely.begin(); } std::vector::iterator begin() { return compressed.begin(); } std::vector::iterator end() { return compressed.end(); } std::vector::reverse_iterator rbegin() { return compressed.rbegin(); } std::vector::reverse_iterator rend() { return compressed.rend(); } }; #line 3 "Library/utils/fixed_point.hpp" namespace workspace { // specify the return type of lambda. template class fixed_point { lambda_type func; public: fixed_point(lambda_type &&f) : func(std::move(f)) {} template auto operator()(Args &&... args) const { return func(*this, std::forward(args)...); } }; } // namespace workspace #line 6 "Library/utils/hash.hpp" #line 3 "Library/utils/sfinae.hpp" #include template class trait> using enable_if_trait_type = typename std::enable_if::value>::type; template using element_type = typename std::decay()))>::type; template struct mapped_of { using type = element_type; }; template struct mapped_of::first_type> { using type = typename T::mapped_type; }; template using mapped_type = typename mapped_of::type; template struct is_integral_ext : std::false_type {}; template struct is_integral_ext< T, typename std::enable_if::value>::type> : std::true_type {}; template <> struct is_integral_ext<__int128_t> : std::true_type {}; template <> struct is_integral_ext<__uint128_t> : std::true_type {}; #if __cplusplus >= 201402 template constexpr static bool is_integral_ext_v = is_integral_ext::value; #endif template struct multiplicable_uint { using type = uint_least32_t; }; template struct multiplicable_uint::type> { using type = uint_least64_t; }; template struct multiplicable_uint::type> { using type = __uint128_t; }; #line 8 "Library/utils/hash.hpp" namespace workspace { template struct hash : std::hash {}; template struct hash> { size_t operator()(uint64_t x) const { static const uint64_t m = std::random_device{}(); x ^= x >> 23; x ^= m; x ^= x >> 47; return x - (x >> 32); } }; template size_t hash_combine(const size_t &seed, const Key &key) { return seed ^ (hash()(key) + 0x9e3779b9 /* + (seed << 6) + (seed >> 2) */); } template struct hash> { size_t operator()(const std::pair &pair) const { return hash_combine(hash()(pair.first), pair.second); } }; template class hash> { template ::value - 1> struct tuple_hash { static uint64_t apply(const Tuple &t) { return hash_combine(tuple_hash::apply(t), std::get(t)); } }; template struct tuple_hash { static uint64_t apply(const Tuple &t) { return 0; } }; public: uint64_t operator()(const std::tuple &t) const { return tuple_hash>::apply(t); } }; template struct hash_table_wrapper : hash_table { using key_type = typename hash_table::key_type; size_t count(const key_type &key) const { return hash_table::find(key) != hash_table::end(); } template auto emplace(Args &&... args) { return hash_table::insert(typename hash_table::value_type(args...)); } }; template using cc_hash_table = hash_table_wrapper<__gnu_pbds::cc_hash_table>>; template using gp_hash_table = hash_table_wrapper<__gnu_pbds::gp_hash_table>>; template using unordered_map = std::unordered_map>; template using unordered_set = std::unordered_set>; } // namespace workspace #line 3 "Library/utils/make_vector.hpp" namespace workspace { template constexpr auto make_vector(size_t* sizes, T const& init = T()) { if constexpr (N) return std::vector(*sizes, make_vector(std::next(sizes), init)); else return init; } template constexpr auto make_vector(const size_t (&sizes)[N], T const& init = T()) { return make_vector((size_t*)sizes, init); } } // namespace workspace #line 3 "Library/utils/random_number_generator.hpp" template class random_number_generator { template struct unif_t { std::uniform_int_distribution unif; unif_t(num_t lower, num_t upper) : unif(lower, upper) {} num_t operator()(std::mt19937 &engine) { return unif(engine); } }; template struct unif_t { std::uniform_real_distribution unif; unif_t(num_t lower, num_t upper) : unif(lower, upper) {} num_t operator()(std::mt19937 &engine) { return unif(engine); } }; unif_t::value> unif; std::mt19937 engine; public: // generate random number in [lower, upper]. random_number_generator(num_t lower = std::numeric_limits::min(), num_t upper = std::numeric_limits::max()) : unif(lower, upper), engine(std::random_device{}()) {} num_t operator()() { return unif(engine); } }; // class random_number_generator #line 3 "Library/utils/read.hpp" namespace workspace { // read with std::cin. template struct read { typename std::remove_const::type value; template read(types... args) : value(args...) { std::cin >> value; } operator T() const { return value; } }; template <> struct read { template operator T() const { T value; std::cin >> value; return value; } }; } // namespace workspace #line 4 "Library/utils/stream.hpp" #line 6 "Library/utils/stream.hpp" namespace std { template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second; } template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second; } template struct tuple_is { static istream &apply(istream &is, tuple_t &t) { tuple_is::apply(is, t); return is >> get(t); } }; template struct tuple_is { static istream &apply(istream &is, tuple_t &t) { return is; } }; template istream &operator>>(istream &is, tuple &t) { return tuple_is, tuple_size>::value - 1>::apply(is, t); } template struct tuple_os { static ostream &apply(ostream &os, const tuple_t &t) { tuple_os::apply(os, t); return os << ' ' << get(t); } }; template struct tuple_os { static ostream &apply(ostream &os, const tuple_t &t) { return os << get<0>(t); } }; template struct tuple_os { static ostream &apply(ostream &os, const tuple_t &t) { return os; } }; template ostream &operator<<(ostream &os, const tuple &t) { return tuple_os, tuple_size>::value - 1>::apply(os, t); } template > typename enable_if::type, string>::value && !is_same::type, char *>::value, istream &>::type operator>>(istream &is, Container &cont) { for (auto &&e : cont) is >> e; return is; } template > typename enable_if::type, string>::value && !is_same::type, char *>::value, ostream &>::type operator<<(ostream &os, const Container &cont) { bool head = true; for (auto &&e : cont) head ? head = 0 : (os << ' ', 0), os << e; return os; } } // namespace std #line 4 "Library/utils/trinary_search.hpp" // trinary search on discrete range. template iter_type trinary(iter_type first, iter_type last, comp_type comp) { assert(first < last); intmax_t dist(last - first); while(dist > 2) { iter_type left(first + dist / 3), right(first + dist * 2 / 3); if(comp(left, right)) last = right, dist = dist * 2 / 3; else first = left, dist -= dist / 3; } if(dist > 1 && comp(first + 1, first)) ++first; return first; } // trinary search on real numbers. template long double trinary(long double first, long double last, const long double eps, comp_type comp) { assert(first < last); while(last - first > eps) { long double left{(first * 2 + last) / 3}, right{(first + last * 2) / 3}; if(comp(left, right)) last = right; else first = left; } return first; } #line 2 "Library/utils/wrapper.hpp" template class reversed { Container &ref, copy; public: reversed(Container &ref) : ref(ref) {} reversed(Container &&ref = Container()) : ref(copy), copy(ref) {} auto begin() const { return ref.rbegin(); } auto end() const { return ref.rend(); } }; #line 7 "other/b.cpp" namespace workspace { struct solver; } // namespace workspace int main() { config::main(); } unsigned config::cases() { // return -1; // unspecified // int t; std::cin >> t; return t; // given return 1; } #line 4 "Library/data_structure/segment_tree/lazy.hpp" #line 6 "Library/data_structure/segment_tree/lazy.hpp" template class Container_tmpl = std::vector> class lazy_segment_tree { size_t size_orig, height, size_ext; Container_tmpl data; Container_tmpl lazy; static_assert(std::is_same::value, "\'Monoid\' has no proper binary operator+."); static_assert(std::is_same::value, "\'Endomorphism\' has no proper binary operator*."); static_assert( std::is_same::value, "\'Endomorphism\' is not applicable to \'Monoid\'."); void pull(const size_t &node) { data[node] = data[node << 1] + data[node << 1 | 1]; } void apply(const size_t &node, const Endomorphism &endo) { data[node] = data[node] * endo; if (node < size_ext) lazy[node] = lazy[node] * endo; } void push(const size_t &node) { if (node >= size_ext) return; apply(node << 1, lazy[node]); apply(node << 1 | 1, lazy[node]); lazy[node] = Endomorphism{}; } template size_t left_search_subtree(size_t node, Pred pred, Monoid mono) { assert(node); while (node < size_ext) { push(node); const Monoid &tmp = data[(node <<= 1) | 1] + mono; if (pred(tmp)) mono = tmp; else ++node; } return ++node -= size_ext; } template size_t right_search_subtree(size_t node, Pred pred, Monoid mono) { assert(node); while (node < size_ext) { push(node); const Monoid &tmp = mono + data[node <<= 1]; if (pred(tmp)) ++node, mono = tmp; } return (node -= size_ext) < size_orig ? node : size_orig; } public: lazy_segment_tree(const size_t n = 0) : size_orig{n}, height(n > 1 ? 32 - __builtin_clz(n - 1) : 0), size_ext{1u << height}, data(size_ext << 1), lazy(size_ext) {} lazy_segment_tree(const size_t &n, const Monoid &init) : lazy_segment_tree(n) { std::fill(std::next(std::begin(data), size_ext), std::end(data), init); for (size_t i{size_ext}; --i;) pull(i); } template ::value_type> lazy_segment_tree(iter_type first, iter_type last) : size_orig(std::distance(first, last)), height(size_orig > 1 ? 32 - __builtin_clz(size_orig - 1) : 0), size_ext{1u << height}, data(size_ext << 1), lazy(size_ext) { static_assert(std::is_constructible::value, "Monoid(iter_type::value_type) is not constructible."); for (auto iter{std::next(std::begin(data), size_ext)}; iter != std::end(data) && first != last; ++iter, ++first) *iter = Monoid(*first); for (size_t i{size_ext}; --i;) pull(i); } template lazy_segment_tree(const Container &cont) : lazy_segment_tree(std::begin(cont), std::end(cont)) {} size_t size() const { return size_orig; } size_t capacity() const { return size_ext; } Monoid operator[](const size_t &index) { return fold(index, index + 1); } void update(const size_t &index, const Endomorphism &endo) { update(index, index + 1, endo); } void update(size_t first, size_t last, const Endomorphism &endo) { assert(last <= size_orig); if (first >= last) return; first += size_ext, last += size_ext - 1; for (size_t i = height; i; --i) push(first >> i), push(last >> i); for (size_t l = first, r = last + 1; last; l >>= 1, r >>= 1) { if (l < r) { if (l & 1) apply(l++, endo); if (r & 1) apply(--r, endo); } if (first >>= 1, last >>= 1) { pull(first), pull(last); } } } Monoid fold() { return fold(0, size_orig); } Monoid fold(size_t first, size_t last) { assert(last <= size_orig); if (first >= last) return Monoid{}; first += size_ext, last += size_ext - 1; Monoid left_val{}, right_val{}; for (size_t l = first, r = last + 1; last; l >>= 1, r >>= 1) { if (l < r) { if (l & 1) left_val = left_val + data[l++]; if (r & 1) right_val = data[--r] + right_val; } if (first >>= 1, last >>= 1) { left_val = left_val * lazy[first]; right_val = right_val * lazy[last]; } } return left_val + right_val; } template size_t left_search(size_t right, Pred pred) { assert(right <= size_orig); right += size_ext - 1; for (size_t i{height}; i; --i) push(right >> i); ++right; Monoid mono{}; for (size_t left{size_ext}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const Monoid &tmp = data[--right] + mono; if (!pred(tmp)) return left_search_subtree(right, pred, mono); mono = tmp; } } return 0; } template size_t right_search(size_t left, Pred pred) { assert(left <= size_orig); left += size_ext; for (size_t i{height}; i; --i) push(left >> i); Monoid mono{}; for (size_t right{size_ext << 1}; left != right; left >>= 1, right >>= 1) { if ((left & 1) != (right & 1)) { const Monoid &tmp = mono + data[left]; if (!pred(tmp)) return right_search_subtree(left, pred, mono); mono = tmp; ++left; } } return size_orig; } }; // class lazy_segment_tree #line 18 "other/b.cpp" struct workspace::solver { solver() { // start here! const i64 inf = 1e18; struct endo { i64 value = 0; endo operator*(endo rhs) { return {value + rhs.value}; } }; struct mono { i64 value = inf; mono operator*(endo rhs) { return {value + rhs.value}; } mono operator+(mono rhs) { return {min(value, rhs.value)}; } }; int n; cin >> n; lazy_segment_tree laz; { vector init(n); for (auto i = 0; i < n; ++i) { i64 a; cin >> a; init[i] = {a}; } laz = init; } int q; cin >> q; while (q--) { int k, l, r, c; cin >> k >> l >> r >> c; --l; k--; if (k) { cout << laz.fold(l, r).value << eol; } else { laz.update(l, r, {c}); } } } };