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. NOTE07152246 JAVA 发展及JDK配置

    一.软件工程师体系: 操作系统(Windows/Linux/Unix) - 数据库系统 - 中间件(WebSphere/Tomcat) - JAVA EE 1.操作系统为用户构建了一个平台.此平台上可 ...

  2. 9509 开灯(dfs)

    9509 开灯 时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description 有16的开关分别控制16盏灯,开关排列成 ...

  3. Windows使用小技巧

    一.windows7 系统切换到 administrator用户 先进入windows 7的安全模式.. (这你应该会吧?) 然后系统会自动调出用administrator 帐号登录.. 密码你必须要 ...

  4. H5页面适配所有iPhone和安卓机型的六个技巧

    http://www.th7.cn/web/html-css/201605/166006.shtml http://www.th7.cn/web/html-css/201601/153127.shtm ...

  5. MySQL安装--ubuntu

    1. 执行命令: $ apt-get install mysql-server 执行上述命令会自动安装mysql-client,mysql-common等包. 2. 安装过程中会让你给root账号设置 ...

  6. mongo细节

    mongo创建表db.createCollection(name, {capped: <Boolean>, autoIndexId: <Boolean>, size: < ...

  7. .htaccess文件url重写小记

    .htaccess文件url重写 当上一条规则匹配 并转换后 符合下一条规则的 继续下一条的匹配转换 RewriteRule ^shangpin-([0-9a-zA-Z]+)/category-([0 ...

  8. PatrolRobot(UVa1600)BFS

    PatrolRobot(UVa1600)BFS 珉黻郐距 河吏蝉醉 闵棵黏言 芤她之瞌 褰上稽莨 錾傻奉 郦玫睃芩 摇摇头还没回答魏海洪就抢先回答道:呵呵你们几个别试 蚰镉氡 钬 绦可 ...

  9. pro文件常用内容

    qmake生成的pro文件中常用变量 SUBDIRS 指定子目录 TARGET 指定生成的应用程序名(默认为项目名) DEPENDPATH 指定程序编译时依赖的相关路径 INCLUDEPATH 指定头 ...

  10. english 释词

    english 释词 [amount of & number of]the amount of /the number of指……的数量an amount of/a number of 指“大 ...