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 ...
随机推荐
- Android 巧妙实现图片和文字上下布局或者左右布局
最近去了一家新公司,然后开始做新的项目,看其代码发现了一个很巧妙的方法来实现图片在上面文字在下面的布局方式.只需要一个控件——RadioButton. 布局文件很简单,用来展示RadioBUtton的 ...
- SQL存储过程基于字段名传入的字符串拼接.
--定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...
- WINDOWS批处理命令使用大全
来源:http://www.942dn.com就是爱电脑网 WINDOWS批处理命令使用大全 批处理,也称为批处理脚本,英文译为BATCH,批处理文件后缀BAT就取的前三个字母.它的构成没有固定格式, ...
- 查找被锁对象的名称、sid,锁定的类型-1123
select lk.sid,lk_obj.object_id,obj.object_name,DECODE(LK.LMODE,0,'None',1,'Null',2,'Row-S (SS)',3,'R ...
- javascript 之DOM篇
要怎么样的开场白才能使我有力气再更新学习进度呢?啊啊啊啊啊,表示好累啊~~~默念“棒棒棒,我最棒~”召唤精气神开总结敲字咯.哈哈哈. --------------------------------- ...
- javascript--”原路返回“
css代码: <style type="text/css"> * { margin: 0px; padding: 0px; font-family: "mic ...
- gvim 常用命令
插入: insert 强退: :q! 退出: :q 保存: :w 保存退出::wq 复制: yy(单行) 多行:8yy 删除: dd(单行) 多行:8dd 或者 :4,8d 执行脚本: :! ...
- C#窗体程序画倾斜一定角度的椭圆
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- bzoj3583: 杰杰的女性朋友 && 4362: Graph
Description 给出一张n个点的有向图G(V,E).对于任意两个点u,v(u可以等于v),u向v的连边数为: ∑OUT(u,i) * IN(v,i),其中1<=i<=K 其中k和数 ...
- isKindOfClass:和isMemberOfClass:-b
isKindOfClass: Returns a Boolean value that indicates whether the receiver is an instance of given c ...