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. mysql5.7.16安装

    系统:centOS6.5 mysql: 5.7.16 wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-linux-glib ...

  2. python模拟登录知乎

    # -*- coding:utf-8 -*- import urllib import urllib2 import cookielib import sys from bs4 import Beau ...

  3. [转载]Winform中Control的Invoke与BeginInvoke方法

    转自http://www.cppblog.com/baby-fly/archive/2010/04/01/111245.html 一.为什么 Control类提供了 Invoke和 BeginInvo ...

  4. Default Constructor的构造操作

    Default Constructor的构造操作 C++ Annotated Reference Manual书中的Section 12.1说过:default constructor 只有在编译器需 ...

  5. Python Cookbook - 1 - 数据结构和算法

    1 序列解压:通过*来通配 *a, b = somelist,   first, *mid, last = somelist,  a, *b = somelist 2 使用双向队列: from col ...

  6. webform在页面生成的代码与事件回传

    <script type="text/javascript"> //<![CDATA[ var theForm = document.forms['form1'] ...

  7. Django:之Sitemap站点地图、通用视图和上下文渲染器

    Django中自带了sitemap框架,用来生成xml文件 Django sitemap演示: sitemap很重要,可以用来通知搜索引擎页面的地址,页面的重要性,帮助站点得到比较好的收录. 开启si ...

  8. 这种方法在受到.NET版本和访问注册表权限时,是最佳解决方案,虽然代码看起来很多,不过下面的类直接拿走用就可以了。

    public class FileContentType  {      private static IDictionary<string, string> _mappings = ne ...

  9. 谷歌浏览器web开发教程之开始篇:使用sublime

    你的代码编辑器是主要的开发工具:你使用它去编辑和保存代码段.你可以通过学习编辑器快捷键和以及安装关键插件来好而快的写出代码. 目录 安装sublime文本编辑器 为什么使用包管理器? 安装插件 摘要 ...

  10. Row_Number实现分页

    1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...