hdu 5172(线段树||HASH)
GTY's gay friends
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1379 Accepted Submission(s): 355
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.
), 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.
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
题意:
n个数m个询问,询问(l,r)中的数是否为1 ~ r-l+1的一个排列。
分析:
若(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 = ;
int pre[N],now[N];
LL sum[N];
struct Tree{
int MAX_ID;
}tree[N<<];
int MAX_ID;
void PushUp(int idx){
tree[idx].MAX_ID = max(tree[idx<<].MAX_ID,tree[idx<<|].MAX_ID);
}
void build(int l,int r,int idx){
if(l==r){
tree[idx].MAX_ID = pre[l];
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
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)>>;
if(mid>=L) query(l,mid,L,R,idx<<);
if(mid<R) query(mid+,r,L,R,idx<<|);
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
memset(now,,sizeof(now));
sum[] = ;
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
sum[i] = sum[i-]+x;
pre[i] = now[x];
now[x] = i;
}
build(,n,);
while(k--){
int l,r;
scanf("%d%d",&l,&r);
LL len = r-l+;
LL S = (len+)*(len)/;
if(sum[r]-sum[l-]!=S){
printf("NO\n");
continue;
}
MAX_ID = -;
query(,n,l,r,);
if(MAX_ID<l) printf("YES\n");
else printf("NO\n");
}
}
return ;
}
不过我们还有更简单的hash做法,对于[1..n][1..n][1..n]中的每一个数随机一个64位无符号整型作为它的hash值,一个集合的hash值为元素的异或和,预处理[1..n]的排列的hash和原序列的前缀hash异或和,就可以做到线性预处理,O(1)O(1)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[],a[],ha[];
int n,m,t,p; //int main(int argc,char const *argv[])
int main()
{
srand(time(NULL));
a[] = ha[] = ;
for(int i=; i<=; i++)
{
ra[i] = rand()*rand();
ha[i] = ha[i-]^ra[i];
}
for(int i=; i<=; i++)
{
printf("%lld\n",ra[i]);
}
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<=n; i++)
{
scanf("%d",&t);
a[i] = ra[t];
a[i]^=a[i-];
}
for(int i=; i<=m; i++)
{
scanf("%d%d",&t,&p);
puts((ha[p-t+]==(a[p]^a[t-])?"YES":"NO"));
}
}
return ;
}
hdu 5172(线段树||HASH)的更多相关文章
- GTY's gay friends HDU - 5172 线段树
GTY has nn gay friends. To manage them conveniently, every morning he ordered all his gay friends to ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu 3974 线段树 将树弄到区间上
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3436 线段树 一顿操作
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu 3397 线段树双标记
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 4578 线段树(标记处理)
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) ...
- hdu 4533 线段树(问题转化+)
威威猫系列故事——晒被子 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu 2871 线段树(各种操作)
Memory Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- json.dumps错误:'utf8' codec can't decode byte解决方案
一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...
- java正则表达式 3 -- 查找
用正则表达式执行查找命令,则需要用正则对象,其规则和执行顺序如下: 指定为字符串的正则表达式必须首先被便以为此类的实例.然后,可将得到的正则对象匹配任意的字符串用于创建Mather对象,执行匹配所涉及 ...
- web相关基础知识3
一 .浮动布局 ★元素浮动之后不占据原来的位置,脱离标准流 ★浮动的盒子在一行上显示 ★行内元素浮动之后转换为行内块元素.(不推荐使用,会脱离标准流,转行内元素最好使用display: inlin ...
- JDBC数据源的驱动问题
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle ...
- Nginx学习笔记之应用篇
Nginx服务器的安装请参考Nginx学习笔记之安装篇 关于Nginx配置文档的API在这里就不一一列出,现在我们来配置第一个Nginx架构实现负载均衡的网站. 1.打开IIS,配置如下站点 重复上述 ...
- 解决Git无法同步空文件夹的问题
思路:在每个空文件夹下创建空文件,同步后再删除 package org.zln.module1.demo1; import org.apache.log4j.Logger; import java.i ...
- C&C++——段错误(Segmentation fault)
C/C++中的段错误(Segmentation fault) Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的.来自:http://o ...
- nfs 和samba
NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS. NFS允许一个系统在网络上与他人共享目录和文件.通过使用N ...
- [poj 3252]数位dp前导0的处理
通过这个题对于数位dp中前导0的处理有了新的认识. 题目链接:http://poj.org/problem?id=3252 //http://poj.org/problem?id=3252 #incl ...
- URAL1277 Cops and Thieves(最小割)
Cops and Thieves Description: The Galaxy Police (Galaxpol) found out that a notorious gang of thieve ...