Codeforces 461C 线段树

来源:互联网 发布:成龙获奥斯卡奖知乎 编辑:程序博客网 时间:2024/05/18 22:45

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 41 31 22 0 12 1 2
output
43
input
10 92 2 91 12 0 11 82 0 81 22 1 31 42 2 4
output
721045
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;}


0 0