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

直接模拟肯定会超时 用stl中的链表也超时 只能用数组自己模拟一个双向链表了 le[i],ri[i]分别表示第i个盒子左边盒子的序号和右边盒子的序号

参考代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <cctype>
#include <cassert>
#include <typeinfo>
#include <utility> //std::move()
using std::cin;
using std::cout;
using std::endl;
const int INF = 0x7fffffff;
const double EXP = 1e-;
const int MS = ;
typedef long long LL;
int left[MS], right[MS];
int n, m, kase;
void link(int l, int r)
{
right[l] = r;
left[r] = l;
} int main()
{
int kase = ;
while (cin >> n >> m)
{
for (int i = ; i <= n; i++)
{
left[i] = i - ;
right[i] = (i + ) % (n + );
}
right[] = ;
left[] = n;
int op, x, y, inv = ;
while (m--)
{
cin >> op;
if (op == )
inv = !inv;
else
{
cin >> x >> y;
if (op == && right[y] == x)
std::swap(x, y);
if (op != && inv)
op = - op;
if (op == && x == left[y])
continue;
if (op == && x == right[y])
continue;
int lx = left[x], rx = right[x], ly = left[y], ry = right[y];
if (op == )
{
link(lx, rx);
link(ly, x);
link(x, y);
}
else if (op == )
{
link(lx, rx);
link(y, x);
link(x, ry);
}
else if (op == )
{
if (right[x] == y)
{
link(lx, y);
link(y, x);
link(x, ry);
}
else
{
link(lx, y);
link(y, rx);
link(ly, x);
link(x, ry);
}
}
} }
int b = ;
LL ans = ;
for (int i = ; i <= n; i++)
{
b = right[b];
if (i % )
ans += b;
}
if (inv&&n % == )
ans = (LL)n*(n + ) / - ans;
cout << "Case " << ++kase << ": " << ans << endl;
}
return ;
}

最新文章

  1. tyvj1113 魔族密码
  2. Solr Zookeeper ACL权限配置
  3. Swift中的注释以及表达式
  4. [转载]C#字符串加密和解密
  5. HDU 3446 daizhenyang&#39;s chess
  6. windows 数据类型转换为 dotnet 数据类型
  7. Euclid Problem - PC110703
  8. jQuery第一章
  9. MapReduce深度分析(一)
  10. 苹果电脑python3安装pillow模块
  11. MyBatis简单使用和入门理解
  12. vue-04-组件
  13. Linux kafka 单机安装
  14. 安装XP时BIOS的设置(ahci ide)
  15. 关于Xilinx MicroBlaze应用modelsim se仿真问题(EDK:3593 - Unable to locate the precompiled library microblaze_v8_50_c)
  16. MFC图形编辑器
  17. Jquery中动态生成的元素没有点击事件或者只有一次点击事件
  18. 如何配置 Oracle VirtualBox 中的客户机与物理机网络
  19. flash builder 4.6在debug调试时需要系统安装flashplayer debug版本
  20. Hashtable(哈希表)

热门文章

  1. bzoj 3931 [CQOI2015]网络吞吐量(最短路,最大流)
  2. HDU5727 Necklace
  3. Linux 中 x86 的内联汇编
  4. 机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting
  5. BootStrap入门教程 (四) :JQuery类库插件(模态窗口,滚动监控,标签效果,提示效果,“泡芙”效果,警告区域,折叠效果,旋转木马,输入提示)
  6. 基于互联网的VOIP电话系统组建
  7. Native libraries .so.XY failing to link at runtime
  8. 使用bootstrap的html文件转换成jsp…
  9. UVALive 7334 Kernel Knights (dfs)
  10. Test Spring el with ExpressionParser