HDOJ 3487 Play with Chain

Source: Internet
Author: User

Bare Splay flip + cut and paste

Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3397 Accepted Submission (s): 1408


Problem DescriptionYaoYao 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 wocould 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. cocould you help him?

InputThere 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 shoshould not be processed as a case.

OutputFor each test case, you shoshould print a line with n numbers. The ith number is the number of the ith diamond on the chain.
Sample Input
8 2CUT 3 5 4FLIP 2 6-1 -1

Sample Output
1 4 3 7 6 2 5 8

Source2010 ACM-ICPC Multi-University Training Contest (5) -- Host by BJTU
# Include
 
  
# Include
  
   
# Include
   
    
# Include using namespace std; const int maxn = 330000; const int INF = 0x3f3f3f; # define Key_Value ch [ch [root] [1] [0] int ch [maxn] [2], rev [maxn], sz [maxn], pre [maxn], key [maxn]; int root, tot1; int s [maxn], tot2; int n, m, ans [maxn], cnt; void NewNode (int & x, int father, int k) {if (tot2) x = s [tot2 --]; else x = ++ tot1; ch [x] [0] = ch [x] [1] = rev [x] = 0; pre [x] = father; key [x] = k; sz [x] = 1;} void Upd_Rev (int x) {if (! X) return; swap (ch [x] [0], ch [x] [1]); rev [x] ^ = 1;} void Push_Up (int x) {sz [x] = sz [ch [x] [1] + sz [ch [x] [0] + 1;} void Push_Down (int x) {if (rev [x]) {Upd_Rev (ch [x] [0]); Upd_Rev (ch [x] [1]); rev [x] = 0 ;}} void Build (int & x, int l, int r, int fa) {if (l> r) return; int mid = (l + r)/2; NewNode (x, fa, mid); Build (ch [x] [0], l, mid-1, x); Build (ch [x] [1], mid + 1, r, x); Push_Up (x);} void Init () {root = tot1 = tot2 = 0; ch [root] [0] = ch [root] [1] = Pre [root] = sz [root] = 0; key [root] = INF; NewNode (root, 0, INF); NewNode (ch [root] [1], root, INF); Build (Key_Value, 1, n, ch [root] [1]); Push_Up (ch [root] [1]); Push_Up (root );} void Rotate (int x, int kind) {int y = pre [x]; Push_Down (y), Push_Down (x); ch [y] [! Kind] = ch [x] [kind]; pre [ch [x] [kind] = y; if (pre [y]) ch [pre [y] [ch [pre [y] [1] = y] = x; pre [x] = pre [y]; pre [y] = x; ch [x] [kind] = y; Push_Up (y);} void Splay (int r, int goal) {Push_Down (r ); while (pre [r]! = Goal) {if (pre [pre [r] = goal) {Push_Down (pre [r]); Push_Down (r); Rotate (r, ch [pre [r] [0] = r);} else {Push_Down (pre [pre [r]); Push_Down (pre [r]); push_Down (r); int y = pre [r]; int kind = (ch [pre [y] [0] = y ); if (ch [y] [kind] = r) Rotate (r ,! Kind); else Rotate (y, kind); Rotate (r, kind) ;}} Push_Up (r); if (goal = 0) root = r ;} int Get_Kth (int r, int k) {Push_Down (r); int t = sz [ch [r] [0] + 1; if (k = t) return r; if (t
    
     



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.