传送门

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 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≤100000

), 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

转自官方题解:http://bestcoder.hdu.edu.cn/

1003 GTY's gay friends
一个区间是排列只需要区间和为len(len+1)2   (len  为区间长度),且互不相同,对于第一个问题我们用前缀和解决,对于第二个问题,预处理每个数的上次出现位置,记它为pre,互不相同即区间中pre的最大值小于左端点,使用线段树或Sparse Table即可在O(n)/O(nlogn)  的预处理后 O(logn)/O(1)  回答每个询问.不过我们还有更简单的hash做法,对于[1..n]  中的每一个数随机一个64位无符号整型作为它的hash值,一个集合的hash值为元素的异或和,预处理[1..n]  的排列的hash和原序列的前缀hash异或和,就可以做到线性预处理,O(1)  回答询问.
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 1000005
#define M 105
#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll n,m;
ll a[N];
ll f[N];
ll pr[N];
ll sum[N]; ll mapre[*N]; ll build(ll i,ll l,ll r)
{
if(l==r){
mapre[i]=pr[l];
//printf(" i=%I64d l=%I64d mapre=%I64d pr=%I64d\n",i,l,mapre[i],pr[l]);
return mapre[i];
}
ll mid=(l+r)/;
ll lm=build(*i,l,mid);
ll rm=build(*i+,mid+,r);
mapre[i]=max(lm,rm);
//printf(" i=%I64d mapre=%I64d\n",i,mapre[i]);
return mapre[i];
} ll query(ll i,ll L,ll R,ll l,ll r)
{
if(l>=L && r<=R){
return mapre[i];
}
ll mid=(l+r)/;
ll lm,rm;
lm=;rm=;
if(L<=mid){
lm=query(i*,L,R,l,mid);
}
if(R>mid){
rm=query(i*+,L,R,mid+,r);
}
return max(lm,rm);
} void ini()
{
ll i;
sum[]=;
memset(f,,sizeof(f));
for(i=;i<=n;i++){
scanf("%I64d",&a[i]);
sum[i]=sum[i-]+a[i];
}
//for(i=0;i<=n;i++){
// printf(" i=%I64d sum=%I64d\n",i,sum[i]);
// }
for(i=;i<=n;i++){
pr[i]=f[ a[i] ];
f[ a[i] ]=i;
}
// for(i=0;i<=n;i++){
// printf(" i=%I64d pr=%I64d\n",i,pr[i]);
// }
build(,,n);
} void solve()
{
ll x,y;
ll ss;
ll len;
ll qu;
while(m--){
scanf("%I64d%I64d",&x,&y);
len=y-x+;
ss=sum[y]-sum[x-];
//printf(" ss=%I64d sum=%I64d\n",ss,len*(len+1)/2);
if(ss==(len*(len+)/)){
qu=query(,x,y,,n);
if(qu<x){
printf("YES\n");
}
else{
printf("NO\n");
}
}
else{
printf("NO\n");
}
}
} void out()
{ } int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
//scanf("%d%d",&n,&m);
while(scanf("%I64d%I64d",&n,&m)!=EOF)
{
ini();
solve();
out();
}
return ;
}

BestCoder Round #29 1003 (hdu 5172) GTY's gay friends [线段树 判不同 预处理 好题]的更多相关文章

  1. HDU 5172 GTY's gay friends 线段树+前缀和+全排列

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...

  2. HDU 5172 GTY's gay friends 线段树

    GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. HDU 5172 GTY's gay friends (线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意: 给你一个n个数的数组,m次询问,询问在[L, R] 这个区间里面有没有 [1, R-L+ ...

  4. hdu 5172 GTY's gay friends

    GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...

  5. hdu 5172 GTY's gay friends(线段树最值)

    题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO ...

  6. GTY's gay friends 线段树判断区间是否有相同数字

    http://acm.hdu.edu.cn/showproblem.php?pid=5172 判断一个区间是否为全排列是: 1.区间总和 = (1 + R - L + 1) * (R - L + 1) ...

  7. 从lca到树链剖分 bestcoder round#45 1003

    bestcoder round#45 1003 题,给定两个点,要我们求这两个点的树上路径所经过的点的权值是否出现过奇数次.如果是一般人,那么就是用lca求树上路径,然后判断是否出现过奇数次(用异或) ...

  8. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  9. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. Kubernetes介绍与特性

    1.Kubernetes 是什么 简单的来说,k8s可以理解为,一个容器平台,一个微服务平台,便携式云平台,我们那可以很快速的搭建一个服务,快速的运行起来 2.Kubernetes特性

  2. 2、Task 使用 ContinueWith 而不要使用 Wait

    1.线程自旋:在阻塞线程的时候为了等待解锁(访问临界资源)(Sleep). 2.上下文切换:将处理器当前线程的状态保存到操作系统内部的线程对象中,然后再挑出一个就绪的线程,把上下文信息传递给处理器,然 ...

  3. PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)

    http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...

  4. Bootstrap 网格系统(Grid System)实例1

    Bootstrap 网格系统(Grid System)实例:堆叠水平 <!DOCTYPE html><html><head><meta http-equiv= ...

  5. Web开发面临的挑战主要有哪些?

    摘要:要成为一名高效的Web开发者,这需要我们做很多工作,来提高我们的工作方式,以及改善我们的劳动成果.而在开发中难免会遇到一些困难,从前端到后端. 导读:要成为一名高效的Web开发者,这需要我们做很 ...

  6. 【最短路径树】51nod1443 路径和树

    并不是什么高端操作并且一些模型会用到 Description 给定一幅无向带权连通图G = (V, E) (这里V是点集,E是边集).从点u开始的最短路径树是这样一幅图G1 = (V, E1),其中E ...

  7. [POJ] 3362 Telephone Lines

    Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7978 Accepted: 2885 Descr ...

  8. 暴力解说之首次部署NGINX

    前言 本章基于Centos 7.x系统讲解 本章讲解下在项目上线部署的时候对NGINX的操作.有些童鞋在网上百度类似LNMP安装就跟着命令一条一条执行了,如果没报错还好,一旦报错就懵逼状态了.这是对自 ...

  9. 在centos7中为php7安装redis扩展

    在此之前一直是用php5.6,安装redis也没遇到啥问题,嗖嗖的就安装上了 更新php版本到php7后,编译的时候报错 include <ext/standard/php_smart_str. ...

  10. centos 7.3 快速安装ceph

    Ceph的部署手册(Centos7.3)     Ceph简介 Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统. 部署逻辑架构 准备3台主机,并且修改主机名(hostnam ...