#include #include #include #include namespace atcoder { namespace internal { #ifndef _MSC_VER template using is_signed_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using is_unsigned_int128 = typename std::conditional::value || std::is_same::value, std::true_type, std::false_type>::type; template using make_unsigned_int128 = typename std::conditional::value, __uint128_t, unsigned __int128>; template using is_integral = typename std::conditional::value || is_signed_int128::value || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using is_signed_int = typename std::conditional<(is_integral::value && std::is_signed::value) || is_signed_int128::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional<(is_integral::value && std::is_unsigned::value) || is_unsigned_int128::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional< is_signed_int128::value, make_unsigned_int128, typename std::conditional::value, std::make_unsigned, std::common_type>::type>::type; #else template using is_integral = typename std::is_integral; template using is_signed_int = typename std::conditional::value && std::is_signed::value, std::true_type, std::false_type>::type; template using is_unsigned_int = typename std::conditional::value && std::is_unsigned::value, std::true_type, std::false_type>::type; template using to_unsigned = typename std::conditional::value, std::make_unsigned, std::common_type>::type; #endif template using is_signed_int_t = std::enable_if_t::value>; template using is_unsigned_int_t = std::enable_if_t::value>; template using to_unsigned_t = typename to_unsigned::type; } // namespace internal } // namespace atcoder #include #include namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template struct fenwick_tree { using U = internal::to_unsigned_t; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder #define REP_(i, a_, b_, a, b, ...) \ for (int i = (a), _Z_##i = (b); i < _Z_##i; ++i) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) #define ALL(x) std::begin(x), std::end(x) using i64 = long long; using u64 = unsigned long long; template inline bool chmax(T &a, U b) { return a < b and ((a = std::move(b)), true); } template inline bool chmin(T &a, U b) { return a > b and ((a = std::move(b)), true); } template inline int ssize(const T &a) { return (int)std::size(a); } template std::istream &operator>>(std::istream &is, std::vector &a) { for (auto &x : a) is >> x; return is; } template std::ostream &print_seq(const Container &a, std::string_view sep = " ", std::string_view ends = "\n", std::ostream *os = nullptr) { if (os == nullptr) os = &std::cout; auto b = std::begin(a), e = std::end(a); for (auto it = std::begin(a); it != e; ++it) { if (it != b) *os << sep; *os << *it; } return *os << ends; } template struct is_iterable : std::false_type {}; template struct is_iterable())), decltype(std::end(std::declval()))>> : std::true_type {}; template ::value && !std::is_same::value && !std::is_same::value>> std::ostream &operator<<(std::ostream &os, const T &a) { return print_seq(a, ", ", "", &(os << "{")) << "}"; } template std::ostream &operator<<(std::ostream &os, const std::pair &a) { return os << "(" << a.first << ", " << a.second << ")"; } #ifdef ENABLE_DEBUG template void pdebug(const T &value) { std::cerr << value; } template void pdebug(const T &value, const Ts &...args) { pdebug(value); std::cerr << ", "; pdebug(args...); } #define DEBUG(...) \ do { \ std::cerr << " \033[33m (L" << __LINE__ << ") "; \ std::cerr << #__VA_ARGS__ << ":\033[0m "; \ pdebug(__VA_ARGS__); \ std::cerr << std::endl; \ } while (0) #else #define pdebug(...) #define DEBUG(...) #endif using namespace std; const int INF = 1e9 + 100; template struct Compress { std::vector vec; explicit Compress(std::vector v) : vec(v) { std::sort(vec.begin(), vec.end()); vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); } int size() const { return vec.size(); } int index(T x) const { return lower_bound(vec.begin(), vec.end(), x) - vec.begin(); } const T &value(int i) const { return vec[i]; } }; int main() { std::ios::sync_with_stdio(false), cin.tie(nullptr); int n, q; cin >> n >> q; vector a(n); cin >> a; Compress comp(a); vector> queries(q); REP(qi, q) { int l, r; cin >> l >> r; --l; queries[qi] = {l, r}; } vector tv(q, 0), fv(q, comp.size()); vector> left(n + 1), right(n + 1); vector pc(q); for (int iter = 0;; ++iter) { REP(i, n + 1) { left[i].clear(); right[i].clear(); } bool done = true; REP(i, q) { if (fv[i] - tv[i] > 1) { auto [l, r] = queries[i]; left[l].push_back(i); right[r].push_back(i); done = false; } } if (done) break; atcoder::fenwick_tree bit_c(comp.size()); fill(ALL(pc), 0); REP(i, n + 1) { for (int j : left[i]) { int mid = (tv[j] + fv[j]) / 2; pc[j] = bit_c.sum(0, mid); } for (int j : right[i]) { int mid = (tv[j] + fv[j]) / 2; i64 after_c = bit_c.sum(0, mid); int smaller_c = after_c - pc[j]; auto [l, r] = queries[j]; int length = r - l; if (smaller_c <= length / 2) { tv[j] = mid; } else { fv[j] = mid; } } if (i < n) { int aix = comp.index(a[i]); bit_c.add(aix, 1); } } } // Compute f. { vector ans(q); vector ps(q), pa(q); REP(i, n + 1) { left[i].clear(); right[i].clear(); } REP(i, q) { auto [l, r] = queries[i]; left[l].push_back(i); right[r].push_back(i); } atcoder::fenwick_tree bit_c(comp.size()); atcoder::fenwick_tree bit_s(comp.size()); fill(ALL(pc), 0); fill(ALL(ps), 0LL); fill(ALL(pa), 0LL); i64 all_sum = 0; REP(i, n + 1) { for (int j : left[i]) { int med = tv[j]; pc[j] = bit_c.sum(0, med); ps[j] = bit_s.sum(0, med); pa[j] = all_sum; } for (int j : right[i]) { int med = tv[j]; i64 after_c = bit_c.sum(0, med); i64 after_s = bit_s.sum(0, med); int smaller_c = after_c - pc[j]; auto [l, r] = queries[j]; i64 smaller_s = after_s - ps[j]; i64 val = comp.value(med); ans[j] = smaller_c * val - smaller_s; ans[j] += (all_sum - pa[j] - smaller_s) - (r - l - smaller_c) * val; } if (i < n) { int aix = comp.index(a[i]); bit_c.add(aix, 1); bit_s.add(aix, a[i]); all_sum += a[i]; } } REP(qi, q) { cout << ans[qi] << '\n'; } } }