1.题目描述:

GTY's gay friends

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

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
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
BestCoder Round #29
Recommend
hujie

2.题意概述:

给出一个数组a[n](1<=a[i]<=n),可能会有重复,然后m组询问 每次询问两个数:l,r 在区间[l,r]内是否构成一个1,2,..,r-l+1的排列;

3.解题思路:

对于[1..n][1..n][1..n]中的每一个数随机一个64位无符号整型作为它的hash值,一个集合的hash值为元素的异或和,预处理[1..n]的排列的hash和原序列的前缀hash异或和,就可以做到线性预处理,O(1)回答询问

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x7fffffff
#define maxn 1001000
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7;
using namespace std;
typedef long long ll;
int n, m;
ll sum[maxn], hs[maxn], val[maxn];
inline ll randval()
{ll one = 1;ll RAND = (rand() + rand()) * (one << 44) + (rand() + rand()) * (one << 33) + (rand() + rand()) * (one << 22) + (rand() + rand());return RAND;
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();
#endifsrand(time(NULL));hs[0] = 0;for (int i = 1; i < maxn; i++){val[i] = randval();hs[i] = hs[i - 1] ^ val[i];}while (scanf("%d%d", &n, &m) != EOF){sum[0] = 0;for (int i = 1; i <= n; i++){int x;scanf("%d", &x);sum[i] = sum[i - 1] ^ val[x];}for (int i = 0; i < m; i++){int l, r;scanf("%d%d", &l, &r);if (hs[r - l + 1] == (sum[r] ^ sum[l - 1]))puts("YES");elseputs("NO");}}
#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms\n", _end_time - _begin_time);
#endifreturn 0;
}

HDU5172 - GTY's gay friends - 哈希相关推荐

  1. hdu5172 GTY‘s gay friends(hash判断排列)

    题意: 给定长度为n的序列a,m个询问 每个询问: 问区间[l,r]中的数是否是1-(r-l+1)的一个排列(全排列中的一种) 数据范围:n<=1e6,1<=a(i)<=n 解法: ...

  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 HASH随机算法

    传送门:点击打开链接 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. GTY's gay friends

    题目链接 英文版 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. 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 ...

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

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

  7. Hdu 5172 GTY's gay friends

    题目链接:点击打开链接 题目大意就是,给一段序列,长度为10^5,然后有10^5个询问,每次询问为一个区间,l为左边界,r为右边界,该区间是否为一个1到r-l+1的一个排列,即该区间是否满足包含1到r ...

  8. GTY‘s gay friends 线段树+前缀和

    链接 题意:n个数,m次询问,每次询问判断一个区间是否为一个序列 解法: 判断长度为x的序列只要满足两个条件: 1.区间和为(1+x)*x/2  2.1~x每个数只出现一次: 对于第一个限制前缀和求区 ...

  9. 【线段树】L - GTY‘s gay friends

    题面 http://acm.hdu.edu.cn/showproblem.php?pid=5172 题意 给你n个数,m次查询,每次询问一个区间[l,r],问这个区间[l,r]是否满足[l,r]之间的 ...

最新文章

  1. Codeforces Round #562 (Div. 2) B. Pairs
  2. 自定义Spinner之IconSpinner
  3. 嘻哈说:开放封闭原则
  4. Ubuntu 安装docker-engine的三种方法
  5. 开发遇到的问题---【spring-security权限控制框架】
  6. jmeter控制器--if控制器
  7. docker源码编译 linux_oracle linux 6 docker 安装(包括编译git源码)
  8. 405 - 不允许用于访问此页的 HTTP 谓词的处理办法
  9. 解除工作压力的四大疗法
  10. 网页设计中常用的HTML代码
  11. Python实战系列-爬取网页内容
  12. 阿里云(飞天)里的 盘古
  13. Pizza店(JAVA程序设计)
  14. [CF1538E] Funny Substrings (模拟)
  15. linux ppm转jpg_python将.ppm格式图片转换成.jpg格式文件的方法
  16. Gaussian Process understanding
  17. hostiko模板-WHMCS自适应模板-略站网
  18. android七牛多张图片上传
  19. HTML5 JavaScript CSS 表单实现购物优惠打折
  20. 简单局域网网络故障排查和处置

热门文章

  1. 镁光闪存颗粒对照表_内存颗粒版本判断方法和编号解析(三星、美光、海力士)...
  2. 2021.icpc网络赛第二场
  3. 【半精度】Pytorch模型加速和减少显存
  4. 怎样用计算机截图,大神教你如何在电脑上视频截图
  5. 代码优雅之路-如何优雅的去除冗杂的if-else语句
  6. 关于有重根情况下微分方程根的一般形式
  7. 端午节-怀念1996之QB45坦克对战游戏
  8. 摩拜单车地图显示的红点是什么_找到最近的摩拜单车——高德地图API的应用
  9. mysql utf8mb4 emoji_mysql utf8mb4与emoji表情[转]
  10. 先谈云计算再谈云大会