HDU - 5172 GTY's gay friends

题目链接

题意:n个数m个查询,问[l,r]中的数是否为1到r-l+1的一个排列。

做法1:
hash一下,对于[1...n],每个数都随机分配一个hash值,一个集合的hash值为元素异或和。预处理出[1...n]的hash值及其前缀的hash,然后就可以O(1)查询了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<ctime>
#define eps (1e-8)

using namespace std;

typedef long long ll;
unsigned long long ra[1000005],a[1000005],ha[1000005];
int n,m,t,p;

int main()
{
    srand(time(NULL));
    a[0] = ha[0] = 0;
    for(int i=1; i<=1000000; i++)
    {
        ra[i] = rand()*rand();
        ha[i] = ha[i-1]^ra[i];
    }
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&t);
            a[i] = ra[t];
            a[i]^=a[i-1];
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&t,&p);
            puts((ha[p-t+1]==(a[p]^a[t-1])?"YES":"NO"));
        }
    }
    return 0;
}

做法二:线段树
若(l,r)中的数为1 ~ r-l+1中的一个排列,则必须满足:

1、(l,r)中的数之和为len*(len+1)/2,其中len = r-l+1。

2、区间内的数字各不相同,即用线段树维护位置i上的数上次出现的位置的最大值。只要区间内所有的数上次出现的位置last[i] < l,则区间内的数各不相同。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = 1000005;
int pre[N],now[N];
LL sum[N];
struct Tree{
    int MAX_ID;
}tree[N<<2];
int MAX_ID;
void PushUp(int idx){
    tree[idx].MAX_ID = max(tree[idx<<1].MAX_ID,tree[idx<<1|1].MAX_ID);
}
void build(int l,int r,int idx){
    if(l==r){
        tree[idx].MAX_ID = pre[l];
        return;
    }
    int mid = (l+r)>>1;
    build(l,mid,idx<<1);
    build(mid+1,r,idx<<1|1);
    PushUp(idx);
}
void query(int l,int r,int L,int R,int idx){
    if(l >= L&& r <= R){
        MAX_ID = max(MAX_ID,tree[idx].MAX_ID);
        return;
    }
    int mid = (l+r)>>1;
    if(mid>=L) query(l,mid,L,R,idx<<1);
    if(mid<R)  query(mid+1,r,L,R,idx<<1|1);
}
int main()
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        memset(now,0,sizeof(now));
        sum[0] = 0;
        for(int i=1;i<=n;i++){
            int x;
            scanf("%d",&x);
            sum[i] = sum[i-1]+x;
            pre[i] = now[x];
            now[x] = i;
        }
        build(1,n,1);
        while(k--){
            int l,r;
            scanf("%d%d",&l,&r);
            LL len = r-l+1;
            LL S = (len+1)*(len)/2;
            if(sum[r]-sum[l-1]!=S){
                printf("NO\n");
                continue;
            }
            MAX_ID = -1;
            query(1,n,l,r,1);
            if(MAX_ID<l) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

HDU - 5172 GTY's gay friends

时间: 2024-08-13 15:12:03

HDU - 5172 GTY's gay friends的相关文章

HDU 5172 GTY&#39;s gay friends (线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意: 给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+1] 的数. 题解: 判断有没有 1 ~ R-L+1 其实就是判断这一段区间和是不是等于 (R-L+1)*(R-L+1+1)/ 2 . 当然还有就是每一个数只能出现1次. 这个问题我们应该怎么解决呢. 我们可以记录第i个数x 的前一个x出现的位置.如果x的前一个x也出现在[L, R]里面,那么这一段

hdu 5172 GTY&#39;s gay friends(线段树最值)

题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO 思路: 先判断是否segma(a[L,R])是否等于(R-L)*(R-L+1)/2. 记录每一个ai上一次的位置pre[i]. 这样只要判断a[L]...a[R]中的每一个pre[i]是否都小于L即可.(这个方法太妙) 线段树区间求最大值. *用map效率明显下降. 代码: int const N

hdu 5172 GTY&#39;s gay friends 线段树

GTY's gay friends 问题描述 GTY有n个基友,出于某种恶趣味,GTY每天早上会让他的基友们排成一行,每个基友有一个特征值,表示基友有多雄壮或娘炮,你,作为GTY的助手,必须回答GTY的每个询问,GTY每次会问一个区间[l,r][l,r]是否为一个11到r-l+1r−l+1的排列. 输入描述 多组数据(约3组),每组数据的第一行有两个数n,m(1 \leq n,m \leq 100000)n,m(1≤n,m≤100000) 表示初始基友数量和询问个数,第二行包含nn个数a_i (

HDU 5172 GTY&#39;s gay friends(线段树)

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 assista

HDU 5172 GTY&#39;s gay friends (预处理+线段树)

题目链接:HDU 5172 GTY's gay friends 题意:给出一串序列,询问[l,r]区间里是否存在1~l-r+1的一个排列. 思路:1~l-r+1的一个排列 等价于 [l,r]中元素互不相同且[l,r]区间和等于(1+len)*len/2(len=l-r+1). 区间和可以用前缀和来处理. 元素互不相同,记录位置i上a[i]上次出现的位置记做pre[i],再用线段树来维护区间的pre最大值,若最大值小于l说明[l,r]中元素互不相同.(区间[l,r]中的每个pre[i]小于l说明区

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

GTY&#39;s gay friends 线段树判断区间是否有相同数字

http://acm.hdu.edu.cn/showproblem.php?pid=5172 判断一个区间是否为全排列是: 1.区间总和 = (1 + R - L + 1) * (R - L + 1) / 2; 2.区间没有重复数字 记录数组a[i]表示第i个数上一次在那个位置出现. 那么最需要在[L, R]中a[i]的最大值 >= L的,就是有重复数字了. #include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false)

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as

hdu 5171 GTY&#39;s birthday gift (BestCoder Round #29)

GTY's birthday gift                                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 209    Accepted Submission(s): 71 Problem Description F