题意:

给一纸条,两种操作:

  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. python(19)- 列表生成式和生成器表达式练习Ⅰ

    列表表达式 程序一: 常规写法: egg_list=[] for i in range(100): egg_list.append('egg%s' %i) print(egg_list) 列表表达式写 ...

  2. python(24)- 面向对象进阶

    面向对象基础知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用: 2.类是一个模板,模板中包装了多个‘函数’供使用(可以将多函数中公用的变量封装到对象中): 3.对象,根据模板 ...

  3. 函数式编程( Functional)与命令式编程( Imperative)对比

    1.函数式编程带来的好处 函数式编程近些年异军突起,又重新回到了人们的视线,并得到蓬勃发展.总结起来,无外乎如下好处: 1.减少了可变量(Immutable Variable)的声明,程序更为安全.  ...

  4. CentOS minimal 安装ssh 服务 和客户端

      检查是否装了SSH包 如果现实有openssh-server 说明系统已经安装了ssh 2 如果系统没有安装ssh 那么可以在线安装 yum install openssh-server 3 设置 ...

  5. 福昕熊雨前:PDFium开源项目的背后

    今天编译android的时候,无意中看到命令行提示出输出编译external/pdfium这个目录,于是乎上百度搜索了一下,找到了如下关于PDF文件解析的开源代码的文章: http://www.csd ...

  6. 腾讯云centos,nginx安装

  7. 在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器

  8. PowerDesigner逆向工程,从SQL Server数据库生成Physical Model -----数据源方式

    1.File-Reverse Engineer-Database 2.DBMS选择SQL Server 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 一路Next..... ...

  9. grouped differently across partitions

    [熵增] 由无序到有序 http://spark.apache.org/docs/latest/rdd-programming-guide.html#shuffle-operations Shuffl ...

  10. virtualbox Units specified don't exist. SHSUCDX can't install.

    version infomatin: virtual box: 5.1.12 platform: win10 x64 target OS: win7 x64 问题 在win10系统上,使用virtua ...