Infinity_Fantasy @ 2024-02-10 22:55:33
#include<bits/stdc++.h>
using namespace std;
int n,m,op,x,y;
struct node{
int maxv,maxl,maxr,sum;
}tmp,tree[1000005];
void pushup(node &fa,node &ls,node &rs){
if(ls.maxr<0&&rs.maxl<0) fa.maxv=max(ls.maxr,rs.maxl);
else{
fa.maxv=0;
if(ls.maxr>0) fa.maxv+=ls.maxr;
if(rs.maxl>0) fa.maxv+=rs.maxl;
}
fa.maxv=max(max(fa.maxv,ls.maxr),rs.maxl);
fa.maxl=max(ls.maxl,ls.sum+rs.maxl);
fa.maxr=max(rs.maxr,rs.sum+ls.maxr);
fa.sum=ls.sum+rs.sum;
}
void build(int p,int l,int r){
if(l==r){
cin>>tree[p].maxv;
tree[p].sum=tree[p].maxl=tree[p].maxr=tree[p].maxv;
return;
}
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(tree[p],tree[p<<1],tree[p<<1|1]);
}
void update(int p,int l,int r,int k,int d){
if(l==r){
tree[p].maxl=tree[p].maxr=tree[p].maxv=tree[p].sum=d;
return;
}
int mid=l+r>>1;
if(k<mid) update(p<<1,l,mid,k,d);
else update(p<<1|1,mid+1,r,k,d);
pushup(tree[p],tree[p<<1],tree[p<<1|1]);
}
node query(int p,int l,int r,int s,int c){
if(s<=l&&r<=c) return tree[p];
int mid=l+r>>1;
if(s<=mid&&mid<=c){
tmp.maxl=tmp.maxr=tmp.maxv=tmp.sum=0;
pushup(tmp,query(p<<1,l,mid,s,c),query(p<<1|1,mid+1,r,s,c));
return tmp;
}else if(s<=mid) return query(p<<1,l,mid,s,c);
return query(p<<1|1,mid+1,r,s,c);
}
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
build(1,1,n);
while(m--){
cin>>op>>x>>y;
if(op==1){
if(x>y) swap(x,y);
cout<<query(1,1,n,x,y).maxv<<"\n";
}else update(1,1,n,x,y);
}
return 0;
}
by Infinity_Fantasy @ 2024-02-10 23:06:20
有没有大佬帮可怜的我康康这个CE呀
o(TヘTo)
by 杜都督 @ 2024-02-10 23:15:28
大致看了一眼,把右值改成指针试试吧,让query()也返回指针
(最近怎么全是线段树,我真的快调吐了)
by Infinity_Fantasy @ 2024-02-10 23:18:23
@杜都督 为什么是右值呢?
by 杜都督 @ 2024-02-10 23:20:27
@Infinity_Fantasy 我是说,你的pushup()形参不是写的右值吗,可以改成指针试一下
by Infinity_Fantasy @ 2024-02-10 23:22:13
@杜都督 哦,不好意思,不知道&是右值的意思,献丑了
by Infinity_Fantasy @ 2024-02-10 23:27:11
@杜都督 这样吗?可还是不行鹅。
#include<bits/stdc++.h>
using namespace std;
int n,m,op,x,y;
struct node{
int maxv,maxl,maxr,sum;
}tmp,tree[1000005];
void pushup(node *fa,node *ls,node *rs){
if(ls->maxr<0&&rs->maxl<0) fa->maxv=max(ls->maxr,rs->maxl);
else{
fa->maxv=0;
if(ls->maxr>0) fa->maxv+=ls->maxr;
if(rs->maxl>0) fa->maxv+=rs->maxl;
}
fa->maxv=max(max(fa->maxv,ls->maxr),rs->maxl);
fa->maxl=max(ls->maxl,ls->sum+rs->maxl);
fa->maxr=max(rs->maxr,rs->sum+ls->maxr);
fa->sum=ls->sum+rs->sum;
}
void build(int p,int l,int r){
if(l==r){
cin>>tree[p].maxv;
tree[p].sum=tree[p].maxl=tree[p].maxr=tree[p].maxv;
return;
}
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(&tree[p],&tree[p<<1],&tree[p<<1|1]);
}
void update(int p,int l,int r,int k,int d){
if(l==r){
tree[p].maxl=tree[p].maxr=tree[p].maxv=tree[p].sum=d;
return;
}
int mid=l+r>>1;
if(k<mid) update(p<<1,l,mid,k,d);
else update(p<<1|1,mid+1,r,k,d);
pushup(&tree[p],&tree[p<<1],&tree[p<<1|1]);
}
node query(int p,int l,int r,int s,int c){
if(s<=l&&r<=c) return tree[p];
int mid=l+r>>1;
if(s<=mid&&mid<=c){
tmp.maxl=tmp.maxr=tmp.maxv=tmp.sum=0;
pushup(&tmp,query(p<<1,l,mid,s,c),query(p<<1|1,mid+1,r,s,c));
return tmp;
}else if(s<=mid) return query(p<<1,l,mid,s,c);
return query(p<<1|1,mid+1,r,s,c);
}
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
build(1,1,n);
while(m--){
cin>>op>>x>>y;
if(op==1){
if(x>y) swap(x,y);
cout<<query(1,1,n,x,y).maxv<<"\n";
}else update(1,1,n,x,y);
}
return 0;
}
by 杜都督 @ 2024-02-10 23:42:17
@Infinity_Fantasy 对不起迷糊了,不是右值是左值,不过这个不重要,你新改的代码要把query()也改成返回node*才行
by Infinity_Fantasy @ 2024-02-10 23:46:03
@杜都督 然后变成RE了、、
#include<bits/stdc++.h>
using namespace std;
int n,m,op,x,y;
struct node{
int maxv,maxl,maxr,sum;
}tmp,tree[1000005];
void pushup(node *fa,node *ls,node *rs){
if(ls->maxr<0&&rs->maxl<0) fa->maxv=max(ls->maxr,rs->maxl);
else{
fa->maxv=0;
if(ls->maxr>0) fa->maxv+=ls->maxr;
if(rs->maxl>0) fa->maxv+=rs->maxl;
}
fa->maxv=max(max(fa->maxv,ls->maxr),rs->maxl);
fa->maxl=max(ls->maxl,ls->sum+rs->maxl);
fa->maxr=max(rs->maxr,rs->sum+ls->maxr);
fa->sum=ls->sum+rs->sum;
}
void build(int p,int l,int r){
if(l==r){
cin>>tree[p].maxv;
tree[p].sum=tree[p].maxl=tree[p].maxr=tree[p].maxv;
return;
}
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(&tree[p],&tree[p<<1],&tree[p<<1|1]);
}
void update(int p,int l,int r,int k,int d){
if(l==r){
tree[p].maxl=tree[p].maxr=tree[p].maxv=tree[p].sum=d;
return;
}
int mid=l+r>>1;
if(k<mid) update(p<<1,l,mid,k,d);
else update(p<<1|1,mid+1,r,k,d);
pushup(&tree[p],&tree[p<<1],&tree[p<<1|1]);
}
node* query(int p,int l,int r,int s,int c){
if(s<=l&&r<=c) return &tree[p];
int mid=l+r>>1;
if(s<=mid&&mid<=c){
tmp.maxl=tmp.maxr=tmp.maxv=tmp.sum=0;
pushup(&tmp,query(p<<1,l,mid,s,c),query(p<<1|1,mid+1,r,s,c));
return &tmp;
}else if(s<=mid) return query(p<<1,l,mid,s,c);
return query(p<<1|1,mid+1,r,s,c);
}
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
build(1,1,n);
while(m--){
cin>>op>>x>>y;
if(op==1){
if(x>y) swap(x,y);
cout<<query(1,1,n,x,y)->maxv<<"\n";
}else update(1,1,n,x,y);
}
return 0;
}
by 杜都督 @ 2024-02-10 23:46:58
@Infinity_Fantasy 那可能是逻辑有问题,我上电脑看看吧
by Infinity_Fantasy @ 2024-02-10 23:48:27
@杜都督 orz 十分感谢