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 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:
- 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).
- 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 树状数组暴力更新的更多相关文章
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- 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 ...
- 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预处理出 ...
- 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 ...
- Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- 贪心 Codeforces Round #263 (Div. 2) C. Appleman and Toastman
题目传送门 /* 贪心:每次把一个丢掉,选择最小的.累加求和,重复n-1次 */ /************************************************ Author :R ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- MVC+EF 的增删改查操作
1. //创建EF映射对象数据集 static Models.db_JiaoYouEntities DbDeleteData = new Models.db_JiaoYouEntities(); 2. ...
- asp.net手动填充TreeView生成树
最近在做项目发现需要用到树的地方,页面的前台任然是使用一个asp.net的控件TreeView来显示树的结构,当然也可以自己在前台写一个树来展示,这在后期跟局功能的不同很大可能会要用到异步的知识,废话 ...
- OpenGL ES 3.0 点,线,三角形绘制形式总结
OpenGL ES 3.0 顶点 -1, 1, 0, -0.5f, 0, 0, 0, -1, 0, -1, 0, 0, 0.5f, 0, 0, 1, -1, ...
- Echarts使用随笔(2)-Echarts中mapType and data
本文出处:http://blog.csdn.net/chenxiaodan_danny/article/details/39081071 series : [ { ...
- oracle数据库读取操作系统的物理文件-转载,待完善
--源地址不详 --创建目录SQL> create directory dir_xls as '/home/oracle'; Directory created. --给用户授权SQL> ...
- TaskbarCreated 消息
托盘中的图片就通过注册这个消息来实现,系统和进程通过进程间通信发送这个消息,进程接收他
- Html 中select标签的边框与右侧倒三角的去除
首先是边框的去除:可以设置属性border:none;或border:0px; 不过这还是有一个bug,不同浏览器会在选中select标签时,加上一个边框: 之后是右侧倒三角的去除:设置属性 appe ...
- 页面点击关闭弹出提示js代码
代码效果为: <script> window.onbeforeunload = function() { return "您好!\n我是abc\n —————————————— ...
- web一点小结
1, AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJAX ...
- DataTable举例
// clrTest1.cpp: 主项目文件. #include "stdafx.h" using namespace System; using namespace System ...