GTY's gay friends

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

【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..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 ith number ai ( 1≤ai≤n ) 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 ( 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】

【Sample Output】

YES
NO
YES
YES
YES
YES
NO

【题意】

给出一个数列,询问连续的从l开始到r为止的数是否刚好能够组成从1开始到r-l+1的数列。

 
【分析】
每一次询问都是一个区间询问。
对于每一个区间询问,需要判断区间内的数是否刚好可以组成1到k的连续数列,主要的判断标准有两个:
1.区间数字的总和与(1+k)*k/2相等;
2.保证区间内所有数都只出现一次。
 
第一个可以在读入数据时用前缀和解决。
第二个就要用到线段树了,读入时预处理记录下与当前数相同的数最近一次出现的位置。询问l~r的区间时,检索每个数的最近出现位置位置,若得到的所有结果都在区间左端的左边,那就说明区间中所有的数都是不重复出现的,则满足条件。这里就是用线段树判断区间最大值小于区间左端的过程了。
 
 /* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU5172
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N=1e6+; int last[N],a,sum[N]; typedef struct treetyp
{
int a,b,l,r,data;
} treetype;
treetype tree[*N];
int treetail; void maketree(int l,int r)
{
treetail++;
int now=treetail;
tree[now].a=l;
tree[now].b=r;
if (l<r)
{
tree[now].l=treetail+;
maketree(l,(l+r)/);
tree[now].r=treetail+;
maketree((l+r)/+,r);
}
} void add(int n,int i,int data)
{
if (tree[n].data<data) tree[n].data=data;
if (i==tree[n].a&&i==tree[n].b) return ;
else if (i<=(tree[n].a+tree[n].b)/) add(tree[n].l,i,data);
else add(tree[n].r,i,data);
} int res; void search(int n,int a,int b)
{
if (tree[n].a>=a&&tree[n].b<=b)
{
if (res<tree[n].data) res=tree[n].data;
return ;
}
if (tree[n].a==tree[n].b) return ;
if (a<=(tree[n].a+tree[n].b)/) search(tree[n].l,a,b);
if (b>=(tree[n].a+tree[n].b)/+) search(tree[n].r,a,b);
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
memset(sum,,sizeof(sum));
memset(last,,sizeof(last)); treetail=;
maketree(,n);
for (int i=;i<=n;i++)
{
scanf("%d",&a);
sum[i]=sum[i-]+a;
add(,i,last[a]);
last[a]=i;
} for (int i=;i<=m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if ((r-l+)*(r-l+)/==sum[r]-sum[l-])
{
res=;
search(,l,r);
if (res<l) printf("YES\n");
else printf("NO\n");
} else printf("NO\n");
}
} return ;
}

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. BestCoder Round #29 1003 (hdu 5172) GTY's gay friends [线段树 判不同 预处理 好题]

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

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

  5. hdu 5172 GTY's gay friends

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

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

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

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

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

  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 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

随机推荐

  1. hdu_1115_Lifting the Stone(求多边形重心)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给你N个点围成的一个多边形,让你求这个多边形的重心. 题解: 将多边形划分为若干个三角形. ...

  2. HDU4310:Hero

    Problem Description When playing DotA with god-like rivals and pig-like team members, you have to fa ...

  3. android网络编程之HttpUrlConnection的讲解--DownLoadManager基本用法

    1.DownLoadManager是Android用系统服务(Service)的方式来优化处理长时间的下载操作的一个工具类.避免了我们去处理多线程,通知栏等等. 2.不要忘记添加权限 <uses ...

  4. AIR使用文件对象操作文件和目录

    文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在AIR中可用. 文件对象能做啥? 获取特定目录,包括用户目录.用户文档目录.该应用程序启动的目录和程序目录 ...

  5. Linux网卡配置与绑定

    一定要在服务管理中关闭NetworkManager服务并禁用自动启动. 第一步:先查看下本机网卡,使用命令到network-scripts 下 [root@root~]# cd /etc/syscon ...

  6. 12.04 ubuntu 进入登录界面,账号密码确定是正确的但是进不来系统。

    很简单,HOME目录存储太多东西了,导致开机不了.

  7. E212:无法打开并写入文件

    用vi编辑文件是  老师出现这样的错误 有些文件   需要root权限才能修改   切换成root权限就行了

  8. VB 要求对象

    vDoc = WebBrowser1.Document '提示要求对象 Set vDoc = WebBrowser1.Document '正确执行

  9. 错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ

    错误 1 error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 C:\Users\Administ 这两个fatal error是因为从低版本的WTL到高版本的WTL转变后产生的 ...

  10. log4j打印出线程号和方法名

    先参考实现配置,如果想要更加详细的配置,可加上更多参数: log4j.rootLogger = INFO,FILE,CONSOLE log4j.appender.FILE.Threshold=INFO ...