当前位置: 代码迷 >> 综合 >> 维修数列 HYSBZ - 1500 (伸展树基本操作+区间求和最大子序列)
  详细解决方案

维修数列 HYSBZ - 1500 (伸展树基本操作+区间求和最大子序列)

热度:51   发布时间:2024-01-22 01:54:50.0

题意:

题解:

        伸展树的应用:

操作6 线段树也可以完成,但是有区间插入,删除,翻转的时候,伸展树甚是强大(基本操作!

#include<bits/stdc++.h>using namespace std;
const int maxn = 5e5+99;
const int INF = 0x3f3f3f3f;
#define ddd cout<<111<<endl;#define keyv ch[ch[root][1]][0]int pre[maxn],ch[maxn][2],sz[maxn],key[maxn],root,tot1;
int sum[maxn],rev[maxn],setv[maxn],lx[maxn],rx[maxn],mx[maxn];int s[maxn],tot2;
int n,q;
int a[maxn];void newnode(int &r,int fa,int k)
{if(tot2)r = s[tot2--];else r = ++tot1;key[r] = k;pre[r] = fa;rev[r] = setv[r] = ch[r][0] = ch[r][1] = 0;sz[r] = 1;sum[r] = lx[r] = rx[r] = mx[r] = k;
}void update_rev(int r)
{if(!r)return ;swap(ch[r][0],ch[r][1]);swap(lx[r],rx[r]);rev[r]^=1;
}void update_set(int r,int v)
{if(!r)return ;setv[r] = 1;key[r] = v;sum[r] = sz[r] * v;lx[r] = rx[r] = mx[r] = max(v,v*sz[r]);
}void pushup(int r)
{int lson = ch[r][0],rson = ch[r][1];sz[r] = 1 + sz[lson] + sz[rson];sum[r] = key[r] + sum[lson] + sum[rson];lx[r] = max(lx[lson],sum[lson]+key[r]+max(0,lx[rson]));///最大前缀和rx[r] = max(rx[rson],sum[rson]+key[r]+max(0,rx[lson]));///最大后缀和mx[r] = max(0,rx[lson])+key[r]+max(0,lx[rson]);///来自左和右mx[r] = max(mx[r],max(mx[lson],mx[rson]));///只来自左或者右
}void pushdown(int r)
{if(setv[r]){update_set(ch[r][0],key[r]);update_set(ch[r][1],key[r]);setv[r] = 0;}if(rev[r]){update_rev(ch[r][0]);update_rev(ch[r][1]);rev[r]=0;}}void build(int &x,int l,int r,int fa)
{if(l>r)return;int m = (l+r)>>1;newnode(x,fa,a[m]);build(ch[x][0],l,m-1,x);build(ch[x][1],m+1,r,x);pushup(x);
}
void init()
{root = tot1 = tot2 = 0;pre[root] = ch[root][0] = ch[root][1] = sz[root] = sum[root]= rev[root] = setv[root] = key[root] = 0;lx[root] = rx[root] = mx[root] = -INF;newnode(root,0,-1);newnode(ch[root][1],root,-1);for(int i=0;i<n;i++)scanf("%d",a+i);build(keyv,0,n-1,ch[root][1]);pushup(ch[root][1]);pushup(root);
}void rotate(int x,int d)
{int y = pre[x];pushdown(y);pushdown(x);ch[y][!d] = ch[x][d];pre[ch[x][d]] = y;if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x] = pre[y];ch[x][d] = y;pre[y] = x;pushup(y);
}
void splay(int r,int goal)
{while(pre[r]!=goal){if(pre[pre[r]]==goal)rotate(r,ch[pre[r]][0]==r);else{int y = pre[r];int d = ch[pre[y]][0]==y;if(ch[y][d]==r){rotate(r,!d);rotate(r,d);}else{rotate(y,d);rotate(r,d);}}}pushup(r);if(goal==0)root = r;
}int kth(int r,int k)
{pushdown(r);int t = sz[ch[r][0]]+1;if(k==t)return r;else if(k<t) return kth(ch[r][0],k);else return kth(ch[r][1],k-t);}void Insert(int pos,int tot)///插入
{for(int i=0;i<tot;i++)scanf("%d",a+i);splay(kth(root,pos+1),0);splay(kth(root,pos+2),root);build(keyv,0,tot-1,ch[root][1]);pushup(ch[root][1]);pushup(root);
}void Erase(int r)
{if(!r)return ;s[++tot2]=r;Erase(ch[r][0]);Erase(ch[r][1]);
}void Delete(int pos,int tot)///区间删除
{splay(kth(root,pos),0);splay(kth(root,pos+tot+1),root);Erase(keyv);pre[keyv] = 0;keyv = 0;pushup(ch[root][1]);pushup(root);
}void Set(int pos,int tot,int c)///区间修改
{splay(kth(root,pos),0);splay(kth(root,pos+tot+1),root);update_set(keyv,c);pushup(ch[root][1]);pushup(root);
}void Reverse(int pos,int tot)///区间翻转
{splay(kth(root,pos),0);splay(kth(root,pos+tot+1),root);update_rev(keyv);pushup(ch[root][1]);pushup(root);
}int Sum(int pos,int tot)///求和
{splay(kth(root,pos),0);splay(kth(root,pos+tot+1),root);return sum[keyv];
}int Maxsum(int pos,int tot)///求和最大的子列
{splay(kth(root,pos),0);splay(kth(root,pos+tot+1),root);return mx[keyv];
}int main()
{while(scanf("%d%d",&n,&q)==2){init();char op[22];int x,y,z;while(q--){scanf("%s",op);if(op[0] == 'I'){scanf("%d%d",&x,&y);Insert(x,y);}else if(op[0] == 'D'){scanf("%d%d",&x,&y);Delete(x,y);}else if(op[0] == 'M' && op[2]== 'K'){scanf("%d%d%d",&x,&y,&z);Set(x,y,z);}else if(op[0] == 'R'){scanf("%d%d",&x,&y);Reverse(x,y);}else if(op[0] == 'G'){scanf("%d%d",&x,&y);printf("%d\n",Sum(x,y));}else{printf("%d\n",Maxsum(1,sz[root]-2));}}}
}

  相关解决方案