rt,样例能够,测试点1也能过,大数据死循环
正常splay做法
#include<bits/stdc++.h>
#define lowbit(x) (x&-x)
#define lson id*2
#define rson id*2+1
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod = 998244353,N = 500005,inf = 1ll<<40;
ll stk[N],top = 0;
inline ll read(){
ll 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 << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
ll tree[N],L[N],R[N],cnt = 0,siz[N],lazy[N],lazy2[N],mxl[N],mxr[N],sum[N],ans[N],a[N],f[N];
ll getpos(ll x){
if(L[f[x]] == x)return 0;
return 1;
}
void del(ll x){
stk[++top] = x;
if(getpos(x)){
R[f[x]] = 0;
}else{
L[f[x]] = 0;
}
}
ll getnew(){
if(top){
ll tmp = stk[top--];
del(L[tmp]);del(R[tmp]);
f[tmp] = siz[tmp] = lazy[tmp] = lazy2[tmp] = mxl[tmp] = mxr[tmp] = sum[tmp] = ans[tmp] = L[tmp] = R[tmp] = 0;
return tmp;
}
return ++cnt;
}
ll n,m;
ll root = 1;
void pushup(ll x){
siz[0] = sum[0] = mxl[0] = mxr[0] = ans[0] = 0;
siz[x] = siz[L[x]]+siz[R[x]] + 1;
sum[x] = sum[L[x]]+sum[R[x]] + tree[x];
mxl[x] = max(sum[L[x]]+max(tree[x]+max(mxl[R[x]],0ll),0ll),mxl[L[x]]);
mxr[x] = max(sum[R[x]]+max(tree[x]+max(mxr[L[x]],0ll),0ll),mxr[R[x]]);
ans[x] = max(ans[L[x]],max(ans[R[x]],(max(mxr[L[x]],0ll) + max(mxl[R[x]],0ll) + tree[x])));
}
ll build(ll l,ll r){
if(l > r)return 0;
ll tmp = getnew();
if(l == r){
tree[tmp] = sum[tmp] = mxl[tmp] = mxr[tmp] = a[l];
siz[tmp] = 1;
return cnt;
}
ll mid = l+r >> 1;
tree[tmp] = a[mid];
L[tmp] = build(l,mid-1);
if(L[tmp])f[L[tmp]] = tmp;
R[tmp] = build(mid+1,r);
if(R[tmp])f[R[tmp]] = tmp;
siz[tmp] = siz[L[tmp]] + siz[R[tmp]] + 1;
pushup(tmp);
return tmp;
}
void push_down(ll x){
if(lazy[x]){
swap(L[x],R[x]);
lazy[L[x]] ^= lazy[x];
lazy[R[x]] ^= lazy[x];
swap(mxl[x],mxr[x]);
lazy[x] = 0;
}
if(lazy2[x]){
tree[x] = lazy2[x];
lazy2[L[x]] = lazy2[x];
lazy2[R[x]] = lazy2[x];
sum[x] = siz[x]*lazy2[x];
if(lazy2[x] >= 0){
ans[x] = mxl[x] = mxr[x] = sum[x];
}else{
ans[x] = mxl[x] = mxr[x] = lazy2[x];
}
lazy2[x] = 0;
}
}
void turn(ll x){
push_down(f[x]);push_down(x);
ll p = getpos(x),F = f[x],FF = f[F],pp = getpos(F);
ll mid = (p?L[x]:R[x]);
f[mid] = F;
f[x] = FF;
f[F] = x;
if(!p)R[x] = F,L[F] = mid;
else L[x] = F,R[F] = mid;
if(!pp)L[FF] = x;else R[FF] = x;
pushup(F);
pushup(x);
}
void rotate(ll x){
for (int fa;fa=f[x];turn(x)){
if (f[fa]){
turn((getpos(x)==getpos(fa))?fa:x);
}
}
root=x;
}
void rotate2(ll x){
for(int qwq;(qwq=f[x])!=root;turn(x)){
if(f[qwq]!=root){
turn(getpos(x)==getpos(qwq)?qwq:x);
}
}
}
int find(int x){
int now = root;
// if(siz[now]+1 < x)return 1;
while(1){
push_down(now);
// cout<<now<<" "<<x<<endl;
if(x<=siz[L[now]]){
now=L[now];
}
else {
x-=siz[L[now]] + 1;
if(!x)return now;
now=R[now];
}
}
}
void twist(ll u,ll v){
rotate(find(u));
rotate2(find(v));
lazy[L[R[root]]] ^= 1;
}
void dfs(ll x){
push_down(x);
if(L[x])dfs(L[x]);
if(abs(tree[x]) != (1LL<<40))printf("%lld ",tree[x]);
if(R[x])dfs(R[x]);
}
char IMP[30];
int main(){
freopen("P2042_2.in","r",stdin);
freopen("tmp.out","w",stdout);
n = read(),m = read();
a[1] = 0,a[n+2] = 0;
for(int i = 1;i <= n;i ++)a[i+1] = read();
root = build(1,n+2);
while(m --){
scanf("%s",IMP);
// cout<<m<<endl;
// printf("%s\n",IMP);
if(IMP[0] == 'I'){
ll pos = read(),tot = read();
for(int i = 1;i <= tot;i ++)a[i] = read();
rotate(find(pos+1));
rotate2(find(pos+2));
n += tot;
L[R[root]] = build(1,tot);
f[L[R[root]]] = R[root];
}else if(IMP[0] == 'D'){
ll pos = read(),tot = read();
rotate(find(pos));
rotate2(find(pos+tot+1));
n -= tot;
del(L[R[root]]);
pushup(R[root]);pushup(root);
}else if(IMP[0] == 'R'){
ll pos = read(),tot = read();
rotate(find(pos));
rotate2(find(pos+tot+1));
lazy[L[R[root]]]^=1;
push_down(L[R[root]]);
pushup(R[root]);pushup(root);
// rotate(find(1));
// rotate2(find(n+2));
// cout<<"ROOT = "<<root<<"\n";
// for(int i = 1;i <= n+5;i ++)cout<<i<<" "<<tree[i]<<" "<<L[i]<<" "<<R[i]<<" "<<mxl[i]<<" "<<mxr[i]<<" "<<ans[i]<<endl;
}else if(IMP[0] == 'G'){
ll pos = read(),tot = read();
rotate(find(pos));
rotate2(find(pos+tot+1));
// for(int i = 1;i <= n+5;i ++)cout<<ans[i]<<" "<<tree[i]<<" "<<L[i]<<" "<<R[i]<<endl;cout<<endl;
printf("%lld\n",sum[L[R[root]]]);
}else{
if(IMP[2] == 'X'){
rotate(find(1));
// if(m == 18762)cout<<"YES";
rotate2(find(n+2));
printf("%lld\n",ans[L[R[root]]]);
}else{
ll pos = read(),tot = read();
rotate(find(pos));
rotate2(find(pos+tot+1));
lazy2[L[R[root]]] = read();
push_down(L[R[root]]);
pushup(R[root]);pushup(root);
}
}
// dfs(root);cout<<endl;
}//
return 0;
}
/*
9 8
2 -6 3 5 1 -5 -3 6 4
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 -2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM
*/