Appleman and a Sheet of Paper
题意:
给一纸条,两种操作:
1.将左侧长度为$x$的纸条向右翻折。
2.询问位于$[l,r]$的纸条总长度。
解法:
考虑启发式,每一次一个小纸条折叠我们可以看做是一次合并,如果我们每一次将较小的纸条并入较大的纸条。
这样对于每一个数字,包含它的纸条长度每次至少乘以2,这样每一个数字变动$logn$次。
对于一个$2x > len$过大的操作,我们可以转化为将右面的$len-x$个翻折过来,并执行一次翻转操作。
用$rev$记录是否翻转,分类讨论即可。
同时用线段树记录区间和。
这样总效率$O(nlog^2n)$
#include <iostream>
#include <cstdio>
#include <cstring> #define N 100010
#define lb(x) ((x)&(-x)) using namespace std; int n,q,L,R;
int sumv[N],a[N]; void add(int x,int v)
{
for(int i=x;i<=n&&i;i+=lb(i))
sumv[i]+=v;
} int ask(int x)
{
int ans=;
for(int i=x;i>;i-=lb(i)) ans+=sumv[i];
return ans;
} void solve(int cnt,int typ)
{
if(typ==)
{
for(int i=;i<cnt;i++)
{
a[L+cnt+i] += a[L+cnt-i-];
add(L+cnt+i,a[L+cnt-i-]);
add(L+cnt-i-,-a[L+cnt-i-]);
a[L+cnt-i-]=;
}
L+=cnt;
}
else
{
for(int i=;i<cnt;i++)
{
a[R-cnt-i] += a[R-cnt+i+];
add(R-cnt-i,a[R-cnt+i+]);
add(R-cnt+i+,-a[R-cnt+i+]);
a[R-cnt+i+]=;
}
R-=cnt;
}
} int main()
{
while(~scanf("%d%d",&n,&q))
{
for(int i=;i<=n;i++) sumv[i]=,a[i]=;
for(int i=;i<=n;i++) add(i,);
L=;
R=n;
int rev=;
for(int i=,cmd,x,y;i<=q;i++)
{
scanf("%d%d",&cmd,&x);
if(cmd==)
{
int len = R-L+;
if(x*<=len) solve(x,rev);
else solve(len-x,rev^),rev^=;
}
else
{
int ans;
scanf("%d",&y);
if(!rev) ans = ask(L+y-)-ask(L+x-);
else ans = ask(R-x)-ask(R-y);
printf("%d\n",ans);
}
}
}
}
Appleman and a Sheet of Paper的更多相关文章
- Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
C. Appleman and a Sheet of Paper Appleman has a very big sheet of paper. This sheet has a form of ...
- Codechef A Game With a Sheet of Paper
Discription Yuuko and Nagi like to play the following game: Initially they take a checkered sheet of ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树
http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...
- Codeforces #263 div2 解题报告
比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...
- 如何写出优秀的研究论文 Chapter 1. How to Write an A+ Research Paper
This Chapter outlines the logical steps to writing a good research paper. To achieve supreme excelle ...
- CF Playing with Paper
Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #296 (Div. 2) A. Playing with Paper
A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an o ...
- Uva - 177 - Paper Folding
If a large sheet of paper is folded in half, then in half again, etc, with all the folds parallel, t ...
随机推荐
- git学习(4)---工作流
一.目的 前三章介绍了git工具本身的操作,主要包含本地仓库操作和远程库操作两部分内容.接下来,我们将介绍怎样使用git进行项目开发,也叫做git工作流. git工作流分为三种模式:共享远程库模式.独 ...
- ubuntu安装交叉编译工具链
一.sudo su 切换到root用户权限 二.将arm-linux-gcc-4.3.2.tgz从windows拷贝到Linux /home/ttt/arm-linux-gcc-4.3.2.tgz ...
- canvas 五角星之回顾【初中三角函数】
当程序中遇到三角函数的时候我是懵逼的,于是百度了“初中三角函数”, 忘了这几个公式的,自己打脸. 目的是通过Canvas画一个五角星, 突破口:只要能通过给定的两个外圈点的半径,和内圈点的半径,借助上 ...
- Webkit二:RenderTree 创建
RenderObject 作为所有Render 树节点的基类,完全类似与DOM树中的Node基类,它是构成Render 树的基础,作用非比寻常,其中包含了构成Render 树所可能涉及到的一些基本属性 ...
- CI框架常识
1.有两种方法来加载自定义配置文件(如enums.php): <?php if (! defined('BASEPATH')) exit('No direct script access all ...
- 主题:iframe高度的自适应
在项目开发中,遇到的一个问题.弹出的页面中有iframe.例 <iframe src="www.baidu.html" width="100%" char ...
- 九度OJ 1131:合唱队形 (DP、最长上升下降序列)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2865 解决:881 题目描述: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合唱队形. ...
- Please read "Security" section of the manual to find out how to run mysqld as root!
[root@test ~]# /usr/local/mysql/bin/mysqld2018-08-05T08:29:05.143142Z 0 [Warning] [MY-011070] [Serve ...
- 使用酷Q SDK开发QQ机器人
酷Q SDK下载地址:https://github.com/CoolQ/cqsdk-vc 打开工程,编辑appmain.cpp 将“私聊消息”处的代码 更改为 CQEVENT(int32_t, __e ...
- Date日期转字符创格式的日期
public static void main(String[] args) { SimpleDateFormat dateFormater = new SimpleDateFormat(" ...