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& ...
随机推荐
- Entity Framework的一个实例
环境:Visual studio2013+sql server本地数据库 创建一个C#应用程序,首先在nuget中添加Entity Framework 接下来的工作分为四个主要部分: 第一部分:App ...
- Python对象引用的所有权
目录 引用所有权 传递引用的所有权--返回值 出借引用的所有权--返回值 占据引用的所有权--参数 出借引用的所有权--参数 引用所有权 谁持有对象引用的所有权,谁就要对对象负责. 引用的所有权对函数 ...
- centeros 7配置mailx使用外部smtp服务器发送邮件
发送邮件的两种方式: 1.连接现成的smtp服务器去发送(此方法比较简单,直接利用现有的smtp服务器比如qq.新浪.网易等邮箱,只需要直接配置mail.rc文件即可实现) 2.自己搭建私有的smtp ...
- element-ui table 行内编辑
EditRow.ts vue+element-ui+slot-scope原生实现可编辑表格 interface NoParamConstructor<T> { new(): T; } ex ...
- 五大最受欢迎的BUG管理系统
Google在中国大陆遭遇变故做出临时性的退出大陆市场,也使非常多忠实的用户受到小小的挫折,以本公司为例.原本的BUG都是记录在google的 EXCEL在线文档中,由于常常性的打不开.測试和开发组在 ...
- openCV 和GDI画线效率对照
一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大 ...
- 23. Angular 中用 a 标签 href 路由时在浏览器中显示异常 "%2F" 路由失败问题1
这个是angular1.6默认给hash路由上添加了!(感叹号),导致出错,修改方法如下(添加配置,去掉默认前缀感叹号): angular.module('routingDemoApp',['ng ...
- 4.graph.h
#pragma once #include <stdio.h> #include <graphics.h> #include <mmsystem.h> #pragm ...
- 用vuex构建单页
原文地址:点我 前言:在最近学习 Vue.js 的时候,看到国外一篇讲述了如何使用 Vue.js 和 Vuex 来构建一个简单笔记的单页应用的文章.感觉收获挺多,自己在它的例子的基础上进行了一些优化和 ...
- BZOJ1189: [HNOI2007]紧急疏散evacuate(二分答案,最大流)
Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一 块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是 ...