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.
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.
For each query of the second type, output the answer.
7 4
1 3
1 2
2 0 1
2 1 2
4
3
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
7
2
10
4
5
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();
}