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& ...
随机推荐
- QQ音乐
import re import requestsimport json class Search: def __init__(self, song): ''' self.vkey_url ---&g ...
- 洛谷 P1604 B进制星球
P1604 B进制星球 题目背景 进制题目,而且还是个计算器~~ 题目描述 话说有一天,小Z乘坐宇宙飞船,飞到一个美丽的星球.因为历史的原因,科技在这个美丽的星球上并不很发达,星球上人们普遍采用B(2 ...
- 编译impala、拓展impala语法解析模块
以前也编译过,但是每次编译都忘记怎么做,然后都得重新找需要下载的文件. 编译文件:buildall.sh 如果想只编译前端可以这样运行: buildall.sh -fe_only 编译时会去S3下载一 ...
- 千千万万的IT开发project师路在何方
已经找不到该文章的最初出处了,有找到的人请告诉我.谢谢~~ 千千万万的IT开发project师路在何方 2007-06-25 21:41 恭喜,你选择开发project师作为自已的职业! 悲哀.你选择 ...
- tp5框架知识点
项目包含的关键点,后台,前台. 入口文件. 通用配置文件. 数据库配置文件. 共有文件,css,images,js. 控制器,模型,视图. 共有类. 共有函数. 属性,方法. 命名规范. 命名空间. ...
- mysql python中的pymysql模块使用
import pymysql # 在这之前需要给mysql授登录权限 grant all on "; 否则会导致连接时出错 # flush privileges; #创建连接 conn = ...
- HDU 1005 Number Sequence(矩阵)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 目前常见的三种SQL分页方式:
--top not in方式 select top 条数 * from tablename where Id not in (select top 条数*页数 Id from tablename) - ...
- noip 2018 day2 T1 旅行 基环树 tarjan
Code: #include<cstdio> #include<cstring> #include<string> #include<stack> #i ...
- CentOS7的聚合链路
1.环境介绍 [root@rhcc ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) ENERAL.DEVICE: en ...