首页 > 代码库 > HDU 3487 splay区间翻转切割
HDU 3487 splay区间翻转切割
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3986 Accepted Submission(s): 1633
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?
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.
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
8 2 CUT 3 5 4 FLIP 2 6 -1 -1
Sample Output
1 4 3 7 6 2 5 8
Source
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
代码:
/* *********************************************** Author :rabbit Created Time :2014/10/31 13:27:36 File Name :2.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <string> #include <time.h> #include <math.h> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; const int maxn=300300; struct Node; Node *null; struct Node{ Node *ch[2],*fa; int size,rev,key; Node(){ ch[0]=ch[1]=fa=null; rev=0; } inline void push_up(){ if(this==null)return; size=ch[0]->size+ch[1]->size+1; } inline void setc(Node *p,int d){ ch[d]=p; p->fa=this; } inline bool d(){ return fa->ch[1]==this; } void clear(){ size=1; ch[0]=ch[1]=fa=null; rev=0; } void Update_Rev(){ if(this==null)return; swap(ch[0],ch[1]); rev^=1; } inline void push_down(){ if(this==null)return; if(rev){ ch[0]->Update_Rev(); ch[1]->Update_Rev(); rev=0; } } inline bool isroot(){ return fa==null||this!=fa->ch[0]&&this!=fa->ch[1]; } }; inline void rotate(Node *x){ Node *f=x->fa,*ff=x->fa->fa; f->push_down(); x->push_down(); int c=x->d(),cc=f->d(); f->setc(x->ch[!c],c); x->setc(f,!c); if(ff->ch[cc]==f)ff->setc(x,cc); else x->fa=ff; f->push_up(); } inline void splay(Node *&root,Node *x,Node *goal){ while(x->fa!=goal){ if(x->fa->fa==goal)rotate(x); else{ x->fa->fa->push_down(); x->fa->push_down(); x->push_down(); bool f=x->fa->d(); x->d()==f?rotate(x->fa):rotate(x); rotate(x); } } x->push_up(); if(goal==null)root=x; } Node *get_kth(Node *r,int k){ Node *x=r; x->push_down(); while(x->ch[0]->size+1!=k){ if(k<x->ch[0]->size+1)x=x->ch[0]; else{ k-=x->ch[0]->size+1; x=x->ch[1]; } x->push_down(); } return x; } Node pool[maxn],*tail,*node[maxn],*root; void build(Node *&x,int l,int r,Node *fa){ if(l>r)return; int mid=(l+r)/2; x=tail++; x->clear(); x->fa=fa; x->key=mid; node[mid]=x; build(x->ch[0],l,mid-1,x); build(x->ch[1],mid+1,r,x); x->push_up(); } void init(int n){ tail=pool; null=tail++; null->fa=null->ch[0]=null->ch[1]=null; null->size=0;null->rev=0;null->key=0; Node *p=tail++; p->clear();p->key=-INF; root=p; p=tail++; p->clear();p->key=INF; root->setc(p,1); build(root->ch[1]->ch[0],1,n,root->ch[1]); root->ch[1]->push_up(); root->push_up(); } void cut(int a,int b,int c){ splay(root,get_kth(root,a),null); splay(root,get_kth(root,b+2),root); Node *tmp=root->ch[1]->ch[0]; root->ch[1]->ch[0]=null; root->ch[1]->push_up(); root->push_up(); splay(root,get_kth(root,c+1),null); splay(root,get_kth(root,c+2),root); root->ch[1]->ch[0]=tmp; tmp->fa=root->ch[1]; root->ch[1]->push_up(); root->push_up(); } void flip(int a,int b){ splay(root,get_kth(root,a),null); splay(root,get_kth(root,b+2),root); root->ch[1]->ch[0]->Update_Rev(); root->ch[1]->push_up(); root->push_up(); } int ans[maxn],cnt; void dfs(Node *root){ if(root==null)return; root->push_down(); dfs(root->ch[0]); ans[cnt++]=root->key; dfs(root->ch[1]); } int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,m; while(~scanf("%d%d",&n,&m)){ if(n==-1&&m==-1)break; char op[20]; init(n); while(m--){ scanf("%s",op); if(op[0]=='C'){ int a,b,c; scanf("%d%d%d",&a,&b,&c); cut(a,b,c); } else { int a,b; scanf("%d%d",&a,&b); flip(a,b); } } cnt=0; dfs(root); for(int i=1;i<cnt-1;i++)printf("%d%c",ans[i],i==cnt-2?'\n':' '); } return 0; }
HDU 3487 splay区间翻转切割
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。