C++#include<bits/stdc++.h>
#define inl inline
#define int long long
#define lll __int128
#define fst ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,x,y) for(int i=x;i<=(y);++i)
#define per(i,x,y) for(int i=x;i>=(y);--i)
#define rpr(i,x,y,z) for(int i=x;i<=(y);i+=z)
#define epe(i,x,y,z) for(int i=x;i>=(y);i-=z)
#define repe(i,x,y) for(i=x;i<=(y);++i)
#define pere(i,x,y) for(i=x;i>=(y);--i)
#define endl '\n'
#define INF 0x3f3f3f3f3f3f3f3f
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define lcm(x,y) ((x)/__gcd(x,y)*(y))
#define ull unsigned long long
#define prr make_pair
#define pii pair<int,int>
#define gt(s) getline(cin,s)
#define at(x,y) for(auto x:y)
#define ff fflush(stdout)
#define mt(x,y) memset(x,y,sizeof(x))
#define idg isdigit
#define fp(s) string ssss=s;freopen((ssss+".in").c_str(),"r",stdin);freopen((ssss+\
".out").c_str(),"w",stdout);
#define sstr stringstream
#define all(x) x.begin(),x.end()
#define mcy(a,b) memcpy(a,b,sizeof(b))
#define ui unsigned
#define si signed
#define eb emplace_back
#define pff(x) ((x)*(x))
#define eush emplace
#define double long double
#define pdi pair<double,int>
#define gc getchar
#define pc putchar
using namespace std;
const int N=1e6+5,M=20;
int a[N],pos[N],r[N],f[N][M],p[N][M],n;
si idx[N][M],nxt[N][M];
stack<int>st;
inl int query(int l,int r){
int len=__lg(r-l+1);
if(a[idx[l][len]]>=a[idx[r-(1ll<<len)+1][len]]) return idx[r-(1ll<<len)+1][len];
return idx[l][len];
}
inl int getans(int s,int post){
int u=s,posu=pos[u],ans=0;
per(i,__lg(n),0)
if(p[u][i]<post){
ans+=f[u][i]-(posu-pos[u])*a[u];
// cout<<"aaa "<<ans<<' '<<i<<' '<<post<<' '<<u<<' '<<posu<<' '<<i<<endl;
posu=p[u][i];
u=nxt[u][i];
}
// cout<<posu<<' '<<post<<endl;
return ans+(post-posu)*a[u];
}
signed main(){
// fp("heluaisita");
fst;
int q,m;
cin>>n>>q>>m;
rep(i,1,n) cin>>a[i];
rep(i,2,n) cin>>pos[i];
rep(i,2,n) pos[i]+=pos[i-1];
r[n]=n;
per(i,n-1,1){
r[i]=r[i+1];
while(pos[r[i]]-pos[i]>m) --r[i];
}
per(i,n,1){
while(!st.empty()&&a[st.top()]>a[i]) st.pop();
if(!st.empty()) r[i]=min(r[i],st.top());
st.push(i);
}
rep(i,1,n) idx[i][0]=i;
rep(j,1,__lg(n))
rep(i,1,n-(1ll<<j)+1)
if(a[idx[i][j-1]]>=a[idx[i+(1ll<<j-1)][j-1]]) idx[i][j]=idx[i+(1ll<<j-1)][j-1];
else idx[i][j]=idx[i][j-1];
rep(i,1,n)
if(a[r[i]]>a[i]){
nxt[i][0]=query(i+1,r[i]);
p[i][0]=pos[i]+m;
f[i][0]=a[i]*m;
}else{
nxt[i][0]=r[i];
p[i][0]=pos[r[i]];
f[i][0]=a[i]*(pos[r[i]]-pos[i]);
}
rep(j,1,__lg(n))
rep(i,1,n){
nxt[i][j]=nxt[nxt[i][j-1]][j-1];
p[i][j]=p[nxt[i][j-1]][j-1];
f[i][j]=f[i][j-1]+f[nxt[i][j-1]][j-1]-(p[i][j-1]-pos[nxt[i][j-1]])*a[nxt[i][j-1]];
}
rep(i,1,q){
int s,t,v;
cin>>s>>t>>v;
if(pos[t]<=pos[s]+v) cout<<0<<endl;
else{
// cout<<getans(s,pos[t])<<' '<<getans(s,pos[s]+v)<<endl;
cout<<getans(s,pos[t])-getans(s,pos[s]+v)<<endl;
}
}
return 0;
}
C++#include<iostream>
#include<algorithm>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define lc (u<<1)
#define rc (lc|1)
#define Lc lc,l,mid
#define Rc rc,mid+1,r
#define mid ((l+r)>>1)
#define U 1,1,m
using namespace std;
const int N=1e6+1;
const int inf=6e6;
ll read()
{
ll s=0; char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s;
}
void print(ll x)
{
if(x>=10) print(x/10);
putchar(x%10+'0');
}
int n,m,q,k,a[N]; ll V,p[N],b[N<<1],res[N],np[N],vp[N];
struct ask{int id,s; ll l,r;}c[N];
bool cmps(ask A,ask B) {return A.s>B.s;}
struct nod{int mx,cmx,tag; ll lmx,w;};
struct SG
{
nod w[N<<3];
void pushup(int u)
{
w[u].w=w[lc].w+w[rc].w;
w[u].mx=max(w[lc].mx,w[rc].mx);
w[u].cmx=max(w[lc].cmx,w[rc].cmx);
w[u].lmx=w[lc].lmx*(w[lc].mx==w[u].mx)+w[rc].lmx*(w[rc].mx==w[u].mx);
int t=(w[lc].mx^w[rc].mx^w[u].mx);
if(t<w[u].mx) w[u].cmx=max(w[u].cmx,t);
}
void pt(int u,int x) {w[u].tag+=x,w[u].mx+=x,w[u].w+=w[u].lmx*x;}
void pushdown(int u)
{
int lm=max(w[lc].mx,w[rc].mx),tg=w[u].tag; w[u].tag=0;
if(w[lc].mx==lm) pt(lc,tg);
if(w[rc].mx==lm) pt(rc,tg);
}
void build(int u,int l,int r)
{
if(l==r) {w[u]={inf,-inf,0,b[l]-b[l-1],(b[l]-b[l-1])*inf}; return;}
build(Lc),build(Rc),pushup(u);
}
void update(int u,int l,int r,int L,int R,int x)
{
if(w[u].mx<=x) return;
if(l>=L&&r<=R&&w[u].cmx<=x) {pt(u,x-w[u].mx); return;}
if(w[u].tag) pushdown(u);
if(L<=mid) update(Lc,L,R,x);
if(R>mid) update(Rc,L,R,x);
pushup(u);
}
ll query(int u,int l,int r,int L,int R)
{
if(l>=L&&r<=R) return w[u].w;
if(w[u].tag) pushdown(u);
ll tr=0;
if(L<=mid) tr+=query(Lc,L,R);
if(R>mid) tr+=query(Rc,L,R);
return tr;
}
int check(int u,int l,int r,int p)
{
if(l==r) return w[u].mx;
if(w[u].tag) pushdown(u);
return p<=mid?check(Lc,p):check(Rc,p);
}
}T;
int main()
{
freopen("heluaisita.in","r",stdin);
freopen("heluaisita.out","w",stdout);
n=read(),q=read(),V=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=2;i<=n;i++) p[i]=read(),p[i]+=p[i-1];
for(int i=1;i<=n;i++)
{
b[++m]=p[i];
if(p[i]+V<=p[n]) b[++m]=p[i]+V;
}
sort(b+1,b+1+m),m=unique(b+1,b+1+m)-b-1;
for(int i=1;i<=n;i++) np[i]=lb(b+1,b+1+m,p[i])-b,vp[i]=ub(b+1,b+1+m,p[i]+V)-b-1;
for(int i=1;i<=q;i++)
{
int s=read(),t=read(); ll v=read();
if(p[s]+v<p[t]) c[++k]={i,s,p[s]+v,np[t]};
}
sort(c+1,c+1+k,cmps),T.build(U); int tp=1;
for(int i=n;i>=1;i--)
{
if(i<n) T.update(U,np[i]+1,vp[i],a[i]);
while(tp<=k&&c[tp].s==i)
{
ll L=c[tp].l; int id=c[tp].id,r=c[tp].r,l=ub(b+1,b+1+m,L)-b;
if(b[l-1]<L) l++;
if(l<=r) res[id]=T.query(U,l,r);
if(b[l-1]>L) res[id]+=(b[l-1]-L)*T.check(U,l-1);
tp++;
}
if(tp>k) break;
}
for(int i=1;i<=q;i++) print(res[i]),putchar('\n');
return 0;
}