#include #include #include using namespace std; #include #include #include template struct segtree{ using F=function; F calcfn,updatefn; int n; T defvalue; vectordat; segtree(int n_=0,T defvalue_=numeric_limits::max(), const F calcfn_=[](T a,T b){return a&v) { for(int i=0;i=0;i--)dat[i]=calcfn(dat[i*2+1],dat[i*2+2]); } void update(int i,T a) { i+=n-1; dat[i]=updatefn(dat[i],a); while(i>0) { i=(i-1)/2; dat[i]=calcfn(dat[2*i+1],dat[2*i+2]); } } T query(int a,int b)//[a,b) { int L=(a<0?0:a>n?n:a)+n-1; int R=(b<0?0:b>n?n:b)+n-1; T lret=defvalue,rret=defvalue; for(;L>=1,R>>=1) { if(!(L&1))lret=calcfn(lret,dat[L]); if(!(R&1))rret=calcfn(dat[--R],rret); } return calcfn(lret,rret); } }; int N,Q; int A[2<<17]; int L[2<<17],R[2<<17]; int l[2<<17],r[2<<17]; long ans[2<<17]; main() { scanf("%d%d",&N,&Q); for(int i=0;i >Ai(N); for(int i=0;i >Qi(Ai.begin(),Ai.end()); sort(Ai.begin(),Ai.end()); for(int i=0;i >T(Q); for(int i=0;icnt(N,0,[](int a,int b){return a+b;},[](int a,int b){return b;}); int id=0; for(int i=0;iT[id].first) { int idd=T[id].second; if(cnt.query(L[idd],R[idd])>=(R[idd]-L[idd]+1)/2)r[idd]=T[id].first; else l[idd]=T[id].first; id++; } cnt.update(~Ai[i].second,1); } while(id=(R[idd]-L[idd])/2)r[idd]=T[id].first; else l[idd]=T[id].first; id++; } } for(int i=0;iSUM(N,0,[](long a,long b){return a+b;},[](long a,long b){return b;}); SUM.copy(vector(A,A+N)); segtreeneg(N,0,[](int a,int b){return a+b;},[](int a,int b){return b;}); for(pairp:Qi) { int id=p.second; int X=p.first; if(id<0) { id=~id; SUM.update(id,-X); neg.update(id,1); } else { int w=R[id]-L[id]; long now=SUM.query(L[id],R[id]); int cnt=neg.query(L[id],R[id]); now+=(long)X*cnt-(long)X*(w-cnt); ans[id]=now; } } for(int i=0;i