C. Appleman and a Sheet of Paper

 

Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 × n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types:

  1. Fold the sheet of paper at position pi. After this query the leftmost part of the paper with dimensions 1 × pi must be above the rightmost part of the paper with dimensions 1 × ([current width of sheet] - pi).
  2. Count what is the total width of the paper pieces, if we will make two described later cuts and consider only the pieces between the cuts. We will make one cut at distance li from the left border of the current sheet of paper and the other at distance ri from the left border of the current sheet of paper.

Please look at the explanation of the first test example for better understanding of the problem.

Input

The first line contains two integers: n and q (1  ≤ n ≤ 105; 1 ≤ q ≤ 105) — the width of the paper and the number of queries.

Each of the following q lines contains one of the described queries in the following format:

  • "1 pi" (1 ≤ pi < [current width of sheet]) — the first type query.
  • "2 li ri" (0 ≤ li < ri ≤ [current width of sheet]) — the second type query.
Output

For each query of the second type, output the answer.

input
7 4
1 3
1 2
2 0 1
2 1 2
output
4
3
思路: 暴力更新, 然后用FenwickTree 或者SegmentTree进行区间求和即可。
因为每个位置上的value只会更新到别的位置一次,所以暴力的话复杂度也是O(n), 然后更新的时候分两种情况, 如果折过去的长度大于右边界 就相当于把右面的对应长度折过来, 否则就是题目中所说的从左面折了。
我用ua, ub,维护了当前区间的左右端点, 每次查询也分两种情况。
 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
namespace FenwickTree {
int arr[maxn];
void Modify(int x, int d) {
while(x < maxn) {
arr[x] += d;
x += x & -x;
}
}
void init(int n) {
memset(arr, , sizeof arr);
for(int i = ; i <= n; i++) {
Modify(i, );
}
}
int query(int x) {
int res = ;
while (x > ) {
res += arr[x];
x -= x & -x;
}
return res;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n, q;
while (~ scanf ("%d%d", &n, &q)) {
int tot = , direction = ;
FenwickTree::init(n);
int ub = n;
for (int j = ; j < q; j++) {
int op, l, r, p;
int ua = tot+;
scanf ("%d", &op);
if (op == ) {
scanf ("%d", &p);
if (!direction) {
if (*p <= + ub - ua) {
for (int i = ua; i <= ua+p-; i++) {
FenwickTree::Modify(*ua+*p-i-, FenwickTree::query(i) - FenwickTree::query(i-));
}
tot += p;
} else {
p = ub - (ua + p-);
for (int i = ub; i >= ub-p+; i--) {
FenwickTree::Modify(*ub-*p+-i, FenwickTree::query(i) - FenwickTree::query(i-));
}
ub = ub - p;
direction ^= ;
}
}else{
if (*p > + ub - ua){
p = ub - (ua + p-);
for (int i = ua; i <= ua+p-; i++) {
FenwickTree::Modify(*ua+*p-i-, FenwickTree::query(i) - FenwickTree::query(i-));
}
direction ^= ;
tot += p;
}else{
for (int i = ub; i >= ub-p+; i--) {
FenwickTree::Modify(*ub-*p+-i, FenwickTree::query(i) - FenwickTree::query(i-));
}
ub = ub - p;
}
} } else {
scanf ("%d%d", &l, &r);
if (!direction) {
printf("%d\n", FenwickTree::query(ua+r-)-FenwickTree::query(ua-+l));
}else{
printf("%d\n", FenwickTree::query(ub-l)-FenwickTree::query(ub-r));
}
}
}
}
return ;
}
 

Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新的更多相关文章

  1. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  2. Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组

    E. George and Cards   George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...

  3. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  4. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  6. 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman

    题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...

  7. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) E. Little Artem and Time Machine 树状数组

    E. Little Artem and Time Machine 题目连接: http://www.codeforces.com/contest/669/problem/E Description L ...

  8. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  9. Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】

    A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...

  2. SVN Global ignore pattern 忽略文件正则后缀

    *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a *.pyc *.pyo __pycache__ *.rej *~ #*# .#* .*.swp .DS_St ...

  3. Vijos1865 NOI2014 魔法森林 LCT维护生成树

    基本思路: 首先按照weightA升序排序,然后依次在图中加边,并维护起点到终点路径上weightB的最大值 如果加边过程中生成了环,则删除环中weightB最大的边 由于是无向图,点之间没有拓扑序, ...

  4. 对象的内置属性和js的对象之父Object()

    js中对象有constructor,valueOf(),toString()等内置属性和方法; 创建一个空对象的方法: var o = {}; 或者 var o= new Object(); o.co ...

  5. Js设置所有连接是触发/swt/的代码

    Js设置所有连接是触发/swt/的代码 代码为: <script> var doca=document.getElementsByTagName('a'); for(var i=0;i&l ...

  6. js切换换class

    1, js代码 function ntabs(thisObj,Num)            {if(thisObj.className == "active")return;   ...

  7. JS设置Cookie,及COOKIE的限制

    在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...

  8. 舵机的PWM控制学习随笔

    舵机的控制信号,对于脉宽调制信号的脉宽变换,常用的一种方法是采用调制信号获取有源滤波后的直流电压,但是需要50Hz(周期是20ms)的信号,这对运放器件的选择有较高要求,从电路体积和功耗考虑也不易采用 ...

  9. 【C语言】严格区分大小写

    C语言严格区分大小写 一.相关基础知识 二.具体内容 C语言严格区分大小写: 如: int为关键字,INT则为用户标识符,即可定义int INT;   int INt;   int Int;  cha ...

  10. EJB

    Enterprise JavaBean,企业级javabean,是J2EE的一部分,定义了一个用于   开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK). 是Jav ...