题意简述
维护一个序列每个位置有 , 两个参数,支持修改 , 两个参数。以及查询某一段区间的一个三元组( , ,)。使得在满足 的前提下, 尽量大。
做法
简单的线段树合并答案的题目,只需要考虑答案如何在线段树上进行合并就好的。首先考虑只有 种贡献答案的可能。
- 在同一个儿子内。
- 在不同的儿子内。
对于第一种情况直接大力转移就好了,对于第二种情况我们可以考虑维护两个值,一个是 , 还有一个是 。就可以进行转移了,具体操作见代码。
#include<bits/stdc++.h>
#define F(i,x,n) for(int i=x;i<=n;++i)
#define _F(i,x,n) for(int i=x;i>=n;--i)
#define lson x<<1
#define rson x<<1|1
using namespace std;
inline int read(){
int s=0;char c=getchar();bool flag=0;
while(!isdigit(c))flag|=(c=='-'),c=getchar();
while(isdigit(c))s=(s<<3)+(s<<1)+(c^48),c=getchar();
return flag?-s:s;
}
const int N=5e5+5;
const int Inf=1e9+7;
struct node{
int mx,mxa,mnb,mxl,mxr;
}t[N<<2];
int a[N],b[N];
int n,m;
void pushup(int x){//合并操作
t[x].mxa=max(t[lson].mxa,t[rson].mxa),t[x].mnb=min(t[lson].mnb,t[rson].mnb);
t[x].mxl=max(t[lson].mxl,max(t[rson].mxl,t[lson].mxa-t[rson].mnb));
t[x].mxr=max(t[lson].mxr,max(t[rson].mxr,t[rson].mxa-t[lson].mnb));
t[x].mx=max(t[lson].mx,max(t[rson].mx,max(t[lson].mxl+t[rson].mxa,t[lson].mxa+t[rson].mxr)));
}
void build(int x,int l,int r){
if(l==r){
t[x].mxa=a[l],t[x].mnb=b[l];
t[x].mx=t[x].mxl=t[x].mxr=-Inf;
return;
}
int mid=l+r>>1;
build(lson,l,mid),build(rson,mid+1,r);
pushup(x);
}
void upd(int x,int l,int r,int k,int v,bool op){
if(l==r){
if(op)t[x].mxa=v;
else t[x].mnb=v;
return;
}
int mid=l+r>>1;
if(k<=mid)upd(lson,l,mid,k,v,op);
else upd(rson,mid+1,r,k,v,op);
pushup(x);
}
node qry(int x,int l,int r,int L,int R){
if(l>R||r<L)return node{-Inf,-Inf,Inf,-Inf,-Inf};
if(L<=l&&r<=R){
return t[x];
}
node p1,p2,p3;
int mid=l+r>>1;
p1=qry(lson,l,mid,L,R),p2=qry(rson,mid+1,r,L,R);
p3.mxa=max(p1.mxa,p2.mxa),p3.mnb=min(p1.mnb,p2.mnb);
p3.mxl=max(p1.mxl,max(p2.mxl,p1.mxa-p2.mnb));
p3.mxr=max(p1.mxr,max(p2.mxr,p2.mxa-p1.mnb));
p3.mx=max(p1.mx,max(p2.mx,max(p1.mxl+p2.mxa,p2.mxr+p1.mxa)));
return p3;
}
signed main(){
n=read(),m=read();
F(i,1,n)a[i]=read();
F(i,1,n)b[i]=read();
build(1,1,n);
F(i,1,m){
int op=read();
if(op==1){
int x=read(),y=read();
upd(1,1,n,x,y,1);
}
else if(op==2){
int x=read(),y=read();
upd(1,1,n,x,y,0);
}
else{
int l=read(),r=read();
printf("%d\n",qry(1,1,n,l,r).mx);
}
}
return 0;
}