#pragma GCC optimize ("Ofast") #include using namespace std; void *wmem; char memarr[96000000]; template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){ int i; pair > *arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second.first = b[i]; arr[i].second.second = c[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second.first; c[i] = arr[i].second.second; } } template void sortA_L(int N, T1 a[], T2 b[], T3 c[], T4 d[], void *mem = wmem){ int i; pair, pair > *arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first.first = a[i]; arr[i].first.second = b[i]; arr[i].second.first = c[i]; arr[i].second.second = d[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first.first; b[i] = arr[i].first.second; c[i] = arr[i].second.first; d[i] = arr[i].second.second; } } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void wt_L(char a){ putchar_unlocked(a); } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } template inline T popFirst(multiset &a){ T res = *(a.begin()); a.erase(a.begin()); return res; } template inline T getFirst(multiset &a){ return *(a.begin()); } template inline T popLast(multiset &a){ T res; typename multiset::iterator it; it = a.end(); it--; res = *it; a.erase(it); return res; } template inline T getLast(multiset &a){ typename multiset::iterator it; it = a.end(); it--; return *it; } template inline T popFirst(set &a){ T res = *(a.begin()); a.erase(a.begin()); return res; } template inline T getFirst(set &a){ return *(a.begin()); } template inline T popLast(set &a){ T res; typename set::iterator it; it = a.end(); it--; res = *it; a.erase(it); return res; } template inline T getLast(set &a){ typename set::iterator it; it = a.end(); it--; return *it; } int N; int Q; int A[200000]; int L[200000]; int R[200000]; int lbox[200000]; int ind[200000]; long long res[200000]; multiset h1; multiset h2; long long s1; long long s2; inline long long calc(void){ long long m; if(h1.size() > h2.size()){ m = getLast(h1); } else{ m = getFirst(h2); } return (long long) h1.size() * m - s1 + s2 - (long long) h2.size() * m; } void addval(int a){ int x; int y; if(h1.size() > h2.size()){ h2.insert(a); s2 += a; } else{ h1.insert(a); s1 += a; } if(h1.size() && h2.size() && getLast(h1) > getFirst(h2)){ x = popLast(h1); y = popFirst(h2); h1.insert(y); h2.insert(x); s1 += y - x; s2 += x - y; } } void delval(int a){ int x; multiset::iterator it; it = h1.find(a); if(it != h1.end()){ h1.erase(it); s1 -= a; } else{ it = h2.find(a); h2.erase(it); s2 -= a; } if(h2.size() > h1.size()+1){ x = popFirst(h2); h1.insert(x); s1 += x; s2 -= x; } if(h1.size() > h2.size()+1){ x = popLast(h1); h2.insert(x); s1 -= x; s2 += x; } } int main(){ int i; wmem = memarr; int x; int y; long long tmp; rd(N); rd(Q); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } { int e98WHCEY; for(e98WHCEY=(0);e98WHCEY<(Q);e98WHCEY++){ rd(L[e98WHCEY]);L[e98WHCEY] += (-1); rd(R[e98WHCEY]); } } for(i=(0);i<(Q);i++){ lbox[i] = L[i] / 500; } for(i=(0);i<(Q);i++){ ind[i] = i; } sortA_L(Q, lbox, R, L, ind); x = y = 0; for(i=(0);i<(Q);i++){ while(y < R[i]){ addval(A[y++]); } while(L[i] < x){ addval(A[--x]); } while(y > R[i]){ delval(A[--y]); } while(L[i] > x){ delval(A[x++]); } res[ind[i]] = calc(); } { int ZIeRIny5; for(ZIeRIny5=(0);ZIeRIny5<(Q);ZIeRIny5++){ wt_L(res[ZIeRIny5]); wt_L('\n'); } } return 0; } // cLay varsion 20191108-1 // --- original code --- // int N, Q, A[2d5]; // int L[2d5], R[2d5]; // int lbox[2d5], ind[2d5]; // ll res[2d5]; // // multiset h1, h2; // ll s1, s2; // // inline ll calc(void){ // ll m; // if(h1.size() > h2.size()) m = getLast(h1); else m = getFirst(h2); // return (ll) h1.size() * m - s1 + s2 - (ll) h2.size() * m; // } // // void addval(int a){ // int x, y; // if(h1.size() > h2.size()){ // h2.insert(a); // s2 += a; // } else { // h1.insert(a); // s1 += a; // } // if(h1.size() && h2.size() && getLast(h1) > getFirst(h2)){ // x = popLast(h1); // y = popFirst(h2); // h1.insert(y); // h2.insert(x); // s1 += y - x; // s2 += x - y; // } // } // // void delval(int a){ // int x; // multiset::iterator it; // // it = h1.find(a); // if(it != h1.end()){ // h1.erase(it); // s1 -= a; // } else { // it = h2.find(a); // h2.erase(it); // s2 -= a; // } // if(h2.size() > h1.size()+1){ // x = popFirst(h2); // h1.insert(x); // s1 += x; // s2 -= x; // } // if(h1.size() > h2.size()+1){ // x = popLast(h1); // h2.insert(x); // s1 -= x; // s2 += x; // } // } // // { // int x, y; // ll tmp; // rd(N,Q,A(N),(L--,R)(Q)); // // rep(i,Q) lbox[i] = L[i] / 500; // rep(i,Q) ind[i] = i; // sortA(Q, lbox, R, L, ind); // // x = y = 0; // rep(i,Q){ // while(y < R[i]) addval(A[y++]); // while(L[i] < x) addval(A[--x]); // while(y > R[i]) delval(A[--y]); // while(L[i] > x) delval(A[x++]); // res[ind[i]] = calc(); // } // // wtLn(res(Q)); // // }