题目传送门


分析

可以发现往左翻太多相当于往右翻一点,所以如果翻的位置超过一半那么打一个取反标记再另一边翻转,

用树状数组维护当前厚度,时间复杂度 \(O(n\log^2 n)\)


代码

#include <cstdio>
#include <cctype>
using namespace std;
int n,Q,L,R,c[100011],you;
int iut(){
int ans=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
void update(int x,int y){
for (;x<=n;x+=-x&x) c[x]+=y;
}
int query(int x){
int ans=0;
for (;x;x-=-x&x) ans+=c[x];
return ans;
}
int main(){
n=iut(),Q=iut(),L=1,R=n;
for (int i=1;i<=n;++i) c[i]=-i&i;
for (int i=1;i<=Q;++i){
int opt=iut(),half=(R-L+1)>>1;
if (opt==1){
int x=iut();
if (you){
if (x<=half){
for (int i=R;i>R-x;--i){
int t=query(i)-query(i-1);
update(R-x-(i-1-(R-x)),t);
}
R-=x;
}else{
x=R-L+1-x;
for (int i=L;i<L+x;++i){
int t=query(i)-query(i-1);
update(L+x+((L+x)-i-1),t);
}
you^=1,L+=x;
}
}else{
if (x<=half){
for (int i=L;i<L+x;++i){
int t=query(i)-query(i-1);
update(L+x+((L+x)-i-1),t);
}
L+=x;
}else{
x=R-L+1-x;
for (int i=R;i>R-x;--i){
int t=query(i)-query(i-1);
update(R-x-(i-1-(R-x)),t);
}
you^=1,R-=x;
}
}
}else{
int l=iut(),r=iut();
if (you) print(query(R-l)-query(R-r));
else print(query(L+r-1)-query(L+l-1));
putchar(10);
}
}
return 0;
}

#树状数组#CF461C 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. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  3. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  6. BZOJ 3529: [Sdoi2014]数表 [莫比乌斯反演 树状数组]

    3529: [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1399  Solved: 694[Submit][Status] ...

  7. BZOJ 3289: Mato的文件管理[莫队算法 树状数组]

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 2399  Solved: 988[Submit][Status][Di ...

  8. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组

    E. e-Government time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  9. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  10. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

随机推荐

  1. 文件IO操作开发笔记(一):使用Qt的QFile对磁盘文件存储进行性能测试以及测试工具

    前言   在做到个别项目对日志要求较高,要求并行写入的数据较多,尽管写入数据的线程放在子线程,仍然会造成界面程序的假死(实际上Qt还是在跑,只是磁盘消耗超过瓶颈,造成假死(注意:控制台还能看到打印输出 ...

  2. live555开发笔记(一):live555介绍、windows上msvc2017编译和工程模板

    前言   在pc上搭建流媒体服务器软件,打开视频接受推流,使用live555方案.   live555介绍   Live555是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了标准流媒体传输 ...

  3. python操作txt文件,去除文件中的隔行空行

    conn = re.sub(result, '\r\n', content) res = "".join( [s for s in conn.strip().splitlines( ...

  4. sqlserver数据库jar包下载

    链接:https://pan.baidu.com/s/1mCx5JpVpmU6uUaqMITxP_Q提取码:4piq 说明:若链接失效,联系会及时补上!

  5. 对于Celery原理的简单理解

    参考博客: https://www.cnblogs.com/forward-wang/p/5970806.html https://blog.csdn.net/cuomer/article/detai ...

  6. 【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数

    问题描述 在Azure App Service for Windows的环境中,部署.NET应用,其中使用了 SAP NetWeaver RFC函数 (需要加载 sapnwrfc.dll).详细的错误 ...

  7. ASP.NET Core 选项

    目录 1,选项接口 2,注入配置与IOptions 3,IOptionsSnapshot 首先要了解 ASP.NET Core 中的配置,请点击这里了解:https://www.cnblogs.com ...

  8. 完整塔建一个spring 注解版 mybaties 过程可供复制代码

    第一步引导包.新建工程maven模块 pom.xml 中导入相对应包 ++++++++++++++++++++++++++++++++++++++++++++       1      +++++++ ...

  9. linux vs code extension C# `GLIBC_2.27' not found

    settings中omnisharp:useModernNet改为true reboot虚机

  10. vscode 快捷键更换 ctrl + h 全局搜索 改为 f1 - 个人习惯 - 针对某些跨文件函数不能自动跳转

    vscode 快捷键更换 ctrl + h 全局搜索 改为 f1 - 个人习惯 - 针对某些跨文件函数不能自动跳转 原来 f1 换成 ctrl + f1 它一般用 ctrl + shift + p 调 ...