#include using namespace std::literals::string_literals; using i64 = long long; using std::cout; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } template class segment_tree { using value_type = Monoid; using size_type = size_t; using binary_function = std::function; using checker = std::function; const size_type size_; size_type height_; const value_type id; const binary_function bi_func; std::vector data; private: const size_type get_height(const size_type& size) const { size_type height = 1; while(1 << height < size) height++; return height; } const size_type base_size() const { return 1 << height_; } void meld(const size_type& index) { data[index] = bi_func(data[index << 1 ^ 0], data[index << 1 ^ 1]); } public: segment_tree(const size_type& size, const binary_function& bi_func, const value_type& id) : size_(size), bi_func(bi_func), id(id) { height_ = get_height(size); data.assign(base_size() << 1, id); } value_type fold(size_type left, size_type right) { value_type l_value = id, r_value = id; for(left += base_size(), right += base_size(); left < right; left >>= 1, right >>= 1) { if(left & 1) l_value = bi_func(l_value, data[left++]); if(right & 1) r_value = bi_func(data[--right], r_value); } return bi_func(std::move(l_value), std::move(r_value)); } void update(size_type index, const value_type& value) { index += base_size(); data[index] = bi_func(data[index], value); while(index >>= 1) meld(index); } void change(size_type index, const value_type& value) { index += base_size(); data[index] = value; while(index >>= 1) meld(index); } const size_type search(const size_type & left, const checker & check) { value_type val = id; size_t base_size_ = base_size(); auto find = [&](auto&& find, size_type k, size_type l, size_type r) -> int { if(l + 1 == r) { val = bi_func(val, data[k]); return (check(val) ? k - base_size_ : -1); } const size_type mid = (l + r) >> 1; if(mid <= left) return find(find, k << 1 ^ 1, mid, r); if(left <= l and !check(bi_func(val, data[k]))) { val = bi_func(val, data[k]); return -1; } const int left_ret = find(find, k << 1 ^ 0, l, mid); if(left_ret == -1) return find(find, k << 1 ^ 1, mid, r); return left_ret; }; return find(find, 1, 0, base_size_); } value_type operator[](const size_type& index) const { return data[index + base_size()]; } const size_type size() const { return size_; } const bool empty() const { return data.empty(); } }; int main() { // input int n, q; scanf("%d%d", &n, &q); assert(1 <= n and n <= 1e5); assert(1 <= q and q <= 1e5); std::vector a(n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); assert(-1e9 <= a[i] and a[i] <= 1e9); } std::vector L(q), R(q); for(int i = 0; i < q; i++) { scanf("%d%d", &L[i], &R[i]); assert(1 <= L[i] and L[i] <= n); assert(1 <= R[i] and R[i] <= n); assert(L[i] <= R[i]); L[i]--; } // solve std::vector> vec; for(int i = 0; i < n; i++) vec.push_back({a[i], i}); sort(begin(vec), end(vec)); std::vector center = [&]{ using P = std::pair; std::vector Left(q, 0), Right(q, n); std::vector> query, nxt; for(int i = 0; i < q; i++) query.push_back({(Left[i] + Right[i]) >> 1, i}); segment_tree seg(n, [](int a, int b) { return a + b; }, 0); while(!query.empty()) { sort(begin(query), end(query)); int cur = 0; for(int i = 0; i < n; i++) { seg.update(vec[i].second, 1); if(i + 1 != n - 1 and vec[i].first == vec[i + 1].first) continue; while(cur < query.size() and query[cur].first <= i) { auto & p = query[cur++]; int id = p.second, mid = p.first; int len = (R[id] - L[id] + 1) >> 1; int val = seg.fold(L[id], R[id]); if(len <= val) Right[id] = mid; else Left[id] = mid; if(Left[id] + 1 != Right[id]) nxt.push_back({(Left[id] + Right[id]) >> 1, id}); } } for(int i = 0; i < n; i++) seg.change(i, 0); query.swap(nxt); nxt.clear(); } std::vector center(q); for(int i = 0; i < q; i++) center[i] = vec[Right[i]].first; return center; }(); std::vector ans = [&]{ std::vector ans(q); for(int loop = 0; loop < 2; loop++) { segment_tree seg(n, [](i64 a, i64 b) { return a + b; }, 0); segment_tree cnt(n, [](i64 a, i64 b) { return a + b; }, 0); std::vector> query; for(int i = 0; i < q; i++) query.push_back({center[i], i}); sort(begin(query), end(query)); if(loop) reverse(begin(query), end(query)); int cur = 0; for(int i = 0; i < n; i++) { seg.update(vec[i].second, vec[i].first); cnt.update(vec[i].second, 1); while(cur < query.size() and query[cur].first == vec[i].first) { auto & p = query[cur++]; i64 id = p.second, val = p.first; i64 tmp = val * cnt.fold(L[id], R[id]) - seg.fold(L[id], R[id]); ans[id] += tmp * (loop ? -1 : 1); } } reverse(begin(vec), end(vec)); } return ans; }(); for(auto v: ans) printf("%lld\n", v); return 0; }