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 ...
随机推荐
- python(2)- python程序的编写简单介绍
一.语句和语法 # 注释 \ 转译回车,继续上一行,在一行语句较长的情况下可以使用其来切分成多行,因其可读性差所以不建议使用 : 将两个语句连接到一行,可读性差,不建议使用 : 将代码的头和体分开 语 ...
- 数据库历险记(三) | 缓存框架的连环炮 数据库历险记(二) | Redis 和 Mecached 到底哪个好? 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle? 面对海量请求,缓存设计还应该考虑哪些问题?
数据库历险记(三) | 缓存框架的连环炮 文章首发于微信公众号「陈树义」,专注于 Java 技术分享的社区.点击链接扫描二维码,与500位小伙伴一起共同进步.微信公众号二维码 http://p3n ...
- cocos2d0基础篇笔记二
1.菜单的使用: CCMenuItemimage*image=CCMenuItemImage*create("xxx.png", "xxx,png", &quo ...
- caffeModels--models-caffes-大全
caffe的伯克利主页:http://caffe.berkeleyvision.org/caffe的github主页:https://github.com/BVLC/caffe caffe的model ...
- antd移动端onClick事件点击无效
最近空余时间比较多,自己想学习react跟移动端的东西,就选用了antd-mobile库,框架搭好开发过程中遇到个问题,里面绑定的点击事件无效,不仅是antd自带的按钮无效,原生button点击也没反 ...
- Appium python自动化测试系列之认识Appium(四)
4.1界面认识 在之前安装appium的时候说过我们有两种方法安装,也就有两种结果,一种是有界面的,一种是没有界面的,首先我们先讲一下有界面的,以及界面有哪些东西. 首先看第一幅图,如果你的是win ...
- (转)MongoDB在mongo控制台下的基本使用命令
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...
- 搭建React Native开发环境
搭建React Native开发环境 本文档是Mac下搭建的环境,针对的目标平台不同,以及开发 iOS 和 Android 的不同,环境搭建也有差异. Github地址:https://github. ...
- 3932: [CQOI2015]任务查询系统
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2559 Solved: 819[Submit][Sta ...
- java object monitor
1 什么是java object monitor 每个java对象头中都有锁状态位标记.java中在使用synchronize同步的时候,肯定是涉及到某个对象的锁.因此,在考虑同步的时候,首先要想到是 ...