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& ...
随机推荐
- ECNUOJ 2855 贪吃蛇
贪吃蛇 Time Limit:1000MS Memory Limit:65536KBTotal Submit:480 Accepted:109 Description 相信很多人都玩过这个游戏,当然 ...
- Xamarin大佬的地址
https://www.cnblogs.com/hlx-blogs/p/7266098.html http://www.cnblogs.com/GuZhenYin/p/6971069.html
- [ Java ] [ Eclipse ] 导出/导入Eclipse的workspace配置(备份Eclipse配置)
Export *.epf 文件 原文連結: http://www.cnblogs.com/52php/p/5677647.html
- CSS3侧滑导航
<!DOCTYPE> <html> <head> <meta charset="utf-8" /> <meta name=&q ...
- display 表格模型值
display:inline-table 此元素会作为内联表格来显示(类似 <table>),表格前后没有换行符.display:inline-table 可以使文字和表格在同一行显示 d ...
- 威联通NAS 网站无法登录,可以ssh情况下重启设备方法
步骤: 1.VPN登录NAS 2.PUTTY SSH登录设备 3.reboot设备 等待重启约5分钟.
- node.js状态码
100——客户必须继续发出请求101——客户要求服务器根据请求转换HTTP协议版本200——交易成功201——提示知道新文件的URL202——接受和处理.但处理未完成203——返回信息不确定或不完整2 ...
- 【APP测试】APP弱网环境测试
方法一:利用抓包工具 1.利用fiddler通过代理连接上手机之后,进入Fiddler->Rules->Customize Rules,点击弹出的CustomRules.js文件,找到m_ ...
- Codeforces 164 D Minimum Diameter
题目链接~~> 做题感悟:越来越感觉CF的题非常好,非常有深度. 解题思路: 这题须要注意 k 的大小.由于 k 仅仅有 30 个,终于形成的点的直径一定是某个确定的值,所以我们能够枚举这个值. ...
- 中科燕园GIS外包--移动GIS
移动GIS恰逢其时 得益于移动智能终端的普及和移动互联网的发展,伴随着GIS技术的发展和应用的深入.越来越多的企业和普通消费者開始体会到移动GIS的巨大潜力和价值. 移动GIS轻便灵活,受众面广.随时 ...