The details are much too big.
Multiply.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<Set>#include<cmath>#include<cstdlib>#defineMAXV 200500#defineMAXN 100500#defineEPS 1e-7#defineINF 0X7F7F7F7F7F7F7F7FLLusing namespacestd;Long Longn,m,h[maxn],x,y,aft[maxn][3],anc[maxv][ -],dis1[maxv][ -],dis2[maxv][ -],HASHED[MAXN],POS[MAXN];Long Longret1=0, ret2=0, ans;structpnt{Long LongRet,dot,rets;} p[5];Set<Long Long>s;Set<Long Long>:: iterator it;Doubleans1=inf;BOOLcmp (PNT x,pnt y) {if(X.ret!=y.ret)returnx.ret<Y.ret; returnX.rets>y.rets;}voiddivide () { for(Long LongI=1; i<=n;i++) hashed[i]=H[i]; Sort (Hashed+1, hashed+n+1); for(Long LongI=1; i<=n;i++) {H[i]=lower_bound (hashed+1, hashed+n+1, H[i])-hashed; Pos[h[i]]=i; }}Long LongAskLong LongXLong Longy) {Ret1=0; ret2=0; for(Long LongE= +; e>=0; e--) { if((ret1+ret2+dis1[x][e]+dis2[x][e]<=y) &&(Anc[x][e])) {Ret1+=dis1[x][e];ret2+=Dis2[x][e]; X=Anc[x][e]; } }}voidUpdateLong Longx) { if(!RET1)return; if((Double) {ans1= (ret2/ret1<ans1) {Double) ret2/ret1;ans=x;} Else if(((Double) ret2/ret1-ans1<=eps) && (hashed[h[ans]]x;}intMain () {scanf ("%lld",&N); for(Long LongI=1; i<=n;i++) scanf ("%lld",&H[i]); Divide (); for(Long Longi=n;i>=1; i--) { for(Long Longj=1; j<=4; j + +) p[j].dot=p[j].ret=inf; It=S.lower_bound (H[i]); if(it!=S.end ()) {p[3].ret=hashed[h[pos[*it]];p [3].dot=pos[*it]; if(++it!=s.end ()) {p[4].ret=hashed[h[pos[*it]];p [4].dot=pos[*it];} It--; } if(it!=S.begin ()) {It--; p[1].ret=hashed[h[pos[*it]];p [1].dot=pos[*it]; if(It!=s.begin ()) {it--;p [2].ret=hashed[h[pos[*it]];p [2].dot=pos[*it];it++;} } for(intj=1; j<=4; j + +) { if(p[j].ret!=inf) {P[j].rets=hashed[h[i]]-P[j].ret; P[j].ret=abs (hashed[h[i]]-P[j].ret); }} sort (P+1, p+5, CMP); if(p[1].ret!=inf) aft[i][1]=p[1].dot; if(p[2].ret!=inf) aft[i][2]=p[2].dot; S.insert (H[i]); } for(Long LongI=1; i<=n;i++) {anc[2*i-1][0]=2*aft[i][1];d is1[2*i-1][0]=abs (hashed[h[aft[i][1]]]-hashed[h[i]]);d is2[2*i-1][0]=0; anc[2*i][0]=max (2*aft[i][2]-1,(Long Long)0);d is1[2*i][0]=0;d is2[2*i][0]=abs (hashed[h[aft[i][2]]]-Hashed[h[i]]); } N*=2; for(Long LongE=1; e<= +; e++) for(Long LongI=1; i<=n;i++) {Anc[i][e]=anc[anc[i][e-1]][e-1]; Dis1[i][e]=dis1[i][e-1]+dis1[anc[i][e-1]][e-1]; Dis2[i][e]=dis2[i][e-1]+dis2[anc[i][e-1]][e-1]; } scanf ("%lld",&x); for(Long LongI=2; i<=n;i+=2) {ask (i,x); Update (i/2);} printf ("%d\n", ans); scanf ("%lld",&m); for(Long LongI=1; i<=m;i++) {scanf ("%lld%lld",&x,&y); Ask (2*x, y); printf ("%lld%lld\n", Ret2,ret1); } return 0;}
Vijos 1780 travel by car