【ACM菜逼解题报告】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 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


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

水吧,慢慢调试吧。


附代码如下:
#include<cstdio> 
#include<algorithm>

using namespace std;

#define MAXN (100000+5) 

struct DoubleLinked{
	int l,r;
};

DoubleLinked a[MAXN];
int n,m;

void build(int x,int y){
	a[x].r=y;a[y].l=x;
}

int main(){
	int K=1;
	while(scanf("%d%d",&n,&m)==2){
		for(int i=1;i<=n;i++)build(i-1,i);build(n,0);
		bool isltor=true;
		for(int i=0;i<m;i++){
			int op,x,y;
			scanf("%d",&op);
			if(op==4){
				isltor=!isltor;
			}else{
				scanf("%d%d",&x,&y);
				if(!isltor&&op!=3)op=op%2+1;
				if(op==3&&a[x].l==y)swap(x,y);
				int XL=a[x].l,XR=a[x].r;
				int YL=a[y].l,YR=a[y].r;
				if(op==1&&XR==y)continue;
				if(op==2&&XL==y)continue;
				if(op==1){build(XL,XR);build(YL,x);build(x,y);}
				else if(op==2){build(XL,XR);build(y,x);build(x,YR);}
				else if(op==3){
					if(XR==y){build(XL,y);build(y,x);build(x,YR);}
					else{build(XL,y);build(y,XR);build(YL,x);build(x,YR);}
				}
			}		
		}
		int b=0;
		long long int ans=0;
		for(int i=1;i<=n;i++){
			b=a[b].r;
			//printf("%d ",b);
			if(i%2==1)ans+=b;
		}
		if(!isltor&&n%2==0)ans=(long long)n*(n+1)/2-ans;
		printf("Case %d: %lld\n",K++,ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值