COJ 1002 WZJ的数据结构(二)(splay模板)
我的LCC,LCT,Splay格式终于统一起来了。。。
另外。。这个形式的Splay是标准的Splay(怎么鉴别呢?看Splay函数是否只传了一个变量node就行),刘汝佳小白书的Splay写的真是不想吐槽了,局限性太大,别学。。。
好了我要去写维修数列了。。。。。
标准Splay模板:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define CH for(int d=0;d<=1;d++) if(ch[d])
using namespace std;
const int maxn=+;
struct node{
node*ch[],*fa;
int x,siz;bool rev;char c;
node(){x=;siz=;rev=false;}
void revt(){swap(ch[],ch[]);rev^=;return;}
inline void down(){
if(rev){CH{ch[d]->revt();}rev=false;}return;
}
inline void update(){
siz=;CH{siz+=ch[d]->siz;}return;
}
}Splay[maxn],*root=&Splay[];int cnt=;void print(node*x);
inline int parent(node*x,node*&y){return (y=x->fa)?y->ch[]==x?:y->ch[]==x?:-:-;}
inline void rotate(node*x){
node*y,*z;int d1=parent(x,y),d2=parent(y,z);
if(y->ch[d1]=x->ch[d1^]) y->ch[d1]->fa=y;
y->fa=x;x->fa=z;x->ch[d1^]=y;
if(d2!=-) z->ch[d2]=x;
y->update();return;
}
void pushdown(node*x){
static node*s[maxn];int top=;
for(node*y;;x=y){
s[top++]=x;y=x->fa;
if(!y||(y->ch[]!=x&&y->ch[]!=x)) break;
} while(top--) s[top]->down();return;
}
node*splay(node*x){
pushdown(x);node*y,*z;int d1,d2;
while(true){
if((d1=parent(x,y))<) break;
if((d2=parent(y,z))<){rotate(x);break;}
if(d1==d2) rotate(y),rotate(x);
else rotate(x),rotate(x);
} x->update();return x;
}
node*find(node*x,int rank){
x->down();int kth=; if(x->ch[]) kth=x->ch[]->siz+;
if(kth==rank) return x;
if(kth>rank) return find(x->ch[],rank);
else return find(x->ch[],rank-kth);
}
void split(node*&x,node*&y,int a){//
if(!a){y=x;x=NULL;return;}
x=splay(find(x,a));
y=x->ch[];x->ch[]=NULL;x->update();return;
}
void split(node*&x,node*&y,node*&z,int a,int b){//
split(x,z,b);split(x,y,a-);
return;
}
void join(node*&x,node*y){
if(!x){x=y;return;}
if(!y) return;
x=splay(find(x,x->siz));
x->ch[]=y;y->fa=x;x->update();return;
}
void join(node*&x,node*y,node*z){
join(y,z);join(x,y);return;
}
void reverse(int a,int b){
node*x,*y;split(root,x,y,a,b);x->revt();join(root,x,y);return;
}
char s[maxn];
void build(node*&x,int L,int R){
if(L>R) return;x=&Splay[++cnt];
int M=L+R>>;x->c=s[M];
build(x->ch[],L,M-);build(x->ch[],M+,R);
if(x->ch[]) x->ch[]->fa=x;
if(x->ch[]) x->ch[]->fa=x;
x->update();return;
}
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*sig;
}
void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void printer(node*x){
if(!x) return;
x->down();
printer(x->ch[]);
putchar(x->c);
printer(x->ch[]);
return;
}
void init(){
scanf("%s",s);
build(root,,strlen(s)-);
int Q=read(),L,R;
while(Q--){L=read();R=read();reverse(L,R);}
printer(root);
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
刘汝佳的Splay跑得虽然快但局限性太大:
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
using namespace std;
inline void read(int& x)
{
x=; char ch=getchar();int sig=;
while(!isdigit(ch)) {if(ch=='-') sig=-;ch=getchar();}
while(isdigit(ch)) x=x*+ch-'',ch=getchar();
x*=sig;
}
const int maxn=;
struct Node
{
Node* ch[];
int s,flip;
char c;
int cmp(int k)
{
if(k==ch[]->s+) return -;
return k<=ch[]->s?:;
}
void maintain(){s=ch[]->s+ch[]->s+;}
void pushdown()
{
if(flip)
{
ch[]->flip^=;ch[]->flip^=;
swap(ch[],ch[]);
flip=;
}
}
}*null=new Node(),nodes[maxn];
int tot;
void rotate(Node* &o,int d)
{
Node* k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
o->maintain();k->maintain();o=k;
}
void splay(Node* &o,int k)
{
o->pushdown();
int d=o->cmp(k);
if(d) k-=o->ch[]->s+;
if(d!=-)
{
Node* p=o->ch[d];p->pushdown();
int d2=p->cmp(k);
int k2=d2?k-p->ch[]->s-:k;
if(d2!=-)
{
splay(p->ch[d2],k2);
if(d==d2) rotate(o,d^);
else rotate(o->ch[d],d);
}
rotate(o,d^);
}
}
void print(Node* &o)
{
if(o==null) return;
o->pushdown();
print(o->ch[]);
printf("%c",o->c);
print(o->ch[]);
}
char s[maxn];
void build(Node* &o,int L,int R)
{
o=null;
if(L>R) return;
int M=L+R>>;
o=&nodes[tot++];
o->c=s[M];o->flip=;
build(o->ch[],L,M-);build(o->ch[],M+,R);
o->maintain();
}
Node *root,*o,*left,*mid,*right;
void merge(Node* &left,Node* &right)
{
if(left==null) left=right;
else
{
splay(left,left->s);
left->ch[]=right;
left->maintain();
}
}
void split(Node* &o,Node* &left,Node* &right,int k)
{
if(!k) left=null,right=o;
else
{
splay(o,k);
left=o;
right=left->ch[];
left->ch[]=null;
left->maintain();
}
}
int main()
{
scanf("%s",s);
build(root,,strlen(s)-);
int Q,L,R;
read(Q);
while(Q--)
{
read(L),read(R);
split(root,left,o,L-);
split(o,mid,right,R-L+);
mid->flip^=;
merge(left,mid);
merge(left,right);
root=left;
}
print(root);
return ;
}
搜索
复制
COJ 1002 WZJ的数据结构(二)(splay模板)的更多相关文章
- COJ 0979 WZJ的数据结构(负二十一)
WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...
- COJ 0981 WZJ的数据结构(负十九)树综合
WZJ的数据结构(负十九) 难度级别:E: 运行时间限制:3500ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 WZJ的数据结构中有很多都是关于树的.这让很多练习 ...
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- COJ 1007 WZJ的数据结构(七) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...
- COJ 0999 WZJ的数据结构(负一)
WZJ的数据结构(负一) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 输入N个模板串Pi和文本串T,输出每个模板串Pi在T ...
- COJ 0967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0990 WZJ的数据结构(负十)
WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...
- COJ 1003 WZJ的数据结构(三)ST表
WZJ的数据结构(三) 难度级别:B: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为N的 ...
随机推荐
- raknet unity3d
Raknet是一高性能的跨平台的网络库. 他主要基于UDP实现,性能非常好,能够做server. 鉴于unity3d较差的网络支持. 本人成功实现了raknet c# for unity3d的使用,s ...
- Linux 内核的文件 Cache 管理机制介绍-ibm
https://www.ibm.com/developerworks/cn/linux/l-cache/ 1 前言 自从诞生以来,Linux 就被不断完善和普及,目前它已经成为主流通用操作系统之一,使 ...
- C++笔试题库-------Coding整理
1. 反转字符串 char* strrev1(const char* str) { int len = strlen(str); ]; char *p = temp + len; *p = '\0'; ...
- 学习《Spring 3.x 企业应用开发实战》Day-1
Day-1 记录自己学习spring的笔记 提要:根据<Spring 3.x 企业应用开发实战>开头一个用户登录的例子,按照上面敲的. 1.项目分层
- Android 开源项目 eoe 社区 Android 客户端(转)
本文内容 环境 开源项目 eoe 社区 Android 客户端 本文介绍 eoe 社区 Android 客户端.它是一个开源项目,功能相对简单,采用侧边菜单栏.可以学习一下.点击此处查看 GitHub ...
- checkbox 删除
先创建del.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...
- HDU 3533 Escape(bfs)
Escape Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 新版本ButterKnife的配置
新版本的ButterKnife的添加方式发生了变化,然后摸索着记录了一下. 按照ButterKnife的官网描述,使用ButterKnife需要在Gradle中添加如下依赖: compile 'com ...
- 9.2noip模拟试题
题目名称 改造二叉树 数字对 交换 英文名称 binary pair swap 输入文件名 binary.in pair.in swap.in 输出文件名 binary.out pair.out ...
- Mac下Sublime快捷键
由于自己笔记本是mac,造成window与mac中sublime快捷键不同,现在稍微整理下常用的方便于记忆: 1.control+alt+enter 打开Emmet(Zencoding) 2.supe ...