csu 1365 双向链表模拟超时

1365: Play with Chain

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 21  Solved: 5
[Submit][Status][Web Board]

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?

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.

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

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<algorithm>
  6 using namespace std;
  7 
  8 struct node
  9 {
 10     int num;
 11     struct node *next;
 12     struct node *father;
 13 };
 14 struct node *head;
 15 void mem(struct node *p)
 16 {
 17     p->num=0;
 18     p->next=NULL;
 19     p->father=NULL;
 20 }
 21 void CUT(int l,int r,int k,int n)
 22 {
 23     if(k==l-1)return;
 24     if(l==1&&r==n)return ;
 25     struct node *p,*q,*st,*ed,*hxl;
 26     int i,cur;
 27     if(k<l) cur=r;
 28     else 
 29     {
 30         cur=r-l+1+k;
 31         k=cur;
 32     }
 33     p=head;
 34     for(i=1;i<=cur&&p!=NULL;i++)
 35     {
 36         p=p->next;
 37         if(i==l) st=p;
 38         if(i==r) ed=p;
 39         if(i==k) hxl=p;
 40     }
 41     if(k==0) hxl=head;
 42     p=st->father;
 43     q=ed->next;
 44     p->next=q;
 45     if(q!=NULL) q->father=p;
 46 
 47     p=hxl->next;
 48     ed->next=hxl->next;
 49     if(p!=NULL) p->father=ed;
 50 
 51     hxl->next=st;
 52     st->father=hxl;
 53 }
 54 void FLIP(int l,int r)
 55 {
 56     int i,tmp,tom;
 57     struct node *st,*ed,*q;
 58     q=head;
 59     for(i=1;i<=r;i++)
 60     {
 61         q=q->next;
 62         if(i==l)    st=q;
 63         if(i==r)    ed=q;
 64     }
 65     tom=(r-l+1)/2;
 66     while(tom--)
 67     {
 68         tmp=st->num;
 69         st->num=ed->num;
 70         ed->num=tmp;
 71 
 72         st=st->next;
 73         ed=ed->father;
 74     }
 75 }
 76 int main()
 77 {
 78     int n,m,i;
 79     int l,r,k;
 80     char cur[10];
 81     struct node *p,*q;
 82     while(scanf("%d%d",&n,&m)>0)
 83     {
 84         if(n==-1&&m==-1)break;
 85         head=(struct node*)malloc(sizeof(struct node));
 86         mem(head);
 87         p=head;
 88         for(i=1;i<=n;i++)
 89         {
 90             q=(struct node*)malloc(sizeof(struct node));
 91             q->num=i;
 92             q->next=p->next;
 93             p->next=q;
 94             q->father=p;
 95             p=q;
 96         }
 97         getchar();
 98         while(m--)
 99         {
100             scanf("%s",cur);
101             if(cur[0]=='C')
102             {
103                 scanf("%d%d%d",&l,&r,&k);
104                 CUT(l,r,k,n);
105             }
106             else if(cur[0]=='F')
107             {
108                 scanf("%d%d",&l,&r);
109                 FLIP(l,r);
110             }
111         }
112         p=head;
113         for(i=1;i<=n;i++)
114         {
115             q=p;
116             p=p->next;
117             free(q);
118             printf("%d",p->num);
119             if(i!=n)printf(" ");
120             else printf("\n");
121         }  
122         free(p);
123     }
124     return 0;
125 }

 

 
posted @ 2014-03-10 18:03  芷水  阅读(233)  评论(0编辑  收藏  举报