HDU5172 - GTY's gay friends - 哈希

本文介绍了一道算法题目,需要判断给定区间内的数列是否构成特定排列。通过使用哈希技巧,实现O(1)时间复杂度的回答。代码采用C++实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  long _begin_time = clock();
#endif
  srand(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");
      else
        puts("NO");
    }
  }
#ifndef ONLINE_JUDGE
  long _end_time = clock();
  printf("time = %ld ms\n", _end_time - _begin_time);
#endif
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值