Play with Chain

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

【Problem Description】
YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n. At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n. He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain. For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
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?
 
【Input】
There will be multiple test cases in a test data. For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively. Then m lines follow, each line contains one operation. The command is like this: CUT a b c   // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1). FLIP a b    // Means a FLIP operation, 1 ≤ a < b ≤ n. The input ends up with two negative numbers, which should not be processed as a case.
 
【Output】
For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.
 
【Sample Input】
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的更多相关文章

  1. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  2. 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 ...

  3. 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 ...

  4. HDU 3487 Play with Chain 【Splay】

    1-n的序列,有两种操作: 1,将一段区间翻转 2,将一段区间切下来放到剩余序列的第C个数后 采用延迟更新的方法维护区间的翻转,并维护一个size域. 添加一个最大点和一个最小点,防止出界 翻转时,将 ...

  5. Hdu 3487 play the chain

    Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...

  6. 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 ...

  7. 【HDU 3487】Play with Chain Splay

    题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...

  8. Play with Chain 【HDU - 3487】【Splay+TLE讲解】

    题目链接 很好的一道题,用了三天多的时间,终于知道了我为什么T的原因,也知道了在Splay的同时该怎样子的节约时间,因为Splay本身就是大常数的O(N*logN),我们如果不在各种细节上节约时间,很 ...

  9. HDU 3487 Splay tree

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. 斯坦福大学公开课:iOS 7应用开发 笔记

    2015-07-06 第一讲   课务.iOS概述 -------------------------------------------------- 开始学习斯坦福大学公开课:iOS 7应用开发留 ...

  2. windows下如何快速搭建web.py开发框架

    在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方 ...

  3. ios隐藏键盘的方式简单应用

    iOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我们可以实现点击键盘以外的空白区域来将键盘隐藏, ...

  4. 搭建Mantis 缺陷管理系统

    什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...

  5. Chapter 2 Open Book——7

    I gunned my deafening engine to life, ignoring the heads that turned inmy direction, and backed care ...

  6. @Autowired 和 @Resource

    转自:Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resourc ...

  7. hdu_5807_Keep In Touch(分段dp)

    题目链接:hdu_5807_Keep In Touch 题意: 在Byteland一共有nn个城市,编号依次为11到nn,同时有mm条单向道路连接着这些城市,其中第ii条道路的起点为u_iu​i​​, ...

  8. 如何在sharepoint里通过correlation id查找详细的错误信息

    Sharepoint里我们经常遇到这样的错误信息: 我们能通过下面的power shell 命令来查到详细的错误信息: $correlationid = "943e6e9c-b5d9-207 ...

  9. 20150627分享iOS开发笔记

    util是工具的意思:Ad Hoc是特别的,临时的意思;validate是验证的意思: 打包 苹果的键盘真好使 6和6 plus真机测试报错:No architectures to compile f ...

  10. Windows安装Composer出现【Composer Security Warning】警告

    提示信息: The openssl extension is missing from the PHP version you specified.This means that secure HTT ...