B-one box

Source: Internet
Author: User

Description

You have a box numbered 1, 2, 3,… from left to right ,..., N. You can execute four commands:

1 x y indicates moving box X to the left of box Y (ignore this command if X is already on the left of box Y ).
2 x y indicates moving box X to the right of box Y (ignore this command if X is already on the right of box Y ).
3 x y indicates the position of swap box X and Y.
4 indicates that the entire chain is reversed.

The command must be valid, that is, X is not equal to y. For example, when n is 6 and 1 1 4 is executed in the initial state, the box sequence is 2 3 1 4 5 6. Next, execute 2 3 5, and the box sequence is changed to 2 1 4 5 3 6. Then execute 3 1 6 and get 2 6 4 5 3 1. Finally execute 4 and get 1 3 5 4 6 2.

 

Input

The input contains no more than 10 groups of data. The number of first behavior boxes in each group is n and the number of command lines is m (1 <= n, m <= 100,000). The following M lines contain one command per line.

 

Output

Each data group outputs a row, that is, the sum of box numbers at all odd positions. Positions from left to right are numbered 1 ~ N.

 

Sample Input

6 41 1 42 3 53 1 646 31 1 42 3 53 1 6100000 14 

Sample output

Case 1: 12 Case 2: 9 case 3: 2500050000

The difficulty lies in the fourth type of operation, the fourth type of operation is the reversal, and the effect is the follow-up exchange. In fact, there is no need to perform the operation. You only need to record the number of reverse operations. When the number of reversal operations is odd, the operations with flag = 1 are converted to operations with flag = 2, and those with flag = 2 are converted to operations with flag = 1, while those with other operations remain unchanged.
Because after the reversal, the front of a number is changed to the back, and the back is changed to the front. There is also a ring to facilitate the start and end points.
1 # include "stdio. H "2 # include" string. H "3 const int MS = 100004; 4 int left [100004]; 5 Int right [100004]; 6 int * tmp1, * tmp2; 7 inline void Link (int, int B) 8 {9 right [a] = B; 10 left [B] = A; 11} 12 INT main () 13 {14 int n, m, I, J, k, flag, X, Y, P = 1, CNT, next; 15 while (scanf ("% d", & N, & M )! = EOF) 16 {17 for (I = 1; I <= N; I ++) 18 {19 left [I] = I-1; 20 right [I] = I + 1; 21} 22 right [N] = 0; // forms a ring to facilitate searching for the Start Node and End Node 23 CNT = 0; 24 while (M --) 25 {26 scanf ("% d", & flag); 27 if (flag! = 4) 28 scanf ("% d", & X, & Y); 29 else 30 CNT ++; 31 if (CNT & 1) & flag <3) 32 flag = 3-flag; 33 If (flag = 1 & (right [x]! = Y) 34 {35 if (right [y] = x) 36 {37 int T1 = left [X], T2 = right [X], t3 = left [Y], T4 = right [y]; 38 Link (T3, x); 39 Link (x, y); 40 Link (Y, T2 ); 41} 42 else 43 {44 Link (left [X], right [x]); 45 Link (left [Y], x); 46 Link (x, y ); 47} 48} 49 else if (flag = 2 & right [y]! = X) 50 {51 if (right [x] = y) 52 {53 int T1 = left [X], T2 = right [X], t3 = left [Y], T4 = right [y]; 54 Link (T1, Y); 55 Link (Y, x); 56 Link (x, T4 ); 57} 58 else 59 {60 Link (left [X], right [x]); 61 Link (x, right [y]); 62 Link (Y, X ); 63} 64} 65 else if (flag = 3) 66 {67 If (right [x] = y) 68 {69 int T1 = left [X], t2 = right [X], T3 = left [Y], T4 = right [y]; 70 Link (T1, Y); 71 Link (Y, X ); 72 Link (x, T4); 73} 74 else if (right [y] = x) 75 {76 int T1 = left [X], T2 = right [X], t3 = left [Y], T4 = right [y]; 77 Link (T3, x); 78 Link (x, y); 79 Link (Y, T2 ); 80} 81 else {82 int T1 = left [X], T2 = right [X], T3 = left [Y], T4 = right [y]; 83 Link (T1, y); 84 Link (Y, T2); 85 Link (T3, x); 86 Link (x, T4); 87} 88} 89} 90 long ans = 0; 91 If (CNT & 1) 92 {93 tmp2 = right; 94 tmp1 = left; 95} 96 else 97 {98 tmp2 = left; 99 tmp1 = right; 100} 101 for (I = 1; I <= N; I ++) 102 {103 If (tmp2 [I] = 0) 104 {105 for (k = 1; i; I = tmp1 [I], K ++) 106 If (K & 1) 107 ans + = I; 108 printf ("case % d: % LLD \ n ", P ++, ANS); 109 break; 110} 111} 112 return 0; 113}
 

 




B-one box

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.