ull xs[2d5]; ll ys[2d5]; ll ls[2d5],rs[2d5]; ll d(ll i,ll j){ return abs(ls[ys[j]]-ls[ys[i]])+abs(rs[ys[j]]-rs[ys[i]]); } void opt1(ll b){ rep(i,2d5-1-b){ if(d(i,i+1)+d(i+b,i+b+1)>d(i,i+b)+d(i+1,i+b+1)){ reverse(ys+(i+1),ys+(i+1+b)); } } } { Timer timer; timer.set(); ll@n,@q,@wt,@st,w[n+1]; w[0]=0; rep(i,n){ ll@wi; w[i+1]=w[i]+wi; } rep(i,q){ ll@l,@r; ls[i]=l=w[l-1]; rs[i]=r=w[r]; ll m=w[n]+1>>1; ull x=0; rep(32){ x<<=2; if(r=m){ x|=3; (l,r)=(l-m,r-m); }else if(l+r<2m){ x|=1; (l,r)=(r-m,m-1-l); }else{ x|=2; (l,r)=(2m-1-r,l); } l*=2; r*=2; } xs[i]=x; ys[i]=i; } sortA(q,xs,ys); rrep(i,4096){ long k=i%512+2; opt1(k); if(timer.get()>=4.95){ break; } } rep(i,q){ ys[i]+=1; } wt(ys(q)); }