HDU 3487(Play with Chain-Splay)[template:Splay]
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4679 Accepted Submission(s): 1892
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?
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.
8 2
CUT 3 5 4
FLIP 2 6
-1 -1
1 4 3 7 6 2 5 8
pid=3486">3486
3479 3480 3481pid=3482">3482
Splay 裸题
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (300000+10)
#define MAXM (300000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;} int n,m; class Splay
{
public:
int father[MAXN],siz[MAXN],n;
int ch[MAXN][2],val[MAXN];
bool root[MAXN],rev[MAXN];
int roo; //root
void mem(int _n)
{
MEM(father) MEM(siz) MEM(root) MEM(rev) MEM(ch) MEM(val) flag=0;
n=0;
roo=1;
build(roo,1,_n,0);root[1]=1;
}
void newnode(int &x,int f,int v)
{
x=++n;
father[x]=f;
val[x]=v-1;
} void build(int &x,int L,int R,int f)
{
if (L>R) return ;
int m=(L+R)>>1;
newnode(x,f,m);
build(ch[x][0],L,m-1,x);
build(ch[x][1],m+1,R,x);
maintain(x);
}
int getkth(int x,int k)
{
pushdown(x);
int t;
if (ch[x][0]) t=siz[ch[x][0]]; else t=0; if (t==k-1) return x;
else if (t>=k) return getkth(ch[x][0],k);
else return getkth(ch[x][1],k-t-1); } void pushdown(int x)
{
if (x) if (rev[x])
{
swap(ch[x][0],ch[x][1]);
if (ch[x][0]) rev[ ch[x][0] ]^=1;
if (ch[x][1]) rev[ ch[x][1] ]^=1;
rev[x]^=1;
}
}
void maintain(int x)
{
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
}
void rotate(int x)
{
int y=father[x],kind=ch[y][1]==x; pushdown(y); pushdown(x); ch[y][kind]=ch[x][!kind];
if (ch[y][kind]) {
father[ch[y][kind]]=y;
}
father[x]=father[y];
father[y]=x;
ch[x][!kind]=y;
if (root[y])
{
root[x]=1;root[y]=0;roo=x;
}
else
{
ch[father[x]][ ch[father[x]][1]==y ] = x;
}
maintain(y);maintain(x);
}
void splay(int x)
{
while(!root[x])
{
int y=father[x];
int z=father[y];
if (root[y]) rotate(x);
else if ( (ch[y][1]==x)^(ch[z][1]==y) )
{
rotate(x); rotate(x);
}
else
{
rotate(y); rotate(x);
}
}
roo=x;
}
void splay(int x,int r)
{
while(!(father[x]==r))
{
int y=father[x];
int z=father[y];
if (father[y]==r) rotate(x);
else if ( (ch[y][1]==x)^(ch[z][1]==y) )
{
rotate(x); rotate(x);
}
else
{
rotate(y); rotate(x);
}
}
} void Cut(int a,int b,int c)
{
int x=getkth(roo,a),y=getkth(roo,b);
splay(x);
splay(y,roo);
pushdown(x);pushdown(y);
int z=ch[y][0];
ch[y][0]=0; maintain(y); maintain(x); int u=getkth(roo,c),v=getkth(roo,c+1);
splay(u);
splay(v,roo);
pushdown(u);pushdown(v);
ch[v][0]=z;father[z]=v;
maintain(v);maintain(u); } void Flip(int a,int b)
{
int x=getkth(roo,a),y=getkth(roo,b);
splay(x);
splay(y,roo);
pushdown(x);pushdown(y);
int z=ch[y][0];
rev[z]^=1;
maintain(y); maintain(x);
} bool flag;
void print(int x)
{
if (x==0) return ;
pushdown(x);
print(ch[x][0]); if (val[x]!=0&&val[x]!=n-1)
{
if (flag) putchar(' '); else flag=1;
printf("%d",val[x]); }
print(ch[x][1]);
} }S;
char s[MAXN]; int main()
{
// freopen("hdu3487.in","r",stdin);
// freopen(".out","w",stdout); while(cin>>n>>m)
{
if (n<0&&m<0) break;
n+=2;
S.mem(n);
For(i,m)
{
scanf("%s",s);
if (s[0]=='C')
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
S.Cut(a,b+2,c+1); } else {
int a,b;
scanf("%d%d",&a,&b);
S.Flip(a,b+2);
}
} S.print(S.roo);cout<<endl; } return 0;
}
HDU 3487(Play with Chain-Splay)[template:Splay]的更多相关文章
- 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 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 tree)
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Hdu 3487 play the chain
Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...
- Play with Chain 【HDU - 3487】【Splay+TLE讲解】
题目链接 很好的一道题,用了三天多的时间,终于知道了我为什么T的原因,也知道了在Splay的同时该怎样子的节约时间,因为Splay本身就是大常数的O(N*logN),我们如果不在各种细节上节约时间,很 ...
- POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11384 Accepted: 3572 Case T ...
- hdu 3487
splay #include<cstdio> #include<cstring> #include<iostream> #include<algorithm& ...
随机推荐
- ubuntu 同时安装anaconda2和anaconda3
说明:先根据Ubuntu预装的python2.7来安装Anaconda2,然后将Anaconda3作为其环境安装在envs文件夹下. 重要提示:有一些软件需要py2.7的环境,比如XX-Net, 最好 ...
- AWK的介绍学习
第一节.awk的工作流程和基本用法 1.awk介绍 awk是一种报表生成器,就是对文件进行格式化处理的,这里的格式化不是文件系统的格式化,而是对文件内容进行各种"排版",进而格式化 ...
- 【IDEA】Error: java: Compliance level '1.6' is incompatible with target level '1.8'. A compliance level '1.8' or better is required解决办法
在运行的时候常常出现如下错误: Error: java: Compliance level '1.6' is incompatible with target level '1.8'. A compl ...
- 自己定义View之Chart图标系列(1)——点阵图
近期要做一些图表类的需求,一開始就去github上看了看,发现开源的图表框架还是蛮多的.可是非常少有全然符合我的需求的.另外就是使用起来比較麻烦.所以就决定自己来造轮子了~~~ 今天要介绍的就是And ...
- atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t
atitit. java queue 队列体系and自己定义基于数据库的队列总结o7t 1. 堵塞队列和非堵塞队列 1 2. java.util.Queue接口. 1 3. ConcurrentLin ...
- 对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了
对照 Android 的 Intent 与 iOS StoryBoard 的 Segue - Intent 假设也能添加个prepareForSegue回调就好了 太阳火神的漂亮人生 (http:// ...
- hdu1501 Zipper--DFS
原题链接:pid=1501">http://acm.hdu.edu.cn/showproblem.php?pid=1501 一:原题内容 Problem Description Giv ...
- Sqoop Import原理和详细流程讲解
Sqoop Import原理 Sqoop Import详细流程讲解 Sqoop在import时,需要指定split-by参数.Sqoop根据不同的split-by参数值来进行切分,然后将切分出来的区域 ...
- Python正则表达式的简单应用和示例演示
前一阵子小编给大家连续分享了十篇关于Python正则表达式基础的文章,感兴趣的小伙伴可以点击链接进去查看.今天小编给大家分享的是Python正则表达式的简单应用和示例演示,将前面学习的Python正则 ...
- QNX与Linux两家未来有望独霸车载电子操作系统
车载电子操作系统是汽车智能化的核心,能够有效分配车机的硬件资源,对车内各种任务功能进行协同管理,并控制各项任务优先级别.常见的车载电子操作系统有:QNX.Linux(Android,AaliOS).W ...