SPOJ DWARFLOG 线段树

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值