BZOJ 1500 splay
来源:互联网 发布:武器知乎 编辑:程序博客网 时间:2024/05/20 19:17
点击打开链接
题意:中文
思路:就是几个splay的操作,然后最后一个操作可以利用线段树区间合并的思想,然后还有就是在反转操作的时候,左右的最大值也需要反转就是代码中的L和R
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=500010;int pre[maxn],ch[maxn][2],size[maxn],root,tot1;//父节点,左右孩子,子树规模,根节点,节点数量int key[maxn],flag[maxn],rev[maxn],m[maxn],sum[maxn];//该点的值,懒惰标记int a[maxn],n,q;int s[maxn],tot2,max1[maxn],L[maxn],R[maxn];void treaval(int x){//debug部分 if(x){ treaval(ch[x][0]); printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d,key=%2d add=%2d\n",x,ch[x][0],ch[x][1],pre[x],size[x],key[x],flag[x]); treaval(ch[x][1]); }}void debug(){ printf("root:%d\n",root);treaval(root);}void newnode(int &r,int fa,int k){ if(tot2) r=s[tot2--]; else r=++tot1; pre[r]=fa;size[r]=1;key[r]=m[r]=k;flag[r]=-2016;rev[r]=0;sum[r]=0; L[r]=R[r]=max1[r]=k; ch[r][0]=ch[r][1]=0;}void update_rev(int r){ if(r==0) return ; swap(ch[r][0],ch[r][1]); swap(L[r],R[r]); rev[r]^=1;}void update_add(int r,int val){ if(r==0) return ; flag[r]=val; key[r]=val; sum[r]=val*size[r]; L[r]=R[r]=max1[r]=max(val,val*size[r]);// m[r]+=val;}void pushup(int r){ size[r]=size[ch[r][0]]+size[ch[r][1]]+1; sum[r]=sum[ch[r][0]]+sum[ch[r][1]]+key[r]; L[r]=max(L[ch[r][0]],sum[ch[r][0]]+key[r]+max(0,L[ch[r][1]]));//从左开始的最大值 R[r]=max(R[ch[r][1]],sum[ch[r][1]]+key[r]+max(0,R[ch[r][0]]));//从右开始的最大值 max1[r]=max(0,R[ch[r][0]])+key[r]+max(0,L[ch[r][1]]);//右区间的从左开始加上左区间的右开始的分别最大值 max1[r]=max(max1[r],max(max1[ch[r][0]],max1[ch[r][1]]));// m[r]=key[r];// if(ch[r][0]) m[r]=min(m[r],m[ch[r][0]]);// if(ch[r][1]) m[r]=min(m[r],m[ch[r][1]]);}void pushdown(int r){ if(rev[r]){ update_rev(ch[r][0]); update_rev(ch[r][1]); rev[r]=0; } if(flag[r]!=-2016){ update_add(ch[r][0],flag[r]); update_add(ch[r][1],flag[r]); flag[r]=-2016; }}void buildtree(int &x,int l,int r,int fa){ if(l>r) return ; int mid=(l+r)>>1; newnode(x,fa,a[mid]); buildtree(ch[x][0],l,mid-1,x); buildtree(ch[x][1],mid+1,r,x); pushup(x);}void init(){ root=tot1=tot2=0; ch[root][0]=ch[root][1]=pre[root]=size[root]=0;flag[root]=-2016;rev[root]=0; L[root]=R[root]=max1[root]=-inf; key[root]=0;m[root]=inf; newnode(root,0,inf); newnode(ch[root][1],root,inf); buildtree(ch[ch[root][1]][0],1,n,ch[root][1]); pushup(ch[root][1]);pushup(root);}void Rotate(int x,int kind){ int y=pre[x]; pushdown(y);pushdown(x); ch[y][!kind]=ch[x][kind]; pre[ch[x][kind]]=y; if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[x]=pre[y]; ch[x][kind]=y;pre[y]=x; pushup(y);}void splay(int r,int goal){ pushdown(r); while(pre[r]!=goal){ if(pre[pre[r]]==goal){ pushdown(pre[r]);pushdown(r); Rotate(r,ch[pre[r]][0]==r); }else{ pushdown(pre[pre[r]]);pushdown(pre[r]); pushdown(r); int y=pre[r]; int kind=ch[pre[y]][0]==y; if(ch[y][kind]==r) Rotate(r,!kind),Rotate(r,kind); else Rotate(y,kind),Rotate(r,kind); } } pushup(r); if(goal==0) root=r;}int get_kth(int r,int k){ pushdown(r); int t=size[ch[r][0]]+1; if(t==k) return r; if(t>k) return get_kth(ch[r][0],k); else return get_kth(ch[r][1],k-t);}int get_min(int r){ pushdown(r); while(ch[r][0]){ r=ch[r][0]; pushdown(r); } return r;}int get_max(int r){ pushdown(r); while(ch[r][1]){ r=ch[r][1]; pushdown(r); } return r;}void add(int l,int r,int val){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); update_add(ch[ch[root][1]][0],val); pushup(ch[root][1]); pushup(root);}void Reverse(int l,int r){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); update_rev(ch[ch[root][1]][0]); pushup(ch[root][1]);pushup(root);}void Insert(int pos,int len){ splay(get_kth(root,pos+1),0); splay(get_kth(root,pos+2),root); buildtree(ch[ch[root][1]][0],1,len,ch[root][1]); pushup(ch[root][1]);pushup(root);}void erase(int r){ if(r){ s[++tot2]=r; erase(ch[r][0]); erase(ch[r][1]); }}void Delete(int x,int y){ splay(get_kth(root,x),0); splay(get_kth(root,x+y+1),root); erase(ch[ch[root][1]][0]);pre[ch[ch[root][1]][0]]=0; ch[ch[root][1]][0]=0; pushup(ch[root][1]);pushup(root);}int query_min(int l,int r){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); return m[ch[ch[root][1]][0]];}void pri(int x){ if(x==0) return ; pushdown(x); if(ch[x][0]) pri(ch[x][0]); if(x!=1&&x!=2) printf("%d\n",key[x]); if(ch[x][1]) pri(ch[x][1]);}void Revolve(int l,int r){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); int tmp=ch[ch[root][1]][0]; ch[ch[root][1]][0]=0; pushup(ch[root][1]);pushup(root); int len=r-l+1; len=n-len+1; splay(get_kth(root,len),0); splay(get_kth(root,len+1),root); ch[ch[root][1]][0]=tmp; pre[ch[ch[root][1]][0]]=ch[root][1]; pushup(ch[root][1]);pushup(root);}int query(int l,int r){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); return sum[ch[ch[root][1]][0]];}int query_max_sum(int l,int r){ splay(get_kth(root,l),0); splay(get_kth(root,r+2),root); return max1[ch[ch[root][1]][0]];}int main(){ int x,y,z; char op[20]; while(scanf("%d%d",&n,&q)!=-1){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); init(); while(q--){ scanf("%s",op); if(op[0]=='I'){ scanf("%d%d",&x,&y); for(int i=1;i<=y;i++) scanf("%d",&a[i]); Insert(x,y); }else if(op[0]=='D'){ scanf("%d%d",&x,&y); Delete(x,y); }else if(op[0]=='R'){ scanf("%d%d",&x,&y); Reverse(x,x+y-1); }else if(op[0]=='G'){ scanf("%d%d",&x,&y); printf("%d\n",query(x,x+y-1)); }else if(op[2]=='K'){ scanf("%d%d%d",&x,&y,&z); add(x,x+y-1,z); }else printf("%d\n",query_max_sum(1,size[root]-2)); } } return 0;}
0 0
- BZOJ 1500 splay
- bzoj 1500 splay伸展树
- BZOJ 1500 维修数列 Splay
- 【BZOJ 1500】 维修数列|Splay
- BZOJ 1500 维修序列 Splay
- BZOJ 1500 splay终结版...
- BZoj 1500 [NOI2005]维修数列 (Splay 模板)
- BZOJ 1500 NOI 2005 维修数列 Splay
- BZOJ 1500 NOI2005 维修数列 Splay
- 【BZOJ】1500 [NOI2005]维修数列 【splay】
- 【splay】BZOJ 1500: [NOI2005]维修数列
- bzoj 1500 NOI2005 维修数列 [Splay]
- bzoj 1500: [NOI2005]维修数列(splay)
- BZOJ 1500 【NOI2005 D1T2】 维修数列 Splay
- bzoj 1500 splay 【终极模板】 【pascal】
- [BZOJ 1500][NOI2005]维修数列(Splay)
- BZOJ 1500: [NOI2005]维修数列 Splay
- BZOJ 1500 [NOI2005]维修数列 Splay
- Swift学习:1.2 Swift初见
- poj1088(记忆化搜索)
- 第一周 项目0 宣告“主权”
- iOS开发:APPicon、LunchImage及LunchScreen的使用
- poj1190(搜索剪枝)
- BZOJ 1500 splay
- poj1258(最小生成树Kruscal)
- HIT1004
- 软件开发思考
- IOS定位第三方框架LocationManager-master
- 脑上访问一个网页,整个过程是怎么样的
- 第二周项目1-C/C++语言中函数参数传递的三种方式
- codeforce C. Learning Languages(并查集)
- 杨辉三角 经典问题