1329: one-line box time limit:1 Sec Memory limit:128 MB
submit:740 solved:145
[Submit] [Status] [Web Board] Description
You have a row of boxes, numbered 1, 2, 3,..., n from left to right. You can perform four kinds of commands:
1 x y means to move the box X to the left of Box y (if X is already on the left side of Y ignore this directive).
2 x y means to move the box X to the right of Box Y (if X is already on the right side of Y ignore this directive).
3 x y indicates the position of the swap box X and Y.
4 indicates that the entire chain is reversed.
The directive guarantees legality, i.e. x is not equal to Y. For example, when n=6 executes 1 1 4 in its initial state, the box sequence is 2 3 1 4 5 6. Next Execute 2 3 5, the box sequence becomes 2 1 4 5 3 6. Re-execute 3 1 6, get 2 6 4 5 3 1. Final execution 4, get 1 3 5 4 6 2.
Input
The input contains no more than 10 sets of data, the first behavior of each group of data box N and the number of instruction strips m (1<=n,m<=100,000), the following m line contains one instruction per line.
Output
Each set of data outputs a row, which is the sum of the box numbers for all odd positions. The position is numbered 1~n from left to right.
Sample Input
6 41 1 42 3 53 1 646 31 1 42 3 53 1 6100000 14
Sample Output
Case 1:12case 2:9
Case 3:2,500,050,000
The difficulty lies in the fourth operation, the fourth operation is the reversal, the effect is the back and forth exchange, in fact, do not need to operate, just record the number of reversal of the line. When the number of reversals is odd,
The operation of Flag=1 becomes flag=2 operation, the operation of flag=2 becomes the operation of Flag=1, the other is unchanged.
Because after the reversal, the front of a number becomes the back and the back becomes the front. There is also the formation of the ring, convenient to find the starting point and end point.
AC Code:
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <queue>6#include <string>7#include <cmath>8 using namespacestd;9 int*tmp1,*TMP2;Ten Const intN = 1e5+Ten; One intLeft[n],right[n]; A voidSwapintAintb) - { -Right[a] =b; theLEFT[B] =A; - } - intMain () - { + intCAS =1; - intt,cnt,nest,flag,m,n,x,y; + while(~SCANF ("%d%d",&m,&N)) A { at for(intI=1; i<=m;i++)//pretreatment - { -Left[i] = i1; -Right[i] = i+1; - } -RIGHT[M] =0; inCNT =0; - while(n--) to { +scanf"%d",&flag); - if(Flag! =4) scanf ("%d%d",&x,&y); the Elsecnt++; * if((cnt&1) && flag<3) flag =3-Flag; $ if(Flag = =1&& left[y]!=x)Panax Notoginseng { - swap (left[x],right[x]); the swap (left[y],x); + swap (x, y); A } the Else if(Flag = =2&& right[y]!=x) + { - swap (left[x],right[x]); $ swap (x,right[y]); $ swap (y,x); - } - Else if(Flag = =3) the { - if(Right[x] = = y)//the two are next to each other;Wuyi { the swap (left[x],y); - swap (x,right[y]); Wu swap (y,x); - } About Else if(Left[x] = =y) $ { - swap (left[y],x); - swap (y,right[x]); - swap (x, y); A } + Else the { - intT1 = Left[x],t2 = Right[x],t3 = Left[y],t4 =Right[y]; $ swap (t1,y); the swap (Y,T2); the swap (t3,x); the swap (X,T4); the - } in } the } the Long LongAns =0; About if(cnt&1)//Flip it. the { theTMP2 =Right ; theTMP1 =Left ; + } - Else { theTMP2 =Left ;BayiTMP1 =Right ; the } the for(intI=1; i<=m;i++) - { - if(Tmp2[i] = =0)//looking for the first place the { the for(intk=1; i;i = tmp1[i],k++) the if(k&1) ans = ans+i; theprintf"Case %d:%lld\n", cas++, ans); - Break; the } the } the }94 return 0; the}
CSU 1329: A row of boxes