BZOJ 1500 Splay 全操作
好久没写splay了,写一发(写了一节课,调了一节课)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#define mp make_pair
#define pa pair<int,int>
#define pb push_back
#define fi first
#define se second
#define Key Root->ch[1]->ch[0]
using namespace std;
inline void Get_Int(int &x)
{
x=; register char ch=getchar(); int f=;
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();} x*=f;
}
inline void Put_Int(int x)
{
char ch[]; register int top=;
if (x<) putchar('-'),x=-x;
if (x==) ch[++top]='';
while (x) ch[++top]=x%+'',x/=;
while (top) putchar(ch[top--]); putchar('\n');
}
inline int Max(int x,int y) {return x>y?x:y;}
inline int Max3(int x,int y,int z) {return Max(x,Max(y,z));}
inline void Swap(int &x,int &y) {int t=x;x=y;y=t;}
inline bool StrCmp(char S1[],char S2[]) {for (int i=;i<;i++) if (S1[i]!=S2[i]) return false; return true;}
//=============================================
const int Maxn=;
const int Inf=0x3f3f3f3f;
int a[Maxn],pos,tot,c,n,m,Point;
char str[];
struct Node
{
int lx,rx,mx,sum,size,key,rev,same;
Node * pre,* ch[];
inline int d() {return this->pre->ch[]==this;}
inline void Setc(Node * r,int d) {r->pre=this; this->ch[d]=r;}
};
inline void Swap(Node *&x,Node *&y) {Node * T=x;x=y;y=T;}
Node Memory[Maxn],* port=Memory,* tmp[Maxn],* Bin[Maxn],* null=port++,* Root;
inline Node * NewNode(Node * f,int v)
{
Node * ret;
if (Point) ret=Bin[Point--]; else ret=port++;
ret->ch[]=ret->ch[]=null;
ret->size=,ret->pre=f;
ret->key=ret->lx=ret->rx=ret->mx=ret->sum=v;
ret->same=ret->rev=;
return ret;
}
inline void Get_Rev(Node * x)
{
if (x==null) return;
Swap(x->ch[],x->ch[]);
Swap(x->lx,x->rx);
x->rev^=;
}
inline void Get_Same(Node * x,int v)
{
if (x==null) return;
x->key=v;
x->sum=x->size*v;
x->lx=x->rx=x->mx=Max(v,x->size*v);
x->same=;
}
inline void Push_Up(Node * x)
{
if (x==null) return;
x->size=x->ch[]->size+x->ch[]->size+;
x->sum=x->ch[]->sum+x->ch[]->sum+x->key;
x->lx=Max(x->ch[]->lx,x->ch[]->sum+x->key+Max(x->ch[]->lx,));
x->rx=Max(x->ch[]->rx,x->ch[]->sum+x->key+Max(x->ch[]->rx,));
x->mx=Max3(x->ch[]->mx,x->ch[]->mx,Max(x->ch[]->rx,)+x->key+Max(x->ch[]->lx,));
}
inline void Push_Down(Node * x)
{
if (x==null) return;
if (x->rev)
{
Get_Rev(x->ch[]);
Get_Rev(x->ch[]);
x->rev=;
}
if (x->same)
{
Get_Same(x->ch[],x->key);
Get_Same(x->ch[],x->key);
x->same=;
}
}
Node * Build(int l,int r,Node * f)
{
if (l>r) return null;
int mid=(l+r)>>;
Node * ret=NewNode(f,a[mid]);
ret->ch[]=Build(l,mid-,ret);
ret->ch[]=Build(mid+,r,ret);
Push_Up(ret);
return ret;
}
inline void Init()
{
null->ch[]=null->ch[]=null->pre=null;
null->size=null->key=null->sum=null->same=null->rev=;
null->lx=null->rx=null->mx=-Inf;
Root=NewNode(null,-);
Root->ch[]=NewNode(Root,-);
for (int i=;i<=n;i++) Get_Int(a[i]);
Key=Build(,n,Root->ch[]);
Push_Up(Root->ch[]),Push_Up(Root);
}
//=========================================================
inline void Rotate(Node * x)
{
Node * y=x->pre; int d=x->d();
y->pre->Setc(x,y->d());
y->Setc(x->ch[!d],d);
x->Setc(y,!d);
Push_Up(y);
}
void Splay(Node * x,Node * Goal)
{
int s=; tmp[]=x; Node * r=x;
while (r->pre!=null) tmp[++s]=r=r->pre;
while (s) Push_Down(tmp[s--]);
while (x->pre!=Goal)
if (x->pre->pre==Goal) Rotate(x); else
(x->pre->d()==x->d())?(Rotate(x->pre),Rotate(x)):(Rotate(x),Rotate(x));
Push_Up(x);
if (Goal==null) Root=x;
}
//===========================================================
Node * Get_K(Node * x,int k)
{
Push_Down(x);
int t=x->ch[]->size+;
if (t==k) return x;
if (t>k) return Get_K(x->ch[],k);
else return Get_K(x->ch[],k-t);
}
inline void Handle(int pos,int tot)
{
Splay(Get_K(Root,pos),null);
Splay(Get_K(Root,pos+tot+),Root);
}
inline void Insert()
{
Get_Int(pos),Get_Int(n);
Handle(pos+,);
for (int i=;i<=n;i++) Get_Int(a[i]);
Key=Build(,n,Root->ch[]);
Push_Up(Root->ch[]),Push_Up(Root);
}
void Erase(Node * x)
{
if (x==null) return;
Erase(x->ch[]),Erase(x->ch[]);
Bin[++Point]=x;
}
inline void Delete()
{
Get_Int(pos),Get_Int(tot);
Handle(pos,tot);
Erase(Key); Key->pre=null; Key=null;
Push_Up(Root->ch[]),Push_Up(Root);
}
inline void Same()
{
Get_Int(pos),Get_Int(tot),Get_Int(c);
Handle(pos,tot);
Get_Same(Key,c);
Push_Up(Root->ch[]),Push_Up(Root);
}
inline void Rev()
{
Get_Int(pos),Get_Int(tot);
Handle(pos,tot);
Get_Rev(Key);
Push_Up(Root->ch[]),Push_Up(Root);
}
inline void Sum()
{
Get_Int(pos),Get_Int(tot);
Handle(pos,tot);
Put_Int(Key->sum);
}
inline void MaxSum()
{
Handle(,Root->size-);
Put_Int(Key->mx);
}
void InOrder(Node * x)
{
if (x==null) return;
Push_Down(x);
InOrder(x->ch[]);
printf("%d ",x->key);
InOrder(x->ch[]);
} int main()
{
Get_Int(n),Get_Int(m);
Init();
for (int i=;i<=m;i++)
{
scanf("%s",str);
if (StrCmp(str,"INSERT")) Insert();
if (StrCmp(str,"DELETE")) Delete();
if (StrCmp(str,"MAKE-SAME")) Same();
if (StrCmp(str,"REVERSE")) Rev();
if (StrCmp(str,"GET-SUM")) Sum();
if (StrCmp(str,"MAX-SUM")) MaxSum();
}
return ;
}
傻逼200+系列..
BZOJ 1500 Splay 全操作的更多相关文章
- BZOJ 1500 splay终结版...
GSS系列有一丝丝像- 只不过那个是线段树 这个是splay 翻转 插入 删除啥的就是普通的splay 合在一起了而已 //By SiriusRen #include <cstdio> # ...
- [BZOJ 1500] [NOI2005] 维修数列
题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...
- [BZOJ 1500] 维护序列
Link: BZOJ 1500 传送门 Solution: 可能平衡树维护序列的所有操作都在这了吧…… 对序列的维护$fhq treap$和$Splay$都能做 有几个注意点: 1.维护序列时始终记得 ...
- C语言链表全操作(增,删,改,查,逆序,递增排序,递减排序,链式队列,链式栈)
一,数据结构——链表全操作: 链表形式: 其中,每个节点(Node)是一个结构体,这个结构体包含数据域,指针域,数据域用来存放数据,指针域则用来指向下一个节点: 特别说明:对于单链表,每个节点(Nod ...
- BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】
以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...
- ROS tab键补全操作出现错误
ros tab键补全操作出现错误如下: $ roslaunch sp[rospack] Warning: error while crawling /home/hemudu: boost::files ...
- 机器学习进阶-图像基本操作-边界补全操作 1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 进行边界的补零操作 2.cv2.BORDER_REPLICATE(边界补零复制操作)...
1.cv2.copyMakeBoder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE) 参数说明: i ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- P2596 [ZJOI2006]书架 && Splay 区间操作(三)
P2596 [ZJOI2006]书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书, ...
随机推荐
- EventBus使用详解(一)——初步使用EventBus
一.概述 EventBus是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间 ...
- JAVA线程同步辅助类CountDownLatch
一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达 ...
- 在用busybox制作系统过程中遇到的问题
遇到的问题: 1.开机报错: 在做完整个系统之后重启出现了这个报错 VFS: Cannot open root device "sda2" or unknown-block(0,0 ...
- Spring RabbitMq
spring-rabbitmq.xml文件内容如下: <?xml version="1.0" encoding="UTF-8"?><beans ...
- 彻底删除java*
提示:先备份重要数据 1. 移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ): apt-get update apt-cache se ...
- ios 模拟器不显示系统版本了,后边都是 uuid 了,怎么弄回来?系统升级xcode6.4,模拟器找不到选择了?
当我用El Capitan Beta 下 Xcode6.4版本时候出现了问题 常用的Scheme 选择版本不见了 而在Xcode 7.0 beta 6中显示有 简直就是坑,经过查资料其实是一个bug ...
- (转) linux之sort用法
sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件] 补充说明:sort可针对文本文件的内容,以行为单位来排序. 参 数 ...
- Monkey环境配置
安卓APP想要测试稳定性,monkey是最佳选则. 首先搭建monkey的运行环境 在Windows下基于SDK 1.下载SDK for Windows 解压:android-sdk-windows ...
- JavaWeb基础: ServletContext
基本概念 Web容器在启动时,会为每个Web应用程序都创建一个对应的ServletContext对象,它代表当前Web应用. ServletContext(javax.servlet.http.Ser ...
- Add two numbers [LeetCode]
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...