hdu-3487-Play with Chain-(splay 区间翻转,切割,插入)
题意:
区间翻转,切割,插入
- // File Name: ACM/HDU/3487.cpp
- // Author: Zlbing
- // Created Time: 2013年08月10日 星期六 21时35分28秒
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<cstdlib>
- #include<cstdio>
- #include<set>
- #include<map>
- #include<vector>
- #include<cstring>
- #include<stack>
- #include<cmath>
- #include<queue>
- using namespace std;
- #define CL(x,v); memset(x,v,sizeof(x));
- #define INF 0x3f3f3f3f
- #define LL long long
- #define REP(i,r,n) for(int i=r;i<=n;i++)
- #define RREP(i,n,r) for(int i=n;i>=r;i--)
- #define L ch[x][0]
- #define R ch[x][1]
- #define KT (ch[ ch[rt][1] ][0])
- const int MAXN = 3e5+;
- struct SplayTree {
- int sz[MAXN];
- int ch[MAXN][];
- int pre[MAXN];
- int rt,top;
- inline void down(int x){
- if(flip[x]) {
- flip[ L ] ^= ;
- flip[ R ] ^= ;
- swap(L,R);
- flip[x]=;
- }
- }
- inline void up(int x){
- sz[x]=+sz[ L ] + sz[ R ];
- }
- inline void Rotate(int x,int f){
- int y=pre[x];
- down(y);
- down(x);
- ch[y][!f] = ch[x][f];
- pre[ ch[x][f] ] = y;
- pre[x] = pre[y];
- if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
- ch[x][f] = y;
- pre[y] = x;
- up(y);
- }
- inline void Splay(int x,int goal){//将x旋转到goal的下面
- down(x);////防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了
- while(pre[x] != goal){
- down(pre[pre[x]]); down(pre[x]);down(x);//在旋转之前需要先下传标记,因为节点的位置可能会发生改变
- if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
- else {
- int y=pre[x],z=pre[y];
- int f = (ch[z][]==y);
- if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
- else Rotate(y,f),Rotate(x,f);
- }
- }
- up(x);
- if(goal==) rt=x;
- }
- inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
- int x=rt;
- down(x);
- while(sz[ L ]+ != k) {
- if(k < sz[ L ] + ) x=L;
- else {
- k-=(sz[ L ]+);
- x = R;
- }
- down(x);
- }
- Splay(x,goal);
- }
- void vist(int x){
- if(x){
- printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d flip:%d\n",x,L,R,val[x],flip[x]);
- vist(L);
- vist(R);
- }
- }
- void Newnode(int &x,int c,int f){
- x=++top;flip[x]=;
- L = R = ; pre[x] = f;
- sz[x]=; val[x]=c;
- }
- inline void build(int &x,int l,int r,int f){
- if(l>r) return ;
- int m=(l+r)>>;
- Newnode(x,m,f);
- build(L , l , m- , x);
- build(R , m+ , r , x);
- pre[x]=f;
- up(x);
- }
- inline void init(int n){
- ch[][]=ch[][]=pre[]=sz[]=;
- rt=top=; flip[]=; val[]=;
- Newnode(rt,-,);
- Newnode(ch[rt][],-,rt);
- sz[rt]=;
- build(KT,,n,ch[rt][]);
- }
- void Del(){
- int t=rt;
- if(ch[rt][]) {
- rt=ch[rt][];
- RTO(,);
- ch[rt][]=ch[t][];
- if(ch[rt][]) pre[ch[rt][]]=rt;
- }
- else rt=ch[rt][];
- pre[rt]=;
- up(rt);
- }
- void solve1(int a,int b,int c)
- {
- RTO(a,);
- RTO(b+,rt);
- int tmp=KT;
- KT=;
- up(ch[rt][]);
- up(rt);
- RTO(c+,);
- RTO(c+,rt);
- KT=tmp;
- pre[tmp]=ch[rt][];
- up(ch[rt][]);
- up(rt);
- }
- void solve2(int a,int b)
- {
- RTO(a,);
- RTO(b+,rt);
- flip[KT]^=;
- }
- void print(int x)
- {
- if(x)
- {
- down(x);
- print(L);
- if(val[x]!=-)
- {
- if(flag)printf(" ");
- flag=;
- printf("%d",val[x]);
- }
- print(R);
- }
- }
- void out()
- {
- flag=;
- print(rt);
- printf("\n");
- }
- int flip[MAXN];
- int val[MAXN];
- int flag;
- }spt;
- int main()
- {
- int n,m;
- while(~scanf("%d%d",&n,&m))
- {
- if(n==-)break;
- char op[];
- int a,b,c;
- spt.init(n);
- REP(i,,m)
- {
- scanf("%s",op);
- if(op[]=='C')
- {
- scanf("%d%d%d",&a,&b,&c);
- spt.solve1(a,b,c);
- }
- else
- {
- scanf("%d%d",&a,&b);
- spt.solve2(a,b);
- }
- }
- spt.out();
- }
- return ;
- }
hdu-3487-Play with Chain-(splay 区间翻转,切割,插入)的更多相关文章
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- HDU 3487 Play with Chain 【Splay】
1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...
- hdu 3487 Play with Chain
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...
- hdu-1890-Robotic Sort splay区间翻转
题意: 依次找第i大的数下标pos[i],然后将区间[i,pos[i]]翻转 分析: splay树区间翻转 // File Name: ACM/HDU/1890.cpp // Author: Zlbi ...
- bzoj 3223 文艺平衡树 splay 区间翻转
Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17715 Solved: 7769[Submit][Status][ ...
- bzoj 1251序列终结者 splay 区间翻转,最值,区间更新
序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4594 Solved: 1939[Submit][Status][Discuss] De ...
- Hdu 3487 play the chain
Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...
- BZOJ 3223 Splay区间翻转
思路: 区间翻转的裸题 终于tm理解splay了-- //By SiriusRen #include <cstdio> #include <cstring> #include ...
- splay区间翻转
原题P3391 [模板]文艺平衡树(Splay) 题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: ...
随机推荐
- 惠普 hpssacli 工具使用
查看raid卡信息(包括控制器状态.Cache状态.电池状态) # hpssacli ctrl all show status 查看raid详细信息 # hpssacli ctrl slot=0 sh ...
- Oracler读取各种格式的相关日期格式
CREATE OR REPLACE Package Pkg_Stm_Date As --Purpose:相关日期处理功能包 --获取某一天是第几周 Function ...
- mvc Action上面加 [HttpPost]
mvc Action上面加 [HttpPost] 意思就是这个action只能响应post请求. 如果发get请求这里是没有响应的
- Oracle --1536错误解决(超出表空间)
--导入数据库时提示 超出表空间限额,1536错误,解决方法:去除限额. 执行:--alter user username quota unlimited on users; 例: alter use ...
- 怎样使用svn开发项目
那么首先什么是svn呢?官方有很好的解释,我说一下个人简单的理解,svn就是开源的版本控制软件, 那么什么是版本呢?简单的说版本就是标记,比如你买了一本书,同样的书名,但是版本不一定一样, 因为里面可 ...
- CSS Clip剪切元素动画实例
1.CSS .fixed { position: fixed; width: 90px; height: 90px; background: red; border: 0px solid blue; ...
- 简单讲解iOS应用开发中的MD5加密的相关使用<转>
这篇文章主要介绍了iOS应用开发中的MD5加密的相关使用,示例代码基于传统的Objective-C,需要的朋友可以参考下 一.简单说明 1.说明 在开发应用的时候,数据的安全性至关重要,而仅仅用POS ...
- C#堆栈原理(我有两个例子测试你到底会不会)
背景 上次写了一篇文章关于try finnally的一些疑问(被我用windows live覆盖了,草),后来经过大神们解释,我明白了在我理解了try.finnally运行原理后,还欠缺的就是关于值类 ...
- c++面试(一)
1.在c++中可以通过"::"来直接操作全局变量. 2.i++与++i效率的比较. (1)內建数据类型时,他们的效率差别不大. (2)自定义数据类型(类等)的情况,(++i)可以返 ...
- 赋值,copy和deepcopy
python的复制,拷贝,和深拷贝. >>> a=[23,3]>>> b=a>>> b.append(234)>>> a[23, ...