Problem uva12657-boxes in a line (array analogue doubly linked list)

Source: Internet
Author: User

Problem Uva12657-boxes in a line

accept:725 submit:9255time limit:1000 mSec problem Description

You had n boxes in a line on the table numbered 1...N from left to right. Your task is to simulate 4 kinds of commands:

? 1 x Y:move box X to the left and Y (ignore this if X are already the left of Y)

? 2 x Y:move box X to the right and Y (ignore this if X are already the right of Y)

? 3 x Y:swap box X and Y

? 4:reverse the whole line.

Commands is guaranteed to be valid, i.e. X would be is not equal to Y. For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing 2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1. Then after executing 4, then line becomes 1 3 5 4 6 2

Input

There'll is at the most test cases. Each test case is begins with a line containing 2 integers n, m (1≤n,m≤100,000). Each of the following m lines contain a command.

Output

For each test case, print the sum of numbers at odd-indexed positions. Positions is numbered 1 to N.

Sample Input

6 4 1 1 4 2 3 5 3 1 6 4 6 3 1 1 4 2 3 5 3 1 6 100000 1 4

Sample output

Case 1:12

Case 2:9

Case 3:2,500,050,000

The puzzle: An array simulates a doubly linked list. Although it is a simulation, but the operation is not very simple, the application of doubly linked list is natural, difficult is the middle of the various changes, my first code used to learn C language like a pointer to the operation, what the pre next is X Next,next's pre is what what, This kind of operation is more easily wrong is the order problem, once the order is wrong, the information will be lost, and then do not know where to go, purple Book method is very excellent, early first record down the precursor, the successor, so do not lose information, order what do not have to consider, and then the operation of the edge of the package into the function, This is to simplify the thinking, or shorten the length of thinking, after these two operations, the original very laborious modification relationship becomes almost no brain operation, such a good method must learn.

The code is completely written in purple ...

1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <cstdio>5 using namespacestd;6typedefLong LongLL;7 8 Const intMAXN =100000+Ten;9 intNEXT[MAXN],PRE[MAXN];Ten intCNT =1; One  A voidLink (intPintN) { -NEXT[P] = N,pre[n] =p; - } the  - intMain () - { -     //freopen ("Input.txt", "R", stdin); +     intn,m; -      while(~SCANF ("%d%d",&n,&m)) { +          for(inti =1; I <= n;i++){ ANext[i] = (i+1)% (n+1); atPre[i] = i1; -         } -next[0] =1, pre[0] =N; -         intX,Y,OPE,INV =0; -          for(inti =1; I <= m;i++){ -scanf"%d",&ope); in             if(Ope = =4) INV =!INV; -             Else{ toscanf"%d%d",&x,&y); +                 if(Inv && (ope==1|| ope==2)) Ope =3-Ope; -                 if(ope==1&& pre[y]==x)Continue; the                 if(ope==2&& pre[x]==y)Continue; *                 if(ope==3&& next[y]==×) swap (x, y); $                 intNX = NEXT[X],PX =Pre[x];Panax Notoginseng                 intNY = Next[y],py =Pre[y]; -                 if(Ope = =1){ the Link (PX,NX); Link (PY,X); Link (x, y); +                 } A                 if(Ope = =2){ the Link (PX,NX); Link (Y,X); Link (x,ny); +                 } -                 if(Ope = =3){ $                     if(next[x]==y) { $ Link (px,y); Link (Y,X); Link (x,ny); -                     } -                     Else{ the Link (px,y); Link (Y,NX); - Link (py,x); Link (x,ny);Wuyi                     } the                 } -             } Wu         } -LL ans =0; About         intt = next[0]; $          for(inti =1; I <= n;i++){ -             if(i%2==1) ans + =T; -t =Next[t]; -         } A         if(Inv && n%2==0) ans = 1ll*n* (n+1)/2-ans; +printf"Case %d:%lld\n", cnt++, ans); the     } -     return 0; $}

Problem uva12657-boxes in a line (array analogue doubly linked list)

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.