线段树直接维护区间最大值即可。或者可以发现,最大值只可能出现在最左端或最右端,贪心解决。
线段树
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m,a[N];
struct node{
ll mx,lazy;
}tree[N<<2];
inline void pushup(int p){
tree[p].mx=max(tree[p<<1].mx,tree[p<<1|1].mx);
}
inline void pushdown(int p){
if(!tree[p].lazy) return;
tree[p<<1].lazy=tree[p].lazy;tree[p<<1|1].lazy=tree[p].lazy;
tree[p<<1].mx=tree[p].lazy;tree[p<<1|1].mx=tree[p].lazy;
tree[p].lazy=0;
}
inline void build(int p,int l,int r){
if(l==r){tree[p].mx=a[l];return;}
int mid=l+r>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
pushup(p);
}
ll qmax(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return tree[p].mx;
int mid=l+r>>1;pushdown(p);ll res=0;
if(x<=mid) res=max(res,qmax(p<<1,l,mid,x,y));
if(y>mid) res=max(res,qmax(p<<1|1,mid+1,r,x,y));
return res;
}
void update(int p,int l,int r,int x,int y,ll val){
if(x<=l&&r<=y){tree[p].lazy=val;tree[p].mx=val;return;}
int mid=l+r>>1;pushdown(p);
if(x<=mid) update(p<<1,l,mid,x,y,val);
if(y>mid) update(p<<1|1,mid+1,r,x,y,val);
pushup(p);
}
int main(){
// freopen("a.in","r",stdin);
n=read();
for(int i=1;i<=n;++i) a[i]=read();
build(1,1,n);m=read();
while(m--){
int x=read(),val=read();
ll res=qmax(1,1,n,1,x);
printf("%I64d\n",res);
update(1,1,n,1,x,res+val);
}
return 0;
}