#pragma GCC optimize("Ofast") #include using namespace::std; using lint=long long; #define rep(i, n) for(lint i = 0; i < (lint)(n); i++) __attribute__((constructor)) void init(){ cin.tie(0); ios::sync_with_stdio(false); cout< class persistent_segment_tree{ struct node; using np=node*; using lint=long long; struct node{ lint val;int cnt=0; np ch[2]={nullptr,nullptr}; node(){} node(T val):val(val){} node(np n):val(n->val),cnt(n->cnt){ch[0]=n->ch[0];ch[1]=n->ch[1];} }; lint sz=1,n; int count(np t){return t?t->cnt:0;} vector>p; vectorx_list; vectorroot; bool is_builded=0; T e; functionf,g; public: persistent_segment_tree(lint n,T e,functionf,functiong):n(n),e(e),f(f),g(g){ while(sz(p[i]); root[i+1]=push_back(get<1>(p[i]),get<2>(p[i]),root[i],-sz,sz); } } inline T get_fold(lint lx,lint rx,lint ly,lint ry){ return g(get_fold(ly,ry,root[lower_bound(x_list.begin(),x_list.end(),rx)-x_list.begin()],-sz,sz),get_fold(ly,ry,root[lower_bound(x_list.begin(),x_list.end(),lx)-x_list.begin()],-sz,sz)); } inline lint get_count(lint lx,lint rx,lint ly,lint ry){ return get_count(ly,ry,root[lower_bound(x_list.begin(),x_list.end(),rx)-x_list.begin()],-sz,sz)-get_count(ly,ry,root[lower_bound(x_list.begin(),x_list.end(),lx)-x_list.begin()],-sz,sz); } inline lint kth_element(lint lx,lint rx,lint k){ return kth_element(root[lower_bound(x_list.begin(),x_list.end(),rx)-x_list.begin()],root[lower_bound(x_list.begin(),x_list.end(),lx)-x_list.begin()],k,-sz,sz); } private: np push_back(lint pos,T val,np t,lint l,lint r){ if(l<=pos&&posval=f(res->val,val); res->cnt++; if(r-l>1){ res->ch[0]=push_back(pos,val,res->ch[0],l,(l+r)/2); res->ch[1]=push_back(pos,val,res->ch[1],(l+r)/2,r); } return res; }else{ return t; } } lint get_count(lint a,lint b,np t,lint l,lint r){ if(!t||r<=a||b<=l)return 0; if(a<=l&&r<=b){return t->cnt;} return f(get_count(a,b,t->ch[0],l,(l+r)/2),get_count(a,b,t->ch[1],(l+r)/2,r)); } T get_fold(lint a,lint b,np t,lint l,lint r){ if(!t||r<=a||b<=l)return e; if(a<=l&&r<=b){return t->val;} return f(get_fold(a,b,t->ch[0],l,(l+r)/2),get_fold(a,b,t->ch[1],(l+r)/2,r)); } lint kth_element(np s,np t,lint k,lint l,lint r){ if(r-l==1)return l; if(!s)s=new node(e); if(!t)t=new node(e); lint cnt=count(s->ch[0])-count(t->ch[0]); if(cnt>k)return kth_element(s->ch[0],t->ch[0],k,l,(l+r)/2); else return kth_element(s->ch[1],t->ch[1],k-cnt,(l+r)/2,r); } }; int main(){ lint n,q; cin>>n>>q; vector a(n); rep(i,n)cin>>a[i]; persistent_segment_treev(1LL<<30,0,plus(),minus()); rep(i,n)v.push_back(i,a[i],a[i]); v.build(); rep(i,q){ lint s,t; cin>>s>>t; s--; lint mid=v.kth_element(s,t,(t-s)/2); lint a=v.get_fold(s,t,mid,1LL<<30)-mid*v.get_count(s,t,mid,1LL<<30); lint b=-v.get_fold(s,t,-1LL<<30,mid)+mid*v.get_count(s,t,-1LL<<30,mid); cout<