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 ...
随机推荐
- 杭电 HDU 1279 验证角谷猜想
验证角谷猜想 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDMI
HDMI,全称为(High Definition Multimedia Interface)高清多媒体接口,主要用于传输高清音视频信号. 现在是HDMI2.0.新一代的HDMI2.0较上一代最大的提升 ...
- 无线(仅WIFI)攻击思路总结
从事信息安全相关工作5年了,虽然主要工作是安全产品售前.安全服务等方向,但既然选择了安全,想必都是有点黑客情节的,因此也前前后后杂七杂八的学了点东西.最近在研究无线(主要是WIFI)安全,相关书籍看了 ...
- 2015最新iherb海淘攻略-图文入门教程-6月免邮
注:仅仅有首次下单才享有新人优惠10$,大家下单之后千万不要取消后.否则之后则不享有新人优惠. 注:眼下Sino-海淘客国际物流已取消,仅有UCS合众速递. IHerb是美国最热门的海淘海购网站之中的 ...
- quick-cocos2d-x开发工具sublime text及其强力插件QuickXDev
更新:如今QuickXDev已经能够通过Package Control下载了,全部QuickXDev相关的请看这里:http://my.oschina.net/lonewolf/blog?catalo ...
- iOS开发-14款状态栏(StatusBar)开源软件
本文转载至 http://mobile.51cto.com/hot-418125.htm 之前逛街看到移动做推广,有一个定位应用挺好的,合理的利用了状态栏,做了一些消息提醒和隐藏动画,自己回家就做了一 ...
- css3-calc用法
css3--calc()使用 css3新增的一个功能,可以计算元素的长度 例如说:一个百分百布局中,分左右两侧,中间需要一个分隔空间,使用padding或者margin则会超出100%,这时候使用ca ...
- kong
https://docs.konghq.com/install/centos/ wget https://bintray.com/kong/kong-community-edition-rpm/dow ...
- Jquery跨域调用
今天在项目中须要做远程数据载入并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发现JQuer ...
- wireshark 学习 1
wireshark 调试WiFi 安装之后的启动脚步. #!/bin/bash iw dev mon0 del iw dev wlp3s0 interface add mon0 type monito ...