HDOJ 题目5172 GTY's gay friends(线段树)

本文精选了一系列技术文章,深入探讨了编程、算法、数据结构等核心主题,涵盖了从基础到进阶的知识点,包括但不限于数据安全、云计算、AI、自然语言处理等领域。通过实践案例和理论解析,旨在帮助开发者提升技术能力,解决实际问题。

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

GTY's gay friends

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1215    Accepted Submission(s): 309


Problem Description
GTY has n gay friends. To manage them conveniently, every morning he ordered all his gay friends to stand in a line. Every gay friend has a characteristic value ai , to express how manly or how girlish he is. You, as GTY's assistant, have to answer GTY's queries. In each of GTY's queries, GTY will give you a range [l,r] . Because of GTY's strange hobbies, he wants there is a permutation [1..rl+1] in [l,r] . You need to let him know if there is such a permutation or not.
 

Input
Multi test cases (about 3) . The first line contains two integers n and m ( 1n,m1000000 ), indicating the number of GTY's gay friends and the number of GTY's queries. the second line contains n numbers seperated by spaces. The ith number ai ( 1ain ) indicates GTY's ith gay friend's characteristic value. The next m lines describe GTY's queries. In each line there are two numbers l and r seperated by spaces ( 1lrn ), indicating the query range.
 

Output
For each query, if there is a permutation [1..rl+1] in [l,r] , print 'YES', else print 'NO'.
 

Sample Input
  
8 5 2 1 3 4 5 2 3 1 1 3 1 1 2 2 4 8 1 5 3 2 1 1 1 1 1 1 2
 

Sample Output
  
YES NO YES YES YES YES NO
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5498  5497  5495  5494  5493 
 题目大意: 给出n个数,m个询问,问你[l,r]区间内是否为1到r-l+1的全排列。
思路:要是符合的话首先,肯定这个区间的和一定是(len*(len+1))/2,然后再利用线段树判重,记录下每个点上的值的上一个的位置,利用线段树求出这段区间中数中的上一个位置最大的数,要是这个数在左边界外,说明区间内没有重的
ac代码
150172152015-10-05 15:58:01Accepted51722418MS49308K1232 BG++Who_you?

#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
using namespace std;
#define LL __int64
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
#define N 1000005
LL a[N],sum[N],pr[N],mp[N];
LL node[N*3];
void pushup(LL tr)
{
	node[tr]=max(node[tr<<1],node[tr<<1|1]);
}
void build(LL l,LL r,LL tr)
{
	if(l==r)
	{
		node[tr]=pr[l];
		return;
	}
	LL mid=(l+r)>>1;
	build(l,mid,tr<<1);
	build(mid+1,r,tr<<1|1);
	pushup(tr);
}
LL query(LL L,LL R,LL l,LL r,LL tr)
{
	if(L<=l&&r<=R)
	{
		return node[tr];
	}
	int mid=(l+r)>>1;
	LL x,y;
	x=y=0;
	if(L<=mid)
		x=query(L,R,l,mid,tr<<1);
	if(R>mid)
		y=query(L,R,mid+1,r,tr<<1|1);
	return max(x,y);
}
int main()
{
	LL n,m;
	while(scanf("%I64d%I64d",&n,&m)!=EOF)
	{
		LL i;
	//	map<LL,LL>mp;
		for(i=1;i<=n;i++)
		{
			scanf("%I64d",&a[i]);
			sum[i]=sum[i-1]+a[i];
			pr[i]=mp[a[i]];
			mp[a[i]]=i;
		}
		build(1,n,1);
		while(m--)
		{
			LL x,y;
			scanf("%I64d%I64d",&x,&y);
			LL len=y-x+1;
			LL ss=sum[y]-sum[x-1];
			if(ss==(len)*(len+1)/2)
			{
				LL q=query(x,y,1,n,1);
				if(q<x)
				{
					printf("YES\n");
				}
				else
					printf("NO\n");
			}
			else
				printf("NO\n");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值