SPOJ DWARFLOG 线段树
来源:互联网 发布:pip安装tensorflow 编辑:程序博客网 时间:2024/05/22 15:32
Manipulate Dwarfs
In a small village beyond seven hills and seven seas, Snow White lives together with N dwarves who spend all their time eating and playing League of Legends. Snow White wants to put an end to this, so she has organized gym classes for them.
At the beginning of each class, the dwarves must stand in line, ordered by their height. For the purposes of this task, assume that the dwarves have heights 1, 2, ..., N (each exactly once) and initially all are standing in sorted order with height from 1 to N. Now Snow White play on them by issuing commands of the form:
• 1 X Y -- dwarves with height X and Y in the line must switch places. She also checks their ordering by issuing queries of the form:
• 2 A B -- do dwarves with heights A, A+1, ..., B (not necessarily in that order) occupy a contiguous subsequence of the current line? Help the doofus dwarves follow Snow White's instructions and respond to her queries.
INPUT
The first line of input contains the two positive integers N and M, the number of dwarves and the number of Snow White's requests, respectively (2 ≤ N ≤ 200 000, 2 ≤ M ≤ 200 000). Each of the following M lines contains a single Snow White's request, of the form "1 X Y" (1 ≤ X, Y ≤ N, X ≠ Y) or “2 A B” (1 ≤ A ≤ B ≤ N), as described in the problem statement.
OUTPUT
The output must contain one line for each request of type 2, containing the reply to the query, either “YES” or “NO”.
Example:
Input :
4 5
2 2 3
2 2 4
1 1 3
2 3 4
1 4 3
Output :
YES
YES
NO
题意:n个小矮人初始编号是1-n,1 x y,代表x和y交换,2 x y代表询问x x+1 ...y 这些小矮人是否连续。
题解:线段树维护区间最大值和最小值,找x到y的最大值和最小值相减,如果长度等于人数,就代表是连续的。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;struct node{int maxx,minn,num;}tree[800005];int n,m,sol[200005];void build(int t,int l,int r){if(l==r){tree[t].num=tree[t].maxx=tree[t].minn=l;return ;}int mid=l+r>>1;build(t<<1,l,mid);build(t<<1|1,mid+1,r);tree[t].maxx=max(tree[t<<1].maxx,tree[t<<1|1].maxx);tree[t].minn=min(tree[t<<1].minn,tree[t<<1|1].minn);}void update(int t,int l,int r,int lab){if(l==r){tree[t].num=tree[t].maxx=tree[t].minn=sol[lab];return ;}int mid=l+r>>1;if(lab<=mid)update(t<<1,l,mid,lab);else update(t<<1|1,mid+1,r,lab);tree[t].maxx=max(tree[t<<1].maxx,tree[t<<1|1].maxx);tree[t].minn=min(tree[t<<1].minn,tree[t<<1|1].minn);}int ans;void querymax(int t,int l,int r,int x,int y){if(r<=y&&l>=x){ans=max(ans,tree[t].maxx);return ;}int mid=l+r>>1;if(y<=mid)querymax(t<<1,l,mid,x,y);else if(x>mid)querymax(t<<1|1,mid+1,r,x,y);else{querymax(t<<1,l,mid,x,y);querymax(t<<1|1,mid+1,r,x,y);}}void querymin(int t,int l,int r,int x,int y){if(r<=y&&l>=x){ans=min(ans,tree[t].minn);return ;}int mid=l+r>>1;if(y<=mid)querymin(t<<1,l,mid,x,y);else if(x>mid)querymin(t<<1|1,mid+1,r,x,y);else{querymin(t<<1,l,mid,x,y);querymin(t<<1|1,mid+1,r,x,y);}}int main(){scanf("%d%d",&n,&m);build(1,1,n);int i,x,y,z;for(i=1;i<=n;i++)sol[i]=i;for(i=1;i<=m;i++){scanf("%d%d%d",&x,&y,&z);if(x==1){swap(sol[y],sol[z]);update(1,1,n,y);update(1,1,n,z);}else{ans=0;querymax(1,1,n,y,z);int rs=ans;ans=n;querymin(1,1,n,y,z);int ls=ans;if(rs-ls==z-y)printf("YES\n");else printf("NO\n");}}return 0;}
- SPOJ DWARFLOG 线段树
- SPOJ 24772 DWARFLOG
- SPOJ MULTQ3 线段树
- SPOJ GSS3 线段树
- SPOJ GGS1 线段树
- SPOJ IITWPC4F 线段树
- SPOJ BGSHOOT 线段树
- SPOJ GSS1 [线段树]
- SPOJ 375 (树链剖分+线段树)
- SPOJ 375 (树链剖分+线段树)
- spoj 61 Brackets(线段树)
- spoj 61 Brackets(线段树)
- SPOJ GSS2 离线线段树
- SPOJ GSS1 第一次线段树
- spoj LITE(线段树)
- SPOJ 1557 GSS2 线段树
- Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)
- SPOJ 3267 主席树||线段树+离线
- PCA 迷你项目
- 1001.A+B Format
- 文章标题 coderforces 761B : Dasha and friends(KMP)
- javascript笔记--(第九章)日期与时间
- 配置JDBC连接池
- SPOJ DWARFLOG 线段树
- 谈谈对象大小——从字节对齐到对象模型
- SRM 688 ParenthesesDiv2Hard
- Linux文件权限及用户管理
- backtracking: 22. Generate Parentheses
- linux命令中的特殊字符
- Java——包装类
- PAT甲级1013
- 【C++】引用调用