给定N个盒子,从左到右分别标号为1~N;有下面4种操作:
“1 X Y” 表示将编号为X的盒子移到编号为Y的左边;
“2 X Y” 表示将编号为X移到编号为Y的右边;
“3 X Y” 表示交换编号为X和Y的位置;
“4” 表示将1~N所有的盒子反序。
要你求经过M次操作之后,所有奇数位置的盒子标号之和。
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
有多组数据,第一行两个整数n和m,(n,m <=100000)
接下来m个操作,如题目描述,输入合法x≠y
Ouput
操作完成后,输出奇数位置上的编号之和。
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
4 1
3 3 4
Case 1: 12
Case 2: 9
Case 3: 2500050000
Case 4: 5
题解 :我写这个的时候,wa了两发,错在这个样例:
2 2
3 1 2
3 2 1
我写了个输出,每次输出整个序列
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int n,m,id,x,y,t=1;
struct fun{
int l,r;
}f[N];
void init(){
f[0].r=1;
f[0].l=-1;
f[n+1].l=n;
f[n+1].r=n+2;
for(int i=1;i<=n;i++){
if(i==1){
f[i].r=i+1;
f[i].l=0;
}
else if(i==n){
f[i].l=i-1;
f[i].r=n+1;
}
else{
f[i].l=i-1;
f[i].r=i+1;
}
}
}
void deal(int x){
f[f[x].r].l=f[x].l;
f[f[x].l].r=f[x].r;
}
void L(int x,int y){
f[x].l=f[y].l;
f[f[y].l].r=x;
f[x].r=y;
f[y].l=x;
}
void R(int x,int y){
f[x].r=f[y].r;
f[f[y].r].l=x;
f[x].l=y;
f[y].r=x;
}
int main(){
while(scanf("%d %d",&n,&m)!=EOF){
init();
int flag=0;
while(m--){
scanf("%d",&id);
if(id==1){
scanf("%d %d",&x,&y);
if(!flag){
deal(x);
L(x,y);
}
else{
deal(x);
R(x,y);
}
}
else if(id==2){
scanf("%d %d",&x,&y);
if(!flag){
deal(x);
R(x,y);
}
else{
deal(x);
L(x,y);
}
}
else if(id==3){
scanf("%d %d",&x,&y);
if(!flag){
if(f[x].l==y||f[x].r==y){ // 判断交换结点是否相邻
if(f[x].l==y){
f[x].l=f[y].l;
f[f[y].l].r=x;
f[y].r=f[x].r;
f[f[x].r].l=y;
f[y].l=x;
f[x].r=y;
}
else{
f[x].r=f[y].r;
f[f[y].r].l=x;
f[y].l=f[x].l;
f[f[x].l].r=y;
f[x].l=y;
f[y].r=x;
}
}
else{
int per1=f[x].l,per2=f[y].l;
deal(x); deal(y);
R(y,per1);
R(x,per2);
}
}
else{
if(f[x].l==y||f[x].r==y){ // 判断交换结点是否相邻
if(f[x].l==y){
f[x].l=f[y].l;
f[f[y].l].r=x;
f[y].r=f[x].r;
f[f[x].r].l=y;
f[y].l=x;
f[x].r=y;
}
else{
f[x].r=f[y].r;
f[f[y].r].l=x;
f[y].l=f[x].l;
f[f[x].l].r=y;
f[x].l=y;
f[y].r=x;
}
}
else{
int per1=f[x].r,per2=f[y].r;
deal(x); deal(y);
L(y,per1);
L(x,per2);
}
}
}
else{
if(flag) flag=0;
else flag=1;
}
/* if(!flag){
int h=0,d=0;
while(h<n){
printf("%d ",f[d].r);
d=f[d].r;
h++;
}
printf("\n");
}
else{
int h=0,d=n+1;
while(h<n){
printf("%d ",f[d].l);
d=f[d].l;
h++;
}
printf("\n");
}*/
}
printf("Case %d: ",t++);
ll sum=0;
if(!flag){
int h=0,d=0;
while(h<n){
if(h%2==0)
sum+=f[d].r;
d=f[d].r;
h++;
}
}
else{
int h=0,d=n+1;
while(h<n){
if(h%2==0)
sum+=f[d].l;
d=f[d].l;
h++;
}
}
printf("%lld\n",sum);
}
return 0;
}