1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define Cn const #define CI Cn int& #define gc getchar #define DD isdigit(c=gc()) #define pc(c) putchar((c)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!DD) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),DD);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=5e4+10,M=sqrt(N)+50; int n,m,SZ,top,tot,bl[N],L[M],R[M],S[N]; struct Val{int D,L;}a[N]; struct node{int g,s;}stk[N]; #define M(x,y) ((node){x,y}) I bool cmp(Cn node& x,Cn node& y){return x.g^y.g?x.g<y.g:x.s<y.s;} struct Block{ node a[N],pre[M],suf[M]; int sz,G,S,pt,st; I void P(node x){a[++sz]=x;} I void Pp(node x){pre[++pt]=x;} I void Ps(node x){suf[++st]=x;} I void B(){ RI i;for(top=0,sort(a+1,a+sz+1,cmp),i=1;i<=sz;stk[++top]=a[i],i++) W(top&&a[i].s>=stk[top].s) top--; for(i=1;i<=top;i++) a[i]=stk[i];sz=top; for(top=0,sort(pre+1,pre+pt+1,cmp),i=1;i<=pt;stk[++top]=pre[i],i++) W(top&&pre[i].s>=stk[top].s) top--; for(i=1;i<=top;i++) pre[i]=stk[i];pt=top; for(top=0,sort(suf+1,suf+st+1,cmp),i=1;i<=st;stk[++top]=suf[i],i++) W(top&&suf[i].s>=stk[top].s) top--; for(i=1;i<=top;i++) suf[i]=stk[i];st=top; } I int V(Cn node& v,CI x0){return min(v.g,v.s+x0);} I int Q(node* x,CI x0){ RI n=x==a?sz:x==pre?pt:x==suf?st:-1,l=1,r=n,p=-1,X=x0;if(!(~n)) return 0; #define mid (l+r>>1) W(l<=r) x[mid].g>=x[mid].s+x0?p=mid,r=mid-1:l=mid+1; if(~p) return X=V(x[p],x0),p<n&&(X=max(X,V(x[p+1],x0))),p>1&&(X=max(X,V(x[p-1],x0))),X; else return max(max(V(x[1],x0),V(x[n],x0)),x0); } }b[M]; I void B(){ RI i,j,k,v;for(i=1;i<=n;i++) !((i-1)%SZ)&&(R[tot]=i-1,L[++tot]=i),bl[i]=tot;R[tot]=n; #define MS M(v,S[k]-S[j-1]) for(i=1;i<=tot;b[i].B(),i++) for(b[i].S=S[R[i]]-S[L[i]-1],j=L[i];j<=R[i];j++) for(v=2e9,k=j;k<=R[i];k++) v=min(a[k].L,v+a[k].D),b[i].P(MS),j==L[i]&&(b[i].Pp(MS),0),k==R[i]&&(b[i].Ps(MS),0),j==L[i]&&k==R[i]&&(b[i].G=v); } I int Q(CI l,CI r,CI x0){ RI i,A=x0,bL=bl[l],bR=bl[r],X=x0;for(i=l;i<=min(r,R[bL]);i++) X=min(max(X,x0)+a[i].D,a[i].L),A=max(A,X); if(X=max(X,x0),bL<bR){for(i=bL+1;i<=bR-1;i++) A=max(A,b[i].Q(b[i].pre,X)),X=min(S[R[i]]-S[L[i]-1]+X,b[i].G), A=max(A,b[i].Q(b[i].a,x0)),X=max(X,b[i].Q(b[i].suf,x0)),X=max(X,x0); for(i=L[bR];i<=r;i++) X=min(max(X,x0)+a[i].D,a[i].L),A=max(A,X);}return A; } int main(){ RI i,l,r,x0;for(read(n,m),SZ=sqrt(N),i=1;i<=n;i++) read(a[i].D),S[i]=S[i-1]+a[i].D; for(i=1;i<=n;i++) read(a[i].L);B();W(m--) read(l,r,x0),writeln(Q(l,r,x0));return 0; }
|