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. Android Activity各启动模式的差异

    Activity共有四种启动模式:standard,singleTop,singleTask,singleInstance 为了方便描述和理解,布局文件.Manifest文件和各个java文件如下: ...

  2. 为Activity设置特定权限才能启动

    1.在AndroidManifest文件中,声明一个权限,并在activity中添加属性 <!--声明权限,权限名一般为包名+permission+类名 --> <permissio ...

  3. XML有哪些解析方式有何优缺点?xml有哪些解析技术?区别是什么?

    有DOM,SAX,STAX等 (1):DOM:处理大型文件时其性能下降的非常厉害.这个问题是由DOM的树结构所造成的,这种结构占用的内存较多,而且DOM必须在解析文件之前把整个文档装入内存,适合对XM ...

  4. I/O多路复用之poll

    poll函数和select函数非常相似,但是函数接口不一样. #include <poll.h> int poll(struct pollfd *fdarray, unsigned lon ...

  5. 一种实现C++反射功能的想法(三)

    如何实现类型名跟类型的对应, 我们很容易想到map, 没错, 就是使用map实现的. std::map<std::string, .....>, 等下, 第二部分该填什么类型, 一个函数指 ...

  6. 【模板】【网络流】Dinic

    /* 唐代杜荀鹤 <小松> 自小刺头深草里,而今渐觉出蓬蒿. 时人不识凌云木,直待凌云始道高. */ #include <iostream> #include <cstd ...

  7. Js regular exprission

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ...

  8. JavaScript 获取当月天数

    getDate() 方法可返回月份的某一天.取值范围是1~31 如果是0的话,就返回最后一天.这样就能取得当月的天数了 比如获取16年2月份的天数 var day = new Date(2016,2, ...

  9. CSS3 :nth-child() 选择器

    CSS3 :nth-child() 选择器 代码: <!DOCTYPE html> <html> <head> <style> p:nth-child( ...

  10. Fedora上配置一个安全FTP

    现在流行的FTP服务器,比较著名的有WU-FTP(Washington University FTP)和VSFTP(Very Secure FTP 非常安全的FTP)以及Proftp,pureftp等 ...