不知道为什么,每次提交 A 的点不一样,其他点就 RE 调了 2 天了。
#include <bits/stdc++.h>
#define int long long
#define ls (tr[x].son[0])
#define rs (tr[x].son[1])
#define mid ((l+r)>>1)
#define inf (1<<30)
using namespace std;
int read(){
int f=1,x=0;char c=getchar();
while(c<'0' || c>'9'){if(c=='-') f=-1;c=getchar();}
while(c>='0' && c<='9') x=(x<<1)+(x<<3)+(c-'0'),c=getchar();
return x*f;
}
const int N=2e5+5;
int ind,root,n,m,st[N],top,a[N];
struct Tr{
int siz,rnd,rev,cov,son[2],val;
int mx,s,l,r;
}tr[N];
int New(int val){
int x;
x=top?st[top--]:++ind;
tr[x].son[0]=tr[x].son[1]=tr[x].rev=0;
tr[x].cov=inf;
tr[x].rnd=rand();
tr[x].siz=1;
tr[x].val=tr[x].s=tr[x].mx=val;
// cout << x << " " << tr[x].rnd << "\n";
tr[x].l=tr[x].r=max(val,0ll);
return x;
}
void pushup(int x){
tr[x].siz=tr[ls].siz+tr[rs].siz+1;
tr[x].s=tr[ls].s+tr[rs].s+tr[x].val;
tr[x].l=max({tr[ls].l,0ll,tr[ls].s+tr[x].val+tr[rs].l});
tr[x].r=max({tr[rs].r,0ll,tr[rs].s+tr[x].val+tr[ls].r});
tr[x].mx=max(tr[x].val,tr[ls].r+tr[x].val+tr[rs].l);
// cout << x << "\n";
if(ls) tr[x].mx=max(tr[x].mx,tr[ls].mx);
if(rs) tr[x].mx=max(tr[x].mx,tr[rs].mx);
}
void rev(int x){
if(!x) return;
swap(tr[x].l,tr[x].r);
swap(ls,rs);
tr[x].rev^=1;
}
void cove(int x,int v){
// if(!x) return;
tr[x].cov=tr[x].val=v;
tr[x].s=tr[x].siz*v;
tr[x].l=tr[x].r=max(0ll,tr[x].s);
tr[x].mx=max(v,tr[x].s);
}
void pushdown(int x){
if(tr[x].cov!=inf){
int c=tr[x].cov;
if(ls) cove(ls,c);
if(rs) cove(rs,c);
tr[x].cov=inf;
}
if(tr[x].rev){
if(ls) rev(ls);
if(rs) rev(rs);
tr[x].rev=0;
}
}
void split_siz(int rt,int k,int &x,int &y){
if(!rt){
x=y=0;
return;
}
pushdown(rt);
if(k<=tr[tr[rt].son[0]].siz){
y=rt;
split_siz(tr[y].son[0],k,x,tr[y].son[0]);
pushup(y);
}else{
x=rt;
split_siz(tr[x].son[1],k-tr[tr[x].son[0]].siz-1,tr[x].son[1],y);
pushup(x);
}
pushup(rt);
}
int merge(int x,int y){
if(!x||!y) return x+y;
if(tr[x].rnd>tr[y].rnd){
pushdown(x);
tr[x].son[1]=merge(tr[x].son[1],y);
// cout << x << " " << tr[x].son[1] << "\n";
pushup(x);
return x;
}else{
pushdown(y);
tr[y].son[0]=merge(x,tr[y].son[0]);
pushup(y);
return y;
}
}
int build(int l,int r){
if(l==r) return New(read());
int x,y;
x=build(l,mid);
y=build(mid+1,r);
// cout << x << " " << y << "\n";
return merge(x,y);
}
void delte(int x){
if(ls)delte(ls);
if(rs)delte(rs);
st[++top]=x;
}
void del(int l,int r){
int x,y,z;
split_siz(root,r,x,z);
split_siz(x,l-1,x,y);
delte(y);
root=merge(x,z);
}
void cover(int l,int r,int v){
int x,y,z;
split_siz(root,r,x,z);
split_siz(x,l-1,x,y);
cove(y,v);
root=merge(merge(x,y),z);
}
void rever(int l,int r){
int x,y,z;
split_siz(root,r,x,z);
split_siz(root,l-1,x,y);
rev(y);
root=merge(merge(x,y),z);
}
int query(int l,int r){
int x,y,z;
split_siz(root,r,x,z);
split_siz(root,l-1,x,y);
// cout << 1 << "\n";
int ans=tr[y].s;
root=merge(merge(x,y),z);
return ans;
}
int mxs(int l,int r){
int x,y,z;
split_siz(root,r,x,z);
split_siz(root,l-1,x,y);
// cout << 1 << "\n";
int ans=tr[y].mx;
root=merge(merge(x,y),z);
return ans;
}
void Insert(int pos,int tot){
int x,y;
split_siz(root,pos,x,y);
root=merge(merge(x,build(pos,pos+tot-1)),y);
}
signed main(){
srand(time(NULL));
cin >> n >> m;
root=build(1,n);
while(m--){
string s;
int pos,tot;
cin >> s;
if(s[0]=='I'){
cin >> pos >> tot;
Insert(pos,tot);
}
else if(s[0]=='D'){
cin >> pos >> tot;
del(pos,pos+tot-1);
}
else if(s[0]=='R'){
cin >> pos >> tot;
rever(pos,pos+tot-1);
}
else if(s=="MAKE-SAME"){
int c;
cin >> pos >> tot >> c;
cover(pos,pos+tot-1,c);
}
else if(s=="GET-SUM"){
cin >> pos >> tot;
cout << query(pos,pos+tot-1) << "\n";
}
else if(s=="GET"){
cin >> pos;
cout << query(pos,pos) << "\n";
}else{
cin >> pos >> tot;
cout << mxs(pos,pos+tot-1) << "\n";
}
}
return 0 ;
}
/*
9 8
2 -6 3 5 1 -5 -3 6 3
GET 5
*/