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
12
9
2500050000
题意:n个盒子,1-n按照顺序排列在桌子上,m个操作,有4种操作,操作1,将x盒子放在y盒子的左边,操作2,将x盒子放在y盒子的右边,操作3,将x盒子和y盒子位置换一下,操作4,将所有盒子顺序按照一条直线颠倒。在m个操作执行之后,问奇数位置的盒子的和是多少。
思路:题意说n,m<=100000,但是用队列暴力过了。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <deque>
using namespace std;
typedef long long ll;
deque <ll> dq1,dq2;
int main()
{
ll n,m,op,x,y;
int i;
while(~scanf("%lld %lld", &n, &m))
{
ll ANS = 0,ans=0;
while(!dq1.empty())
dq1.pop_back();
while(!dq2.empty())
dq1.pop_back();
for(i=1; i<=n; i++)
dq1.push_back(i);
for (i = 1; i <= m; i++)
{
scanf("%lld", &op);
if(op==1)
{
scanf("%lld%lld", &x, &y);
while(!dq1.empty())
{
ll xx=dq1.front();
if(xx!=x&&xx!=y)
{
dq2.push_back(xx);
}
if(xx==y)
{
dq2.push_back(x);
dq2.push_back(y);
}
dq1.pop_front();
}
while(!dq2.empty())
{
ll xx=dq2.front();
dq1.push_back(xx);
dq2.pop_front();
}
}
else if(op==2)
{
scanf("%d%d", &x, &y);
while(!dq1.empty())
{
ll xx=dq1.front();
if(xx!=x&&xx!=y)
dq2.push_back(xx);
if(xx==y)
{
dq2.push_back(y);
dq2.push_back(x);
}
dq1.pop_front();
}
while(!dq2.empty())
{
ll xx=dq2.front();
dq1.push_back(xx);
dq2.pop_front();
}
}
else if(op==3)
{
scanf("%lld%lld", &x, &y);
while(!dq1.empty())
{
ll xx=dq1.front();
if(xx==x)
dq2.push_back(y);
else if(xx==y)
{
dq2.push_back(x);
}
else
dq2.push_back(xx);
dq1.pop_front();
}
while(!dq2.empty())
{
ll xx=dq2.front();
dq1.push_back(xx);
dq2.pop_front();
}
}
else if(op==4)
{
while(!dq1.empty())
{
ll xx=dq1.front();
dq2.push_front(xx);
dq1.pop_front();
}
while(!dq2.empty())
{
ll xx=dq2.front();
dq1.push_back(xx);
dq2.pop_front();
}
}
}
ll ppp=0;
while(!dq1.empty())
{
ll xx=dq1.front();
if(ppp%2==0)
ANS+=xx;
dq1.pop_front();
ppp++;
}
printf("%lld\n",ANS);
}
return 0;
}