Codeforces 461C 线段树

本文介绍了一种基于线段树的数据结构解决方案,用于处理一系列纸张折叠操作,并能在折叠后的纸张上进行区间求和查询。通过巧妙地利用线段树来维护每次折叠后的纸张状态,可以高效地解答区间内的纸张宽度总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Examples
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.


题意:初始你有一个长度为n的矩形纸张。
你可以进行q次操作。
1 pi, 将纸张左边的长为pi部分折叠到右边的(当前纸张长度-pi)部分上。
2 li ri, 询问这段区间内纸张的长度总和。


题解:如果折叠操作不能超过右边  就把折叠的部分覆盖上去  如果超过右边  则翻转纸条  用线段树维护和


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int lab[100005];
struct node{
	int num;
}tree[400005];
void build(int t,int l,int r){
	if(l==r){
		tree[t].num=1;
		lab[l]=t;
		return ;
	}
	int mid=l+r>>1;
	build(t<<1,l,mid);
	build(t<<1|1,mid+1,r);
	tree[t].num=tree[t<<1].num+tree[t<<1|1].num;
}
int ans=0;
void query(int t,int l,int r,int x,int y){
	if(x<=l&&y>=r){
		ans+=tree[t].num;
		return ;
	}
	int mid=l+r>>1;
	if(y<=mid)query(t<<1,l,mid,x,y);
	else if(x>mid)query(t<<1|1,mid+1,r,x,y);
	else{
		query(t<<1,l,mid,x,y);
		query(t<<1|1,mid+1,r,x,y);
	}
}
int main(){
	int n,i,q,m;
	scanf("%d%d",&n,&q);
	build(1,1,n);
	int lef=1,rig=n,now=0;
	while(q--){
		int d,x,y;
		scanf("%d",&d);
		if(d==1){
			scanf("%d",&x);
			if(now==0){
				if(rig-(lef+x)+1>=x){
					for(i=lef;i<lef+x;i++){
						int dt=lab[i+2*(x-(i-lef+1))+1];
						tree[dt].num+=tree[lab[i]].num;
						while(dt){
							dt/=2;
							tree[dt].num=tree[dt<<1].num+tree[dt<<1|1].num;
						}
//						printf("%d\n",tree[lab[i+2*(x-(i-lef+1))+1]]);
					}
					lef=lef+x;
				}
				else{
					for(i=rig;i>=lef+x;i--){
						int dt=lab[i-(i-lef-x)*2-1];
						tree[dt].num+=tree[lab[i]].num;
						while(dt){
							dt/=2;
							tree[dt].num=tree[dt<<1].num+tree[dt<<1|1].num;
						}
//						printf("%d\n",tree[lab[i+2*(x-(i-lef+1))+1]]);
					}
					rig=lef+x-1;
					now^=1;
				}
			}
			else{
				if(rig-x-lef+1>=x){
					for(i=rig;i>=rig-x+1;i--){
						int dt=lab[i-2*(i-(rig-x+1))-1];
						tree[dt].num+=tree[lab[i]].num;
						while(dt){
							dt/=2;
							tree[dt].num=tree[dt<<1].num+tree[dt<<1|1].num;
						}
					}
					rig=rig-x;
				}
				else{
					for(i=lef;i<=rig-x;i++){
						int dt=lab[i+2*(rig-x-i)+1];
						tree[dt].num+=tree[lab[i]].num;
						while(dt){
							dt/=2;
							tree[dt].num=tree[dt<<1].num+tree[dt<<1|1].num;
						}
//						printf("%d\n",tree[lab[i+2*(x-(i-lef+1))+1]]);
					}
					lef=rig-x+1;
					now^=1;
				}
			}
		}
		else{
			scanf("%d%d",&x,&y);
			if(now==0){
				ans=0;
				query(1,1,n,x+lef,lef+y-1);
				printf("%d\n",ans);
			}
			else{
				ans=0;
				query(1,1,n,rig-y+1,rig-x);
				printf("%d\n",ans);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值