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. Vijos1865 NOI2014 魔法森林 LCT维护生成树

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

  2. Win异常: 除了chrome浏览器外,所有安装的软件都连不上网

    经查找资料,是LSP被篡改,恢复后使用正常. 百度百科  LSP: Layered Service Provider, 即分层服务提程序,Winsock 作为应用程序的 Windows 的网络套接字工 ...

  3. Qt Creator编译问题

    有时候需要自己编译Qt Creator,需要注意的就是qmake版本的问题,比如我用4.8.1和4.8.6同样编译出来的Qt Creator在同样的qtconfig-qt4下所呈现的效果是不一样的. ...

  4. 获取IP城市

     新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...

  5. 关于考虑浏览器兼容性时间的工具demo

    //支持跨浏览器的添加事件. var btn = document.getElementById("btn"); function showMes() { alert(" ...

  6. web版扫雷小游戏(二)

    接上篇~~第一次写这种技术博客,发现把自己做的东西介绍出来还是一件脑力活,不是那么轻松啊,好吧,想到哪写到哪,流水记录之,待完成之后再根据大家的意见进行修改吧. 游戏实现 根据对扫雷游戏的体验和分析, ...

  7. input file文件上传样式

    <style>    .file-group {        position: relative;        width: 200px;        height: 80px;  ...

  8. Python Tutorial 学习(二)--Using the Python Interpreter

    Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...

  9. 常用排序算法集合-C实现

    之前熟悉C的时候写着玩的,就当做笔记用吧: #include<stdio.h> #include<stdlib.h> #include<string.h> #inc ...

  10. OC学习总结之面向对象和类

    OC学习总结之面向对象和类   Objective-c是c语言的母集合,它的原意就是在原始的c语言的主体上加入面向对象的特性.1.面向对象和面向过程  面向对象和面向过程是编程的两种思考方式.面向对象 ...