省赛B题。。。。
手写链表。。其实很简单的。。。。
比赛时太急了,各种手残。。。。没搞出来。。。。要不然就有金了。。。
注:对相邻的元素需要特判。。。。。

Problem B

Boxes in a Line

You have 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 to Y (ignore this if X is already the left of Y)
  • 2 X Y: move box X to the right to Y (ignore this if X is already the right of Y)
  • 3 X Y: swap box X and Y
  • 4: reverse the whole line.

Commands are guaranteed to be valid, i.e. X will be 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 will be at most 10 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 are 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

Output for the Sample Input

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


The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan

#include <iostream>
#include <cstdio>
#include <cstring> #define toleft p
#define toright p^1 using namespace std; const int INF=0x3f3f3f3f;
typedef long long int LL; int lian[][],n,m,p; void Init()
{
p=;
for(int i=;i<=n;i++)
{
lian[i][]=i-;
lian[i][]=i+;
}
lian[][toleft]=INF;
lian[][toright]=;
lian[n+][toleft]=n;
lian[n+][toright]=INF;
} void cmdfour()
{
p=p^;
} void cmdone(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xr==y) return; lian[xl][toright]=xr;
lian[xr][toleft]=xl; lian[x][toright]=y;
lian[x][toleft]=yl; lian[yl][toright]=x;
lian[y][toleft]=x;
} void cmdtwo(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xl==y) return; lian[xl][toright]=xr;
lian[xr][toleft]=xl; lian[x][toleft]=y;
lian[x][toright]=yr; lian[y][toright]=x;
lian[yr][toleft]=x;
} void cmdthree(int x,int y)
{
int xl=lian[x][toleft],xr=lian[x][toright];
int yl=lian[y][toleft],yr=lian[y][toright]; if(xr!=y&&xl!=y)
{
lian[x][toleft]=yl;
lian[x][toright]=yr;
lian[yl][toright]=x;
lian[yr][toleft]=x; lian[y][toleft]=xl;
lian[y][toright]=xr;
lian[xl][toright]=y;
lian[xr][toleft]=y;
}
else
{
if(xr==y)
{
lian[x][toright]=yr;
lian[x][toleft]=y; lian[y][toright]=x;
lian[y][toleft]=xl; lian[xl][toright]=y;
lian[yr][toleft]=x;
}
else if(xl==y)
{
lian[x][toleft]=yl;
lian[x][toright]=y; lian[y][toleft]=x;
lian[y][toright]=xr; lian[xr][toleft]=y;
lian[yl][toright]=x;
} }
} LL getSum()
{
LL ans=;
int cnt=,pos=;
if(n%==||p==)
{
if(p) p=;
while(true)
{
if(cnt&) ans+=(LL)lian[pos][toright];
pos=lian[pos][toright];
cnt++;
if(cnt>=n+) break;
}
}
else if(p==)
{
if(p) p=;
while(true)
{
if(cnt%==) ans+=(LL)lian[pos][toright];
pos=lian[pos][toright];
cnt++;
if(cnt>=n+) break;
}
}
return ans;
} int main()
{
int cas=,cmd,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
Init(); while(m--)
{
scanf("%d",&cmd);
if(cmd==)
{
scanf("%d%d",&x,&y);
cmdone(x,y);
}
else if(cmd==)
{
scanf("%d%d",&x,&y);
cmdtwo(x,y);
}
else if(cmd==)
{
scanf("%d%d",&x,&y);
cmdthree(x,y);
}
else if(cmd==)
{
cmdfour();
}
}
LL ans=getSum();
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}

Problem B Boxes in a Line的更多相关文章

  1. Boxes in a Line

    Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...

  2. uva-12657 - Boxes in a Line(双向链表)

    12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...

  3. Boxes in a Line(移动盒子)

      You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...

  4. C - Boxes in a Line 数组模拟链表

    You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...

  5. UVa 12657 Boxes in a Line(应用双链表)

    Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...

  6. Boxes in a Line UVA - 12657

      You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...

  7. UVA 12657 Boxes in a Line 双向链表

    题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47066 利用链表换位置时间复杂度为1的优越性,同时也考虑到使用实际 ...

  8. Boxes in a Line UVA - 12657 (双向链表)

    题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m  代表有n个盒子 每个盒子最开始按1~n排成一行  m个操作, 1 x y  :把盒子x放到y ...

  9. 2019 SCUT SE 新生训练第四波 L - Boxes in a Line——双向链表

    先上一波题目 https://vjudge.net/contest/338760#problem/L 这道题我们维护一个双向链表 操作1 2 3 都是双向链表的基本操作 4操作考虑到手动将链表反转时间 ...

随机推荐

  1. [iOS Hybrid实践:UIWebView中Html中用JS调用OC方法,OC执行JS代码]

    原理: 1.JS调用OC 每次webview执行跳转时都会被iOS给拦截,执行下面函数获得系统允许. 因此可以根据跳转信息转给系统,执行相应功能,比如打开相册等. // 网页中的每一个请求都会被触发 ...

  2. [SVN Mac自带SVN结合新浪SAE进行代码管理]

    前一篇我转载了别人SVN的使用方法,前面的配置和服务器我不是很明白,自己尝试后发现我需要使用到的核心命令是下面一些. 新浪SAE提供了SVN代码管理仓库,只要进入相应应用,然后点击左侧代码管理,到最下 ...

  3. Python 循环判断和数据类型

    循环和判断 1.if 形式 if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_ ...

  4. WCF入门

    一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...

  5. Rabbitmq -Routeing模式- python编码实现

    (using the pika 0.10.0 Python client) In the previous tutorial we built a simple logging system. We ...

  6. yourtour的几种链接

    php,html {:URL('User-Register/index')}    格式:http://www.xxx.com/index.php?g=User&m=User&a=in ...

  7. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)--MySQL错误

    MySQL错误整理: 错误一: ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/my ...

  8. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pat----------<base>元素有关

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. ...

  9. Python-面向对象编程(一)

    初识面向对象: Python中一切皆对象,我自己,我的电脑,电脑桌,都称之为一个对象.对象是类的一个实体. 我们可以通过行为和特征(属性)来描述一个对象,比如小狗,它有四条腿,一个尾巴,两个虎牙,这就 ...

  10. CO-类的本质、description方法

    类的本质 1. 类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class  *Class; 类名就代表着类对象 ...