题目传送门


分析

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

用树状数组维护当前厚度,时间复杂度 \(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. 硬件开发笔记(十四):RK3568底板电路LVDS模块、MIPI模块电路分析、LVDS硬件接口、MIPI硬件接口详解

    前言   本篇继续分析底板原理图mipi/lvds屏幕电路原理图.硬件接口详解.   LVDS与MIPI的区别   液晶屏有RGB TTL.LVDS.MIPI.HDMI接口,这些接口区别于信号的类型( ...

  2. MongoDB的安装及启动

    下载地址 https://www.mongodb.com/try/download/community 安装步骤 自定义安装目录 配置环境 下面是你安装后的mongodb的目录 在电脑的环境变量Pat ...

  3. ASP.NET 读取FTP文件流

    参考资料 ASP.NET 上传文件到共享文件夹 工具类代码 /// <summary> /// 读取ftp文件流 /// </summary> /// <param na ...

  4. 【LeetCode回溯算法#03】电话号码的字母组合(数字映射字母)

    电话号码的字母组合 力扣题目链接(opens new window) 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任 ...

  5. UserWarning: To get the last learning rate computed by the scheduler, please use `get_last_lr()`

    将 lr = scheduler.get_lr() 改为 lr = scheduler.get_last_lr()

  6. Langchain 介绍与入门

    官方介绍 LangChain 是一个利用LLM开发应用程序的框架.它让应用程序具备: 上下文感知能力:将LLM连接到上下文源(提示说明.少量示例.用以形成其响应的内容等) 推理:依靠LLM进行推理(例 ...

  7. 使用 Java 在Excel中创建下拉列表

    下拉列表(下拉框)可以确保用户仅从预先给定的选项中进行选择,这样不仅能减少数据输入错误,还能节省时间提高效率.在MS Excel中,我们可以通过 "数据验证" 提供的选项来创建下拉 ...

  8. Java 小案列 this关键字使用+构造器 +方法+调用

    1 package com.bytezero.thistest; 2 3 public class Boy 4 { 5 private String name; 6 private int age; ...

  9. 3、zookeeper在java使用的API

    引入maven包 <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient< ...

  10. IDEA使用与多线程

    IDEA缩写和快捷键 psvm全称public static void main sout 全称public static void main alt+enter 处理异常 s.out 自动打印s c ...