#include #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 SegTree { using T = typename Monoid::T; inline int n() const { return n_; } inline int offset() const { return offset_; } explicit SegTree(int n) : n_(n) { offset_ = 1; while (offset_ < n_) offset_ <<= 1; data_.assign(2 * offset_, Monoid::id()); } explicit SegTree(const std::vector &leaves) : n_(leaves.size()) { offset_ = 1; while (offset_ < n_) offset_ <<= 1; data_.assign(2 * offset_, Monoid::id()); for (int i = 0; i < n_; ++i) { data_[offset_ + i] = leaves[i]; } for (int i = offset_ - 1; i > 0; --i) { data_[i] = Monoid::op(data_[i * 2], data_[i * 2 + 1]); } } // Sets i-th value (0-indexed) to x. void set(int i, const T &x) { int k = offset_ + i; data_[k] = x; // Update its ancestors. while (k > 1) { k >>= 1; data_[k] = Monoid::op(data_[k * 2], data_[k * 2 + 1]); } } // Queries by [l,r) range (0-indexed, half-open interval). T fold(int l, int r) const { l = std::max(l, 0) + offset_; r = std::min(r, offset_) + offset_; T vleft = Monoid::id(), vright = Monoid::id(); for (; l < r; l >>= 1, r >>= 1) { if (l & 1) vleft = Monoid::op(vleft, data_[l++]); if (r & 1) vright = Monoid::op(data_[--r], vright); } return Monoid::op(vleft, vright); } T fold_all() const { return data_[1]; } // Returns i-th value (0-indexed). T operator[](int i) const { return data_[offset_ + i]; } friend std::ostream &operator<<(std::ostream &os, const SegTree &st) { os << "["; for (int i = 0; i < st.n(); ++i) { if (i != 0) os << ", "; const auto &x = st[i]; os << x; } return os << "]"; } private: int n_; // number of valid leaves. int offset_; // where leaves start std::vector data_; // data size: 2*offset_ }; struct SumCount { struct T { long long sum; int count; }; static T op(const T &x, const T &y) { return {x.sum + y.sum, x.count + y.count}; } static constexpr T id() { return {0, 0}; } }; 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 ans(q); vector> left(n + 1), right(n + 1); vector pc(q); vector ps(q), pa(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; SegTree seg(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 mid = (tv[j] + fv[j]) / 2; auto v = seg.fold(0, mid); pc[j] = v.count; ps[j] = v.sum; pa[j] = all_sum; } for (int j : right[i]) { int mid = (tv[j] + fv[j]) / 2; auto v = seg.fold(0, mid); i64 after_c = v.count; i64 after_s = v.sum; int smaller_c = after_c - pc[j]; auto [l, r] = queries[j]; int length = r - l; if (smaller_c <= length / 2) { i64 smaller_s = after_s - ps[j]; tv[j] = mid; i64 val = comp.value(mid); ans[j] = smaller_c * val - smaller_s; ans[j] += (all_sum - pa[j] - smaller_s) - (r - l - smaller_c) * val; } else { fv[j] = mid; } } if (i < n) { int aix = comp.index(a[i]); auto v = seg[aix]; v.count++; v.sum += a[i]; seg.set(aix, v); all_sum += a[i]; } } } REP(qi, q) { cout << ans[qi] << '\n'; } }