E. Nastya Hasn’t Written a Legend
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In this task, Nastya asked us to write a formal statement.
An array a of length n and an array k of length n−1 are given. Two types of queries should be processed:
increase ai by x. Then if ai+1<ai+ki, ai+1 becomes exactly ai+ki; again, if ai+2<ai+1+ki+1, ai+2 becomes exactly ai+1+ki+1, and so far for ai+3, …, an;
print the sum of the contiguous subarray from the l-th element to the r-th element of the array a.
It’s guaranteed that initially ai+ki≤ai+1 for all 1≤i≤n−1.
Input
The first line contains a single integer n (2≤n≤105) — the number of elements in the array a.
The second line contains n integers a1,a2,…,an (−109≤ai≤109) — the elements of the array a.
The third line contains n−1 integers k1,k2,…,kn−1 (−106≤ki≤106) — the elements of the array k.
The fourth line contains a single integer q (1≤q≤105) — the number of queries.
Each of the following q lines contains a query of one of two types:
if the query has the first type, the corresponding line contains the character ‘+’ (without quotes), and then there are two integers i and x (1≤i≤n, 0≤x≤106), it means that integer x is added to the i-th element of the array a as described in the statement.
if the query has the second type, the corresponding line contains the character ‘s’ (without quotes) and then there are two integers l and r (1≤l≤r≤n).
Output
For each query of the second type print a single integer in a new line — the sum of the corresponding subarray.
如果单纯想着老老实实对每个修改操作去修改a数组,显然会超时。注意到我们更新的时候的表达式
a
[
j
]
=
a
′
[
i
]
+
∑
i
j
−
1
k
a[j]=a'[i]+\sum_{i}^{j-1}{k}
a[j]=a′[i]+∑ij−1k (其中j>=i,而且j<=e, 这里e是最后一个可供更新增大的数的下标)
可以更变成
b
[
j
]
=
a
[
j
]
−
∑
1
j
−
1
k
=
a
′
[
i
]
−
∑
1
i
−
1
k
=
b
′
[
i
]
b[j]=a[j]-\sum_1^{j-1}k=a'[i]-\sum_1^{i-1}k=b'[i]
b[j]=a[j]−∑1j−1k=a′[i]−∑1i−1k=b′[i],那么就转变成了一个区间set的问题,我们需要将区间
[
i
,
e
]
[i,e]
[i,e]的值统统增加到
b
′
[
i
]
b'[i]
b′[i]。那么就用线段树维护b数组解决好了。线段树维护的是区间和,再用一维
u
p
d
[
i
]
upd[i]
upd[i]表示作用于这个区间的set操作,需要支持区间set,区间查询,点查询。
至于e的值,可以由b数组的单调性去二分,这里效率变成了俩log,写单个log当然也可以,但是要额外维护一个区间最大值的域,而且又要另外写个函数,比较麻烦。
用
u
p
d
[
k
]
=
−
i
n
f
upd[k]=-inf
upd[k]=−inf来表示没有区间修改。由于这个问题的特殊性,后来的区间修改的值必然会大于原来区间中的每个数。那么我们只要在区间修改的时候做一次push_down操作,保证upd随着深度的增大而减小(当然-inf值除外),然后区间查询或者点查询的时候,取递归路径上upd的最大值去覆盖就行了(因为这个值一定比子区间的任何值都更加新)。
#include<cstdio>
#include<algorithm>
#define kl (k<<1)
#define kr (k<<1|1)
#define M (L+R>>1)
#define lin L,M
#define rin M+1,R
using namespace std;
using LL=long long;
int n,q,pos,l,r;
LL a[100005],K[100005],T[1<<18],upd[1<<18],x;
char o[2];
void build_tree(int k, int L, int R)
{
if(L==R)
{
T[k]=a[L];
upd[k]=-1E18;
return ;
}
build_tree(kl,lin);
build_tree(kr,rin);
T[k]=T[kl]+T[kr];
upd[k]=-1E18;
}
LL query(int k, int L, int R, int pos)
{
if(L==R)
return max(T[k],upd[k]);
if(pos<=M)
return max(query(kl,lin,pos),upd[k]);
else
return max(query(kr,rin,pos),upd[k]);
}
void modify(int k, int L, int R, int l, int r, LL val)
{
if(l<=L&&R<=r)
{
upd[k]=val;
return ;
}
upd[kr]=max(upd[k],upd[kr]);
upd[kl]=max(upd[k],upd[kl]);
upd[k]=-1E18;
if(l<=M)
modify(kl,lin,l,r,val);
if(r>M)
modify(kr,rin,l,r,val);
T[k]=(upd[kl]>-1E18?upd[kl]*(M-L+1):T[kl])+(upd[kr]>-1E18?upd[kr]*(R-M):T[kr]);
}
LL sum(int k, int L, int R, int l, int r, LL val)
{
if(l<=L&&R<=r)
return max(val,upd[k])>-1E18?max(val,upd[k])*(R-L+1):T[k];
LL res=0;
if(l<=M)
res+=sum(kl,lin,l,r,max(val,upd[k]));
if(r>M)
res+=sum(kr,rin,l,r,max(val,upd[k]));
return res;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=1;i<n;i++)
scanf("%lld",&K[i]),K[i]+=K[i-1],a[i+1]-=K[i];
for(int i=1;i<n;i++)
K[i]+=K[i-1];
scanf("%d",&q);
build_tree(1,1,n);
while(q--)
{
scanf("%s",o);
if(o[0]=='+')
{
scanf("%d%lld",&pos,&x);
LL tmp=query(1,1,n,pos)+x;
int L=pos,R=n+1;
while(L<R)
if(query(1,1,n,M)<=tmp)
L=M+1;
else
R=M;
modify(1,1,n,pos,L-1,tmp);
}
else
{
scanf("%d%d",&l,&r);
printf("%lld\n",sum(1,1,n,l,r,-1E18)+K[r-1]-(l>1?K[l-2]:0));
}
}
return 0;
}