UVA 12657 Boxes in a line (doubly linked list)

Source: Internet
Author: User

12657 Boxes in a line
You are n boxes in a line on the table numbered 1 ... 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 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
From left to right.
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

Analog bidirectional linked list

Number of records during reversal

(Operation after reversal 1 is equivalent to 2, 2 is equivalent to 1)

Pay attention to the details.

#include <bits/stdc++.h>using namespacestd;Const intN =100010;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6; typedefLong LongLl;typedef pair<Double,Double>PII;#defineX First#defineY SecondintN, M, L[n], r[n], tag;voidinit () {tag=0 ;  for(inti =0; I <= N; ++i) {L[i]= I1, r[i] = i +1 ; }}voidTest () { for(inti =0; I <= N; ++i) cout<< L[i] <<' '<< R[i] <<Endl;}voidChangenext (intXinty) {r[l[x]]=y; l[R[y]]=x; L[y]=L[x]; R[X]=R[y]; L[X]=y; R[y]=x;}voidMove_l (intXinty) {if(R[x] = = Y | | x = = y)return ; r[L[x]]=R[x]; l[R[x]]=L[x]; L[X]=L[y]; r[L[y]]=x; L[y]=x; R[X]=y;}voidMove_r (intXinty) {if(L[x] = = Y | | x = = y)return ; r[L[x]]=R[x]; l[R[x]]=L[x]; R[X]=R[y]; l[R[y]]=x; R[y]=x; L[X]=y;}voidChangeintXinty) {if(x = = y)return ; if(R[x] = =y) {Changenext (x, y);return ; }    if(R[y] = =x) {changenext (y,x);return ; }    inttmp, L1, R1, L2, R2; L1= L[x], r1 = r[x], L2 = l[y], r2 =R[y]; TMP= L[x], l[x] = L[y], l[y] =tmp; TMP= R[x], r[x] = R[y], r[y] =tmp; R[L1]= y, l[r1] =y; R[L2]= x, L[R2] =x;} LL Cal (inttag) {LL res=0;intCNT =1 ; if(Tag &&!) (n&1)) tag =0 ; ElseTag =1 ;  for(inti = r[0] ; I <= N; i =R[i]) {        if(Tag && (cnt&1) Res + =i; if(!tag &&!) (cnt&1) Res + =i; CNT++; }    returnRes;}voidRun () {init ();  while(m--){        intop, x, y; scanf ("%d",&op); if(OP = =1) {scanf ("%d%d",&x,&y); if( !tag) move_l (x, y); Elsemove_r (x, y); }        Else if(OP = =2) {scanf ("%d%d",&x,&y); if( !tag) move_r (x, y); Elsemove_l (x, y); }        Else if(OP = =3) {scanf ("%d%d",&x,&y);        Change (x, y); }        Elsetag^=1;//test (); cout << Endl;} printf ("%lld\n", Cal (tag));}intMain () {//freopen ("In.txt", "R", stdin);    int_, cas =1 ;  while(SCANF ("%d%d", &n,&m)! =EOF) {printf ("Case %d:", cas++);    Run (); }}
View Code

UVA 12657 Boxes in a line (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.