#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } template struct Median { multiset x, y; Median() {} void add(T val) { x.insert(val); y.insert(*x.rbegin()); x.erase(x.find(*x.rbegin())); x.insert(*y.begin()); y.erase(y.begin()); if (x.size() > y.size() + 1) { y.insert(*x.rbegin()); x.erase(x.find(*x.rbegin())); } } // 存在は保証されている void del(T val) { if (x.end() != x.find(val)) { x.erase(x.find(val)); if (x.size() < y.size()) { x.insert(*y.begin()); y.erase(y.begin()); } } else if (y.end() != y.find(val)) { y.erase(y.find(val)); if (x.size() > y.size() + 1) { y.insert(*x.rbegin()); x.erase(x.find(*x.rbegin())); } } else { // error } } T get() { if (x.empty())return -1;//error return *x.rbegin(); } }; struct Mo { int width; vectorleft, right, order; vectorans; Median median; //vectorhorder; int n, q; Mo(int _n, int _q) :n(_n), q(_q) { width = (int)sqrt(_n); order.resize(q); iota(all(order), 0); ans.resize(q); } // add,query[l,r) void query(vector &l, vector &r) { left = l; right = r; //horder.resize(q); //rep(i, q) horder[i] = hilbertorder(left[i], right[i]); } // run void run(const vector&data) { sort(all(order), [&](int lh, int rh) { //return horder[lh] < horder[rh]; int lblock = left[lh] / width; int rblock = left[rh] / width; if (lblock != rblock)return lblock < rblock; if (lblock & 1) return right[lh] < right[rh]; return right[lh] > right[rh]; }); int nl = 0, nr = 0; auto add = [&](int idx) { median.add(data[idx]); }; auto del = [&](int idx) { median.del(data[idx]); }; auto ansset = [&](int idx) { ans[idx] = median.get(); }; for (auto idx : order) { while (nl > left[idx])add(--nl); while (nr < right[idx])add(nr++); while (nl < left[idx])del(nl++); while (nr > right[idx])del(--nr); ansset(idx); } } // TLEするときはこっちを使うと通るかも /*const int logn = 20; const int maxn = 1 << logn; long long hilbertorder(int x, int y) { long long d = 0; for (int s = 1 << logn - 1; s; s >>= 1) { bool rx = x & s, ry = y & s; d = d << 2 | rx * 3 ^ static_cast(ry); if (ry) continue; if (rx) { x = maxn - x; y = maxn - y; } swap(x, y); } return d; }*/ }; P op(P a, P b) { return { a.first + b.first,a.second + b.second }; } P e() { return { 0,0 }; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vectora(n); rep(i, n)cin >> a[i]; Mo mo(n, q); vectorl(q), r(q); rep(i, q) cin >> l[i] >> r[i], l[i]--; mo.query(l, r); mo.run(a); vector>eve; rep(i, n) eve.push_back({ a[i],{i, 0} }); rep(i, q) eve.push_back({ mo.ans[i],{i, 1} }); vectorans(q); sort(all(eve)); rep(_,2){ segtreeseg(n); rep(i, eve.size()) { int t = eve[i].second.second; int id = eve[i].second.first; int val = eve[i].first; if (0 == t) { seg.set(id, { val,1 }); } else { auto get = seg.prod(l[id], r[id]); long long val = get.second * mo.ans[id] - get.first; ans[id] += abs(val); } } reverse(all(eve)); } rep(i, q) cout << ans[i] << endl; return 0; }