HDU 3487 Play with Chain | Splay
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position. For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8
He wants to know what the chain looks like after perform m operations. Could you help him?
CUT
FLIP
- -
【Sample Output】
【题意】
给出一列数,然后对整个数列执行两种操作:切下一段插入到另外的位置,或者把其中的一整段整个翻转一下。
求经过一系列操作之后,数列最后的样子。
【分析】
数据范围最高能够到达3e5那么大,因此算法至少要是O(nlogn)复杂度以下才可能达到要求。
考虑采用Splay解决(这样的题目只能用这种动态维护的树结构不是么?)
初始先建树,把1~n加入Splay树。由于数列在后面是要被打乱顺序的,Splay二叉平衡树的性质只有在初始的时候是被保持的,之后是靠size,即每个点在中序遍历中的位置来维护。最后输出数列则只需要中序遍历一遍即可。
切割操作:若要切下a~b段,则把第a-1个结点移到根,把第b+1个结点移到根以下(即跟的右子树),则整个a~b段就落在b+1的左子树上,切出来。插入到c的时候,将c移到根,c+1移到根的右子树,则切出来的插入到c+1的左子树即可
翻转操作:用上面相同的方法把a~b整合到一棵子树上,然后可以参考线段树标记的方法,通过标记来完成访问结点的翻转等操作。
具体可以在纸上模拟一下......
【教训】
教训还是比较惨痛的...卡在这道题上好久了。
首先是输入输出以后要特别注意结尾方式,两个负数结尾还是两个-1结尾
把各种可能出现的不同情况考虑完整
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU3487
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define MAXN 300010 int sons[MAXN][];
int father[MAXN],size[MAXN],data[MAXN],list[MAXN];
bool flag[MAXN];
int spt=,spttail=; void down(int x)
{
if (flag[x])
{
flag[x]=;
swap(sons[x][],sons[x][]);
flag[sons[x][]]^=;
flag[sons[x][]]^=;
}
} void rotate(int x,int w) //rotate(node,0/1)
{
int y=father[x];
down(y);down(x);
sons[y][!w]=sons[x][w];
if (sons[x][w]) father[sons[x][w]]=y; father[x]=father[y];
if (father[y]) sons[father[y]][y==sons[father[y]][]]=x; sons[x][w]=y;
father[y]=x; size[x]=size[y];
size[y]=size[sons[y][]]+size[sons[y][]]+;
} void splay(int x,int y) //splay(node,position)
{
down(x);
while(father[x]!=y)
{
if (father[father[x]]==y) rotate(x,x==sons[father[x]][]);
else
{
int t=father[x];
int w=(sons[father[t]][]==t);
if (sons[t][w]==x)
{
rotate(x,!w);
rotate(x,w);
} else
{
rotate(t,w);
rotate(x,w);
}
}
}
if (!y) spt=x;
} void select(int x,int v,int p) //select(root,k,position)
{
down(x);
while(v!=size[sons[x][]]+)
{
if (v<=size[sons[x][]])
{
x=sons[x][];
down(x);
}
else
{
v-=size[sons[x][]]+;
x=sons[x][];
down(x);
}
}
splay(x,p);
} bool done=false; void outp(int x)
{
down(x);
if (sons[x][]) outp(sons[x][]);
if (done) printf(" ");
done=true;
printf("%d",data[x]);
if (sons[x][]) outp(sons[x][]);
} void maketree(int l,int r)
{
spttail++;
int now=spttail,w=(l+r)/,ls=,rs=;
data[now]=w;
flag[now]=false;
sons[now][]=;
sons[now][]=; if (l<=w-)
{
ls=spttail+;
sons[now][]=ls;
father[ls]=now;
maketree(l,w-);
}
if (w+<=r)
{
rs=spttail+;
sons[now][]=rs;
father[rs]=now;
maketree(w+,r);
} size[now]=size[ls]+size[rs]+;
} int main()
{
freopen("3487.txt","r",stdin); int n,m;
scanf("%d%d",&n,&m);
while(!(n<&&m<))
{
spt=;
spttail=;
father[]=;
maketree(,n); for (int i=;i<=m;i++)
{
char s[];
scanf("%s",&s);
if (s[]=='C')
{
int a,b,c,temp;
scanf("%d%d%d",&a,&b,&c); if (a>)
{
select(spt,a-,);
if (b<n)
{
select(spt,b+,spt);
temp=sons[sons[spt][]][];
sons[sons[spt][]][]=;
size[spt]-=size[temp];
size[sons[spt][]]-=size[temp];
} else
{
temp=sons[spt][];
sons[spt][]=;
size[spt]-=size[temp];
}
} else
{
if (b<n)
{
select(spt,b+,);
temp=sons[spt][];
sons[spt][]=;
size[spt]-=size[temp];
} else temp=spt;
} if (c>)
{
select(spt,c,);
if (c==size[spt])
{
sons[spt][]=temp;
father[temp]=spt;
size[spt]+=size[temp];
} else
{
select(spt,c+,spt);
sons[sons[spt][]][]=temp;
father[temp]=sons[spt][];
size[spt]+=size[temp];
size[sons[spt][]]+=size[temp];
}
} else
{
if (spt!=temp)
{
select(spt,,);
sons[spt][]=temp;
father[temp]=spt;
size[spt]+=size[temp];
}
}
} else
{
int a,b,temp;
scanf("%d%d",&a,&b);
if (a>)
{
select(spt,a-,);
if (b<n)
{
select(spt,b+,spt);
temp=sons[sons[spt][]][];
} else
{
temp=sons[spt][];
}
} else
{
if (b<n)
{
select(spt,b+,);
temp=sons[spt][];
} else temp=spt;
}
flag[temp]^=;
}
}
done=false;
outp(spt);
printf("\n");
scanf("%d%d",&n,&m);
} return ;
}
HDU 3487 Play with Chain | Splay的更多相关文章
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- 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 3487 Play with Chain (splay tree)
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 3487 Play with Chain 【Splay】
1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...
- Hdu 3487 play the chain
Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...
- HDU 3487:Play with Chain(Splay)
http://acm.hdu.edu.cn/showproblem.php?pid=3487 题意:有两种操作:1.Flip l r ,把 l 到 r 这段区间 reverse.2.Cut a b c ...
- 【HDU 3487】Play with Chain Splay
题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...
- Play with Chain 【HDU - 3487】【Splay+TLE讲解】
题目链接 很好的一道题,用了三天多的时间,终于知道了我为什么T的原因,也知道了在Splay的同时该怎样子的节约时间,因为Splay本身就是大常数的O(N*logN),我们如果不在各种细节上节约时间,很 ...
- HDU 3487 Splay tree
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- javacript没有多维数组只能模拟?
为什么说javacript没有多维数组只能模拟?但是我看到是可以实现多维数组的啊!这是为什么呢?下面我们来实验下. <!DOCTYPE html> <html lang=" ...
- 执行 npm run update-webdriver 提示文件不能获取错误
按照angularjs官网的入门教程中输入 npm run update-webdriver 总是提示 https://chromedriver.storage.googleapis.com/2.1 ...
- websphere安装验证报错 忘记密码的配置
http://blog.csdn.net/yulimin/article/details/4048897 ADMU7704E: 在尝试启动与服务器相关联的 Windows 服务时失败:server1: ...
- 欧几里得算法求最大公约数(gcd)
关于欧几里得算法求最大公约数算法, 代码如下: int gcd( int a , int b ) { if( b == 0 ) return a ; else gcd( b , a % b ) ; } ...
- 如何禁用电脑USB接口
方法一,BIOS设置法 重新启动计算机,在开机过程中,点击键盘上的“Delete”键,进入BIOS设置界面,选择“Integrated Peripherals”选项,展开后将“USB 1.1 Cont ...
- Javascript:scrollWidth,clientWidth,offsetWidth的区别(转)
网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...
- Tomcat目录下文件详解
一.Tomcat背景 (转) 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group在完成GNUJSP1.0的开发以后,开始考虑在SUN的JSWDK基础上开发一个可以直接提供We ...
- 1. 用自己的算法实现startsWith和endsWith功能。
package com.xinjian; public class Chazifu { public static void main(String[] args) { String a=" ...
- ios控件 UIControl
< UIControl> 1 处理用户事件的控件的基类,如UIButton,UISlider等 2 一般不直接实例化,而是使用他的子类 3 可以通过跟踪触摸事件来设置和获取控件状态,并且这 ...
- Laravel中使用Redis
安装PHP PRedis PRedis是laravel访问redis的扩展包,只需要下载原码即可,不需要安装PHP扩展(如php-redis.so).但在这之前需要了解一个composer,因为lar ...