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
a
i![]()
, 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..r−l+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 (
1≤n,m≤1000000
), 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
i
th![]()
number
a
i![]()
(
1≤a
i
≤n
) indicates GTY's
i
th![]()
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 (
1≤l≤r≤n
), indicating the query range.
Output
For each query, if there is a permutation
[1..r−l+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
题目大意:
给出n个数,m个询问,问你[l,r]区间内是否为1到r-l+1的全排列。
思路:要是符合的话首先,肯定这个区间的和一定是(len*(len+1))/2,然后再利用线段树判重,记录下每个点上的值的上一个的位置,利用线段树求出这段区间中数中的上一个位置最大的数,要是这个数在左边界外,说明区间内没有重的
ac代码
#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");
}
}
}