競技プログラミング用の C++ ライブラリ
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <iostream>
#include <vector>
#include "../../../src/utility/vector_func.hpp"
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<long long> a(n);
for (auto& x : a) std::cin >> x;
// s[i] = a[0] + ... + a[i-1]
std::vector<long long> s = cumulative_sum(a);
while (q--) {
int l, r;
std::cin >> l >> r;
std::cout << s[r] - s[l] << '\n';
}
}#line 1 "test/yosupo/data_structure/static-range-sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/static_range_sum"
#include <iostream>
#include <vector>
#line 2 "src/utility/vector_func.hpp"
/**
* @file vector_func.hpp
* @brief vector に対する演算子と関数
*/
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <functional>
#include <numeric>
#include <type_traits>
#line 16 "src/utility/vector_func.hpp"
#line 2 "src/utility/traits.hpp"
/**
* @file traits.hpp
* @brief 型特性
*/
#line 10 "src/utility/traits.hpp"
#include <iterator>
#include <set>
#line 13 "src/utility/traits.hpp"
#include <utility>
#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif
namespace internal_traits {
using std::begin, std::end;
#if __cplusplus >= 202002L
template <class T>
struct is_range_impl : std::bool_constant < requires(T& t) {
begin(t);
end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif
// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
std::is_constructible_v<S, std::ranges::range_reference_t<R>>;
template <class R>
auto range_size(R& r) {
return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;
template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
: std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
} // namespace internal_traits
template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;
#if __cplusplus >= 202002L
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(); }; \
} \
template <class T> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, class U> \
concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
} \
template <class T, class U> \
struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
template <class T, class U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name) \
namespace internal_traits { \
template <class T, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T> \
struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
: std::true_type {}; \
} \
template <class T> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {}; \
template <class T> \
inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;
#define HAS_METHOD_ARG(func_name) \
namespace internal_traits { \
template <class T, typename U, class = void> \
struct has_##func_name##_impl : std::false_type {}; \
template <class T, typename U> \
struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
: std::true_type {}; \
} \
template <class T, typename U> \
struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {}; \
template <class T, typename U> \
inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif
HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)
#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))
namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
using is_transparent = void;
template <class T>
constexpr T&& operator()(T&& x) const noexcept {
return std::forward<T>(x);
}
};
#endif
} // namespace internal_traits
template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;
namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
} // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 18 "src/utility/vector_func.hpp"
/**
* @brief 値を proj で写して comp の順に安定ソートしたときの添字の並び (APL の grade up)
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> argsort(const std::vector<T>& v, F comp = F(), P proj = P()) {
std::vector<long long> ret(v.size());
std::iota(ret.begin(), ret.end(), 0LL);
std::stable_sort(ret.begin(), ret.end(), [&](long long i, long long j) {
return std::invoke(comp, std::invoke(proj, v[i]), std::invoke(proj, v[j]));
});
return ret;
}
/**
* @brief 各要素の順位 (proj で写して comp の順で v[i] より前に来る要素の個数)。同率は同じ順位で、次の順位は飛ばす
*/
template <typename T, typename F = std::less<>, typename P = internal_traits::identity>
std::vector<long long> rank(const std::vector<T>& v, F comp = F(), P proj = P()) {
const long long n = v.size();
std::vector<long long> p = argsort(v, comp, proj), ret(n);
for (long long i = 0; i < n; i++) {
ret[p[i]] = i > 0 && !std::invoke(comp, std::invoke(proj, v[p[i - 1]]), std::invoke(proj, v[p[i]])) ? ret[p[i - 1]] : i;
}
return ret;
}
/**
* @brief 逆置換 (ret[p[i]] = i)。p は 0, ..., n-1 の置換であること
*/
#if __cplusplus >= 202002L
template <typename I>
requires(std::integral<I>)
#else
template <typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<long long> inverse_permutation(const std::vector<I>& p) {
const long long n = p.size();
std::vector<long long> ret(n, -1);
for (long long i = 0; i < n; i++) {
const long long x = static_cast<long long>(p[i]);
assert(0 <= x && x < n && ret[x] == -1);
ret[x] = i;
}
return ret;
}
/**
* @brief {v[idx[0]], v[idx[1]], ...} (Uiua の select)
*/
#if __cplusplus >= 202002L
template <typename T, typename I>
requires(std::integral<I>)
#else
template <typename T, typename I, ENABLE_IF_T(std::is_integral_v<I>)>
#endif
std::vector<T> select(const std::vector<T>& v, const std::vector<I>& idx) {
std::vector<T> ret;
ret.reserve(idx.size());
for (I i : idx) {
assert(0 <= static_cast<long long>(i) && static_cast<long long>(i) < static_cast<long long>(v.size()));
ret.push_back(v[i]);
}
return ret;
}
template <typename T>
inline std::vector<T>& operator++(std::vector<T>& v) {
for (auto& e : v) e++;
return v;
}
template <typename T>
inline std::vector<T> operator++(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e++;
return res;
}
template <typename T>
inline std::vector<T>& operator--(std::vector<T>& v) {
for (auto& e : v) e--;
return v;
}
template <typename T>
inline std::vector<T> operator--(std::vector<T>& v, int) {
auto res = v;
for (auto& e : v) e--;
return res;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] += v2[i];
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res += v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator+=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e += u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator+(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res += u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v1, const std::vector<U>& v2) {
if (v2.size() > v1.size()) {
v1.resize(v2.size());
}
for (std::size_t i = 0; i < v2.size(); i++) {
v1[i] *= v2[i];
}
for (std::size_t i = v2.size(); i < v1.size(); i++) {
v1[i] *= U(0);
}
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v1, const std::vector<U>& v2) {
std::vector<T> res(v1);
return res *= v2;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator*=(std::vector<T>& v, const U& u) {
for (T& e : v) {
e *= u;
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res *= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T> operator*(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res *= u;
}
template <typename T, typename U>
inline std::vector<T>& assign(std::vector<T>& v1, const std::vector<U>& v2) {
v1.assign(v2.begin(), v2.end());
return v1;
}
template <typename T, typename U>
inline std::vector<T>& extend(std::vector<T>& v1, const std::vector<U>& v2) {
v1.insert(v1.end(), v2.begin(), v2.end());
return v1;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::convertible_to<U, T>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_convertible_v<U, T>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v1, const std::vector<U>& v2) {
return extend(v1, v2);
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T>& operator|=(std::vector<T>& v, const U& u) {
if constexpr (std::is_signed_v<U>) assert(u >= 0);
std::vector<T> w(v);
v.clear();
for (long long i = 0; i < static_cast<long long>(u); i++) {
extend(v, w);
}
return v;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const std::vector<T>& v, const U& u) {
std::vector<T> res(v);
return res |= u;
}
#if __cplusplus >= 202002L
template <typename T, typename U>
requires(std::integral<U>)
#else
template <typename T, typename U, ENABLE_IF_T(std::is_integral_v<U>)>
#endif
inline std::vector<T> operator|(const U& u, const std::vector<T>& v) {
std::vector<T> res(v);
return res |= u;
}
template <typename T>
inline std::vector<T> abs(const std::vector<T>& v) {
std::vector<T> ret;
ret.reserve(v.size());
for (const T& e : v) ret.push_back(std::abs(e));
return ret;
}
template <typename T>
std::vector<T> cumulative_sum(std::vector<T> v) {
v.insert(v.begin(), T{});
std::vector<T> ret(v.size());
std::partial_sum(v.begin(), v.end(), ret.begin());
return ret;
}
#if __cplusplus >= 202002L
template <typename T>
requires(std::integral<T>)
#else
template <typename T, ENABLE_IF_T(std::is_integral_v<T>)>
#endif
std::vector<T> iota(T n) {
assert(n >= 0);
std::vector<T> ret(n);
std::iota(ret.begin(), ret.end(), 0);
return ret;
}
template <typename T>
std::vector<T> unique(const std::vector<T>& v) {
std::vector<T> res(v);
std::sort(res.begin(), res.end());
res.erase(std::unique(res.begin(), res.end()), res.end());
return res;
}
inline long long radix_convert(const std::vector<long long>& v, int base = 10) {
assert(base >= 2);
long long res = 0;
for (long long d : v) {
assert(0 <= d && d < base);
res = res * base + d;
}
return res;
}
inline std::vector<long long> radix_convert(long long v, int base = 10) {
assert(v >= 0 && base >= 2);
std::vector<long long> res;
while (v > 0) {
res.push_back(v % base);
v /= base;
}
std::reverse(res.begin(), res.end());
return res;
}
#line 7 "test/yosupo/data_structure/static-range-sum.test.cpp"
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
std::cin >> n >> q;
std::vector<long long> a(n);
for (auto& x : a) std::cin >> x;
// s[i] = a[0] + ... + a[i-1]
std::vector<long long> s = cumulative_sum(a);
while (q--) {
int l, r;
std::cin >> l >> r;
std::cout << s[r] - s[l] << '\n';
}
}