_H17_ @ 2024-11-16 10:32:34
#include<bits/stdc++.h>
#define int long long
#define ALL(x) x.begin(),x.end()
using namespace std;
constexpr int N=1e6+1;
struct ChthollyTreeNode{
int l,r;
mutable int val;
ChthollyTreeNode(int l=0,int r=0,int val=0):l(l),r(r),val(val){}
friend bool operator<(ChthollyTreeNode a,ChthollyTreeNode b){
return a.l<b.l;
}
};
set<ChthollyTreeNode>odt;
int n,q,a[N];
void build(){
int lst=1;
for(int i=2;i<=n;i++)
if(a[i]!=a[lst]){
odt.insert(ChthollyTreeNode(lst,i-1,a[lst]));
lst=i;
}
odt.insert(ChthollyTreeNode(lst,n,a[n]));
return;
}
set<ChthollyTreeNode>::iterator split(int x){//split[l,x-1],[x,r]
set<ChthollyTreeNode>::iterator id=odt.lower_bound(ChthollyTreeNode(x,0,0));
if(id==odt.end())
return id;
if((id->l)==x)
return id;
ChthollyTreeNode old=(*(--id));
odt.erase(id);
odt.insert(ChthollyTreeNode(old.l,x-1,old.val));
return odt.insert(ChthollyTreeNode(x,old.r,old.val)).first;
}
void assign(int l,int r,int val){
set<ChthollyTreeNode>::iterator it2=split(r+1),it1=split(l);
odt.erase(it1,it2),odt.insert(ChthollyTreeNode(l,r,val));
return;
}
void add(int l,int r,int val){
for(set<ChthollyTreeNode>::iterator it2=split(r+1),it1=split(l);it1!=it2;it1++)
(it1->val)+=val;
return;
}
int query(int l,int r){
int ret=-9e18;
for(set<ChthollyTreeNode>::iterator it2=split(r+1),it1=split(l);it1!=it2;it1++)
ret=max(ret,it1->val);
return ret;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
build();
for(int op,l,r,x;q;--q){
cin>>op>>l>>r;
if(op==1){
cin>>x;
assign(l,r,x);
}
else if(op==2){
cin>>x;
add(l,r,x);
}
else
cout<<query(l,r)<<'\n';
}
return 0;
}
只调 WA 部分。
by _H17_ @ 2024-11-16 11:21:19
@Lyw_Cyq_01好的谢谢
by _H17_ @ 2024-11-16 11:26:10
@Lyw_Cyq_01你split n+1 的时候感觉不太对劲,但是又搞不清楚