题意:

给一纸条,两种操作:

  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的更多相关文章

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

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

  3. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  4. CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树

    http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...

  5. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  6. 如何写出优秀的研究论文 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 ...

  7. CF Playing with Paper

    Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

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

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

随机推荐

  1. grep man 有删减 百科

    NAME grep, egrep, fgrep, rgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FIL ...

  2. Android 非静态内部类导致内存泄漏原因深入剖析

    背景 上周发现蘑菇街IM-Android代码里面.一些地方代码编写不当.存在内存泄漏的问题.在和疯紫交流的过程中.发现加深了一些理解,所以决定写一下分析思路,相互学习. 内存泄漏 一个不会被使用的对象 ...

  3. 按照HashMap中value值进行排序

    import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  4. .Net 中的反射(动态创建类型实例)

    动态创建对象 在前面节中,我们先了解了反射,然后利用反射查看了类型信息,并学习了如何创建自定义特性,并利用反射来遍历它.可以说,前面三节,我们学习的都是反射是什么,在接下来的章节中,我们将学习反射可以 ...

  5. MySQL 数据类型转换

    版权个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5505523.html 网络越来越达到所以带来的好处不容置疑. ...

  6. IOS之禁用UIWebView的默认交互行为

    本文转载至 http://my.oschina.net/hmj/blog/111344       UIKit提供UIWebView组件,允许开发者在App中嵌入Web页面.通过UIWebView组件 ...

  7. HDU 6076 Security Check DP递推优化

    Security Check Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  8. 【BZOJ4952】lydsy七月月赛 E 二分答案

    [BZOJ4952]lydsy七月月赛 E 题面 题解:傻题...二分答案即可,精度有坑. #include <cstdio> #include <cstring> #incl ...

  9. SAM4E单片机之旅——5、LED呼吸和PWM

    PWM在高频情况下,一个很好的用处就是通过控制占空比来控制输出的功率,比如控制风扇转速.LED灯的亮度等.这次就利用PWM的中断功能,动态改变脉冲的占空比,来实现呼吸灯的效果. 一.实现思路 PWM可 ...

  10. EasyDarwin开源流媒体社区视频教程

    EasyDarwin开源社区出视频教程了,经过几个深夜的努力,终于将第一次课程的5个课时的视频教程录好<EasyDarwin开源流媒体服务器:编译.配置.部署>,EasyDarwin后面会 ...