codeforces Round #263(div2) E解题报告

E. Appleman and a Sheet of Paper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
7 4
1 3
1 2
2 0 1
2 1 2
output
4
3
input
10 9
2 2 9
1 1
2 0 1
1 8
2 0 8
1 2
2 1 3
1 4
2 2 4
output
7
2
10
4
5
Note

The pictures below show the shapes of the paper during the queries of the first example:

After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.


题目大意:

给出一段1*n的纸条,有2个操作:

        1. 制定某个位置,进行折叠,

        2. 询问某段区间纸条的厚度(折叠后的区间)

解法:

用线段树来维护每段区域的厚度,由于每次折叠都会慢慢缩小范围,所以暴力维护也不会太差。

        在使用线段树时,由于题意要求每次都是从左向右折叠,当出现左边的区域大于右边的区域时,可以发现我们的线段树的区间将会扩大很多,这不利于我们控制线段树的大小,所以,可以采取翻转折叠的方式,当左边的区域大于右边的区域时,则先按照从右到左折叠,然后再翻转。之前不注意翻转这个问题,导致WA TEST 6

代码:

#include <cstdio>
#include <algorithm>
#define root 1, 0, n
#define N_max 123456

using namespace std;

int n, q, l ,r;
int tree[3*N_max];
int line[N_max];

void build(int v, int l, int r) {
	if (l+1 == r) {
		tree[v] = 1;
		line[r] = 1;
		return;
	}

	int ls = v<<1, rs = ls+1, mid = (l+r)>>1;

	build(ls, l, mid);
	build(rs, mid, r);

	tree[v] = tree[ls]+tree[rs];
}

void update(int v, int l, int r, int whe, int val) {
	if (whe <= l || whe > r)  return;
	if (l == r-1 && r == whe) {
		tree[v] = val;
		line[whe] = val;
		return;
	}

	int midn = (l+r)>>1, ls = v<<1, rs = ls+1;
	update(ls, l, midn, whe, val);
	update(rs, midn, r, whe, val);

	tree[v] = tree[ls] + tree[rs];
}

int query(int v, int l, int r, int ql, int qr) {
	if (r <= ql || l >= qr)  return 0;

	if (ql <= l && r <= qr)
		return tree[v];

	int midn = (l+r)>>1, ls = v<<1, rs = ls+1;
	return query(ls, l, midn, ql, qr) + query(rs, midn, r, ql, qr);
}

void init() {
	scanf("%d%d", &n, &q);
	l = 0;
	r = n;

	build(root);
}

void solve() {
	int rev = 0;

	for (int t = 1; t <= q; t++) {
		int tmp;

		scanf("%d", &tmp);

		if (tmp == 1) {
			int pos;

			scanf("%d", &pos);

			if (rev)  rev=0, pos = r-l-pos;

			if (pos <= (r-l)>>1) {
				for (int i = 0; i < pos; i++) {
					update(root, l+pos+i+1, line[l+pos+i+1]+line[l+pos-i]);
					update(root, l+pos-i, 0);
				}

				l = l+pos;
			}
			else {
				for (int i = 0; i < r-(l+pos); i++) {
					update(root, l+pos-i, line[l+pos-i]+line[l+pos+i+1]);
					update(root, l+pos+i+1, 0);
				}

				r = l+pos;
				rev ^= 1;
			}
		}
		else {
			int posl, posr;

			scanf("%d%d", &posl ,&posr);

			if (rev) {
				int tmp = posr;
				posr = (r-l)-posl;
				posl = (r-l)-tmp;
			}

			printf("%d\n", query(root, l+posl, l+posr));
		}
	}
}

int main() {
	init();
	solve();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值