bzoj1251
来源:互联网 发布:灰色模型预测软件 编辑:程序博客网 时间:2024/06/16 09:21
splay大模板题。。。调了好久。。。简直智障
#include<iostream>#include<cstdio>#define inf 0x7fffffff#define Abs(x) ((x) > 0 ? (x) : (-(x))) #define forup(i,a,b) for(int i=(a);i<=(b);i++) #define fordown(i,a,b) for(int i=(a);i>=(b);i--) using namespace std;inline int read(){ int 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*10+ch-'0';ch=getchar();} return x*f;}int n,m,cnt;int fa[50005],ch[50005][2],id[50005];int tag[50005],val[50005],mx[50005],siz[50005];bool rev[50005];void pushup(int node){ int l=ch[node][0],r=ch[node][1]; int k=val[node]; siz[node]=1; if(l) { k=max(k,mx[l]);siz[node]+=siz[l];} if(r) {k=max(k,mx[r]);siz[node]+=siz[r];} mx[node]=k;}void pushdown(int node){ if(rev[node]) {rev[node]=0; swap(ch[node][1],ch[node][0]); rev[ch[node][1]]^=1;rev[ch[node][0]]^=1; }tag[ch[node][0]]+=tag[node];tag[ch[node][1]]+=tag[node];val[ch[node][0]]+=tag[node];val[ch[node][1]]+=tag[node];mx[ch[node][0]]+=tag[node];mx[ch[node][1]]+=tag[node]; tag[node]=0;}void build(int l,int r){ if(l==r) { siz[cnt]=1;id[cnt]=l;return;} int mid=(l+r)>>1;id[cnt]=mid; siz[cnt]=1; int last=cnt; if(mid>l) {cnt++;ch[last][0]=cnt;fa[cnt]=last;build(l,mid-1);} if(mid<r) {cnt++;ch[last][1]=cnt;fa[cnt]=last;build(mid+1,r);} pushup(last);}int s[50005];void rotate(int x){ int y=fa[x],z=fa[y],l,r; l=(ch[y][0]==x)?0:1;r=l^1; if(ch[z][0]==y) ch[z][0]=x; else ch[z][1]=x; fa[x]=z; fa[y]=x; fa[ch[x][r]]=y; ch[y][l]=ch[x][r];ch[x][r]=y; pushup(y);pushup(x);}int splay(int root,int x){ int top = 0;for(int i = x; fa[i]!=root; i = fa[i]) s[++top] = i;for(int i = top; i >= 1; i--) pushdown(s[i]);while(fa[x]!=root){ int y=fa[x],z=fa[y]; if(z!=root) { if((ch[y][0]==x)^(ch[z][0]==y)) rotate(x); else rotate(y); } rotate(x); } }int find(int node,int rk){ pushdown(node); if(1+siz[ch[node][0]]==rk) return node; if(1+siz[ch[node][0]]>rk) return find(ch[node][0],rk); return find(ch[node][1],rk-1-siz[ch[node][0]]); }int shaobin(int l,int r){ int p=find(ch[0][0],l); splay(0,p); int k=find(ch[0][0],r+2); splay(0,k);//这句话本来没加。。后来想到自己写的是双旋啊。。。不加肯定错啊 splay(k,p); return ch[p][1];}int main() {//freopen("1.in","r",stdin); n=read();m=read(); cnt=1; ch[0][0]=1;fa[1]=0; build(0,n+1); for(int i=1;i<=m;i++) {int f=read(),l,r,v;switch(f){int root;case 1:l=read();r=read();v=read();root=shaobin(l,r);tag[root]+=v;mx[root]+=v;val[root]+=v;break;case 2:l=read();r=read(); root=shaobin(l,r);rev[root]^=1;break;case 3:l=read();r=read();int x=shaobin(l,r);int top=0;for(int i= x; fa[i]!=0; i = fa[i]) s[++top] = i;for(int i = top; i >= 1; i--) pushdown(s[i]);printf("%d\n",mx[x]);break;} } return 0;}
0 0
- bzoj1251
- bzoj1251 -- splay
- [splay][bzoj1251]
- bzoj1251 序列终结者
- [BZOJ1251] 序列终结者
- [BZOJ1251]序列终结者
- [BZOJ1251]序列终结者
- BZOJ1251序列终结者
- bzoj1251 序列终结者
- bzoj1251 序列终结者
- BZOJ1251: 序列终结者
- bzoj1251: 序列终结者
- 【BZOJ1251】序列终结者
- BZOJ1251 序列终结者
- 【BZOJ1251】【splay】序列终结者
- splay bzoj1251序列终结者
- [Bzoj1251]序列终结者 (Splay)
- 【bzoj1251】序列终结者 Splay
- PsychoPy入门_00_简介
- 递归实现和非递归实现中序遍历二叉树
- 数组与链表的优缺点
- 实现K歌字幕效果
- Leetcode 26 Remove Duplicates from Sorted Array
- bzoj1251
- php 获取当年日期中的年份
- 【华为OJ】iNOC产品部-杨辉三角的变形
- JQuery模板示例
- C++学习笔记(五)--头文件的书写,new的四种用法,名称空间的用法
- org.hibernate.hql.internal.ast.QuerySyntaxException: appUser is not mapped
- 思维导图总结-多态
- 我的第一篇博客
- ASP.NET设置404页面返回302HTTP状态码的解决方法