题目链接:传送门

分析:每次操作都会花费大量时间,显然我们只需要关注每个元素的左边是啥,右边是啥就够了,那么用双向链表,l[i]表示i左边的数,r[i]表示i右边的数,每次操作模拟一下数组的变化就好了.

不过这样有一个问题:第4个操作似乎无法很高效地进行,如果真的按照它说的那样模拟翻转,恐怕复杂度和暴力也要差不多了,那么我们怎么处理呢?

观察发现题目只让我们输出奇数位置的编号和,也就是说,如果题目给定的序列长度就是奇数的,那么无论用多少次4操作都没有影响,否则4的次数是奇数,则用总和减去当前奇数位置的和就是答案。这就相当于线段树中的lazy标记的做法,现在要考虑这个标记对其它操作的影响,显然对3操作是没有任何影响的,而对1,2操作无非就是把1变成了2,把2变成了1,这样就很快地就能解决问题了。

如果数据结构上的某一个操作很耗时,有时可以用加标记的方式处理,而不需要真的执行那个操作。但同时,该数据结构的所有其他操作都要考虑这个标记!

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int maxn = ; int n, m, kase, l[maxn], r[maxn],flag;
long long ans; int main()
{
while (scanf("%d%d", &n, &m) == )
{
for (int i = ; i <= n; i++)
{
l[i] = i - ;
r[i] = (i + ) % (n + );
}
l[] = n;
r[] = ;
flag = ;
for (int i = ; i <= m; i++)
{
int op,x,y;
scanf("%d", &op);
if (op == )
flag = (flag + ) % ;
else
{
scanf("%d%d", &x, &y);
if (op != && flag)
op = - op;
if (op == && l[y] == x)
continue;
if (op == && r[y] == x)
continue; if (op == )
{
l[r[x]] = l[x];
r[l[x]] = r[x];
r[l[y]] = x;
r[x] = y;
l[x] = l[y];
l[y] = x;
}
else
if (op == )
{
l[r[x]] = l[x];
r[l[x]] = r[x];
l[x] = y;
r[x] = r[y];
l[r[y]] = x;
r[y] = x;
}
else
if (op == )
{
if (r[x] == y)
{
r[l[x]] = y;
l[y] = l[x];
r[y] = x;
l[x] = y;
r[x] = r[y];
l[r[y]] = x;
}
else
{
r[l[y]] = x;
l[r[y]] = x;
r[l[x]] = y;
l[r[x]] = y;
swap(l[x], l[y]);
swap(r[x], r[y]);
}
}
}
int cur = ;
ans = ;
for (int i = ; i <= n; i++)
{
cur = r[cur];
if (i % == )
ans += cur;
}
if (flag && n % == )
ans = (long long)n * (n + ) / - ans;
}
printf("Case %d: %lld\n", ++kase, ans);
} return ;
}

Uva12657 Boxes in a Line的更多相关文章

  1. 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 ...

  2. UVA12657 Boxes in a Line:题解

    题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...

  3. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

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

    题目大意:一个1~n的升序数字序列,有4种操作.操作1,将x放到y前面一个位置:操作2将x放到y后面的一个位置:操作3交换x和y的位置:操作4反转整个序列.求经过m次操作后的所有奇数项的和. 题目分析 ...

  5. Problem B Boxes in a Line

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

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 10.12NOIP模拟题(1)

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...

  2. centos 安装sysbench

    安装sysbench 下载并且解压 shell> wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbe ...

  3. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  4. 二分+RMQ/双端队列/尺取法 HDOJ 5289 Assignment

    题目传送门 /* 题意:问有几个区间最大值-最小值 < k 解法1:枚举左端点,二分右端点,用RMQ(或树状数组)求区间最值,O(nlog(n))复杂度 解法2:用单调队列维护最值,O(n)复杂 ...

  5. 在Azure Ubunt Server 14.04虚机中使用Deep-Visualization-Toolbox

      参考网站 a)   https://zhuanlan.zhihu.com/p/24833574?utm_source=tuicool&utm_medium=referral b)   ht ...

  6. PHP安装yaf在ubuntu下面的问题解决

    1.在执行make的时候出现如下错误: In file included from /root/yaf-2.1.2/yaf_router.c:28: /usr/include/php/ext/pcre ...

  7. Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。

    Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...

  8. iOS 声明属性关键字讲解

    atomic: 原子操作(原子性是指事务的一个完整操作,操作成功就提交,反之就回滚. 原子操作就是指具有原子性的操作)在objective-c 属性设置里面 默认的就是atomic ,意思就是 set ...

  9. vue2.0框架认识

    虚拟dom和声明式渲染: Vue的编译器在编译模板之后,会把这些模板编译成一个渲染函数 .而函数被调用的时候就会渲染并且返回一个 虚拟DOM的树 .这个树非常轻量,它的职责就是描述当前界面所应处的状态 ...

  10. Assembly之instruction之JC

    JC Jump if carry setJHS  Jump if higher or same Syntax JC label JHS label Operation If C = 1: PC + 2 ...