Codeforces 461C 线段树
来源:互联网 发布:成龙获奥斯卡奖知乎 编辑:程序博客网 时间:2024/05/18 22:45
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 41 31 22 0 12 1 2
43
10 92 2 91 12 0 11 82 0 81 22 1 31 42 2 4
721045
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;}
- Codeforces 461C 线段树
- Codeforces 52C - Circular RMQ - 线段树
- Codeforces 444C(线段树)
- codeforces 446C 线段树 + fibnacci
- 【CodeForces】52C Circular RMQ 线段树
- 【CodeForces】383C Propagating tree 线段树
- codeforces 380C 线段树括号匹配
- Codeforces 52C (线段树区间更新)
- Codeforces 527C 线段树 /set
- Codeforces Round 718C (矩阵线段树)
- CodeForces 570C Replacement 线段树
- [codeforces] 762C - Two strings 线段树
- codeforces 799C Fountains 线段树
- codeforces 272C. Dima and Staircase(线段树)
- Codeforces 380C - Sereja and Brackets (线段树括号匹配)
- Codeforces 380C Sereja and Brackets(线段树)
- CodeForces 52C Circular RMQ (区间更新线段树)
- CodeForces 52C Circular RMQ (区间更新线段树)
- 1053
- placeholder在IE浏览器不兼容、input点击边框换颜色、jquery中Uncaught ReferenceError: $ is not defined
- css 多行文字居中
- Copy和MutableCopy
- 1054
- Codeforces 461C 线段树
- 源码安装mysql
- java位运算(一),了解二进制与八进制,十进制以及16进制的转换
- 浅谈微服务框架-spring boot
- Nullable<System.DateTime>型的日期字段转格式化日期格式的问题
- php+redis消息队列抢购实现
- python初学者快速入门(四)列表和元组、字典
- 1055
- angularjs指令中的compile详解