线段树(区间内数是否都不相同)hdu5172GTY's gay friends

GTY's gay friends

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


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
 
题意:给定集合,询问区间(l,r)内的数字是不是[1...r-l+1]的一个排列

思路:只需要区间数字和是len*(len+1)/2,并且区间内数字都不相同就是YES,否则为NO

线段树维护位置i上的数字上一次出现的位置的最大值,那么只需要区间[l,r]中的值小于l,就表示区间内数都不相同

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=1000010;
typedef long long LL;
LL sum[maxn];
int N,Q;
int a[maxn];
int last[maxn],pos[maxn];
struct IntervalTree
{
    int maxv[maxn<<2];
    void build(int o,int l,int r)
    {
        maxv[o]=0;
        if(l==r)
        {
            maxv[o]=last[l];
            return;
        }
        int mid=(l+r)>>1;
        build(o<<1,l,mid);
        build(o<<1|1,mid+1,r);
        pushup(o);
    }
    void pushup(int o)
    {
        maxv[o]=max(maxv[o<<1],maxv[o<<1|1]);
    }
    int query(int o,int l,int r,int q1,int q2)
    {
        if(q1<=l&&r<=q2)return maxv[o];
        int mid=(l+r)>>1;
        int ans=0;
        if(q1<=mid)ans=max(ans,query(o<<1,l,mid,q1,q2));
        if(q2>mid)ans=max(ans,query(o<<1|1,mid+1,r,q1,q2));
        return ans;
    }
}tree;
int main()
{
    while(scanf("%d%d",&N,&Q)!=EOF)
    {
        memset(sum,0,sizeof(sum));
        memset(pos,0,sizeof(pos));
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
            last[i]=pos[a[i]];
            pos[a[i]]=i;
        }
        tree.build(1,1,N);
        int l,r;
        while(Q--)
        {
            scanf("%d%d",&l,&r);
            LL len=r-l+1;
            if((sum[r]-sum[l-1]==len*(len+1)/2)&&(tree.query(1,1,N,l,r)<l))
                printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值