【BZOJ3207】花神的嘲讽计划I 可持久化线段树/莫队
看到题目就可以想到hash
然后很自然的联想到可持久化权值线段树
WA:base取了偶数
这道题还可以用莫队做,比线段树快一些
可持久化线段树:
#include<bits/stdc++.h>
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define inf 4294967295
#define N 100005
#define M 100005
#define base 100003
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int rt[N],n,m,k,tot,ls[M*],rs[M*],sz[M*];
uint hash[N],bas;
void add(int pre,int &x,uint l,uint r,uint pos){
x=++tot;ls[x]=ls[pre],rs[x]=rs[pre];sz[x]=sz[pre]+;
if(l==r)return;
uint mid=l/+r/;if((l&)&&(r&))mid++;
if(pos<=mid)add(ls[pre],ls[x],l,mid,pos);
else add(rs[pre],rs[x],mid+,r,pos);
}
int query(int L,int R,uint l,uint r,uint pos){
if(l==r)return sz[R]-sz[L];
uint mid=l/+r/;if((l&)&&(r&))mid++;
if(pos<=mid)query(ls[L],ls[R],l,mid,pos);
else query(rs[L],rs[R],mid+,r,pos);
}
int main(){
n=read();m=read();k=read();
hash[]=;
for(int i=;i<=n;i++)hash[i]=hash[i-]*base+(uint)read();
bas=;
for(int i=;i<=k;i++)bas*=base;
for(int i=;i<=n-k+;i++)add(rt[i-],rt[i],,inf,hash[i+k-]-hash[i-]*bas);
while(m--){
int x=read(),y=read();
uint ha=;
for(int i=;i<=k;i++)ha=ha*base+(uint)read();
query(rt[x-],rt[y-k+],,inf,ha)?puts("No"):puts("Yes");
}
return ;
}
莫队算法:
#include<bits/stdc++.h>
#define ll long long
#define N 100005
#define base 100003
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,a[N],tot,pos[N],cnt[N];
bool ans[N];
ll hash[N],h[N],bas;
struct Node{
int l,r,v,id;
}q[N];
bool cmp(Node a,Node b){
if(pos[a.l]==pos[b.l])return a.r<b.r;
else return a.l<b.l;
}
bool find(int x){
if(x==-)return ;
return cnt[x]?:;
}
int main(){
n=read();m=read();k=read();
for(int i=;i<=n;i++)hash[i]=hash[i-]*base+(ll)read();
bas=;
for(int i=;i<=k;i++)bas*=base;
for(int i=;i<=n-k+;i++)h[++tot]=hash[i+k-]-hash[i-]*bas;
sort(h+,h++tot);
for(int i=;i<=n-k+;i++)a[i]=lower_bound(h+,h++tot,hash[i+k-]-hash[i-]*bas)-h;
int block=sqrt(n);
for(int i=;i<=n;i++)pos[i]=(i-)/block+;
for(int i=;i<=m;i++){
int x=read(),y=read();y=y-k+;
ll tmp=;
for(int j=;j<=k;j++)tmp=tmp*base+(ll)read();
int t=lower_bound(h+,h++tot,tmp)-h;
if(t>n||h[t]!=tmp)t=-;
q[i].l=x,q[i].r=y;q[i].id=i;q[i].v=t;
}
sort(q+,q++m,cmp);
int ql=,qr=;
for(int i=;i<=m;i++){
while(qr<q[i].r)cnt[a[++qr]]++;
while(ql>q[i].l)cnt[a[--ql]]++;
while(qr>q[i].r)cnt[a[qr--]]--;
while(ql<q[i].l)cnt[a[ql++]]--;
ans[q[i].id]=find(q[i].v);
}
for(int i=;i<=m;i++)puts(ans[i]?"No":"Yes");
return ;
}
3207: 花神的嘲讽计划Ⅰ
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1627 Solved: 587
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 4 5 6 7 8
2 5 2 3 4
1 8 3 2 1
5 7 4 5 6
2 5 1 2 3
1 7 3 4 5
Sample Output
Yes
Yes
Yes
No
HINT
【BZOJ3207】花神的嘲讽计划I 可持久化线段树/莫队的更多相关文章
- BZOJ 3207: 花神的嘲讽计划Ⅰ( hash + 可持久化线段树 )
O(NK)暴力搞出所有子串的哈希值, 然后就对哈希值离散化建权值线段树, 就是主席树的经典做法了.总时间复杂度O(NK+(N+Q)logN) ----------------------------- ...
- [bzoj3207][花神的嘲讽计划Ⅰ] (字符串哈希+主席树)
Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...
- BZOJ_3207_花神的嘲讽计划Ⅰ_哈希+主席树
BZOJ_3207_花神的嘲讽计划Ⅰ_哈希+主席树 Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程 ...
- [bzoj3207]花神的嘲讽计划Ⅰ[可持久化线段树,hash]
将每k个数字求一个哈希值,存入可持久化线段树,直接查询即可 #include <iostream> #include <algorithm> #include <cstd ...
- BZOJ3207: 花神的嘲讽计划Ⅰ(hash)
3207: 花神的嘲讽计划Ⅰ Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3569 Solved: 1258[Submit][Status][Di ...
- [BZOJ3207] 花神的嘲讽计划Ⅰ (主席树)
Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...
- BZOJ3207花神的嘲讽计划Ⅰ——主席树+hash
题目描述 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天DJ在给吾等众蒟 ...
- BZOJ3207 花神的嘲讽计划I
Time Limit: 10 Sec Memory Limit: 128 MB Summary 给你一个模式串P,q个询问,对每个询问回答从Pl到Pr是否存在与给定串相同的子串,同时有所有的给定串长度 ...
- BZOJ3207: 花神的嘲讽计划Ⅰ
显然hash,然后stl随便搞. #include<bits/stdc++.h> #define N 100005 using namespace std; typedef unsigne ...
随机推荐
- 【翻译六】java-连接和实例
Joins The join method allows one thread to wait for the completion of another. If t is a Thread obje ...
- Integer Inquiry【大数的加法举例】
Integer Inquiry Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27730 Accepted: 10764 ...
- [LeetCode] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- SSIS Dataflow使用存储过程不能检索列名
在项目中遇到一个问题,需要在Dataflow中调用一个存储过程,然后把结果生成一个csv文件. 然而在dataflow调用存储过程中遇到了问题,SP不能正确的返回列名. 在SSMT里面明明是可以查出数 ...
- 关于cout<<ends你不知道的那些事
关于ends是C++中比较基础的一个东西,但是可能不是每个人都能够清楚的理解这是个什么东西,我就经历了这么一个过程,写出来让大家看看,有什么理解的不对的地方欢迎拍砖. 今天以前我对ends的理解是:输 ...
- The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience
全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...
- WORD2007多级列表
转自玄鸟翩翩 http://hi.baidu.com/shine_yen http://hi.baidu.com/shine_yen/item/01ff2255043bc1aeacc85722 用Wo ...
- 在Salesforce中编写Unit Test
Unit Test 也是一个 Class 文件,所以在创建 Unit Test 的时候要选择 Test Class 类型来创建,请看如下截图(在Eclipse中): 编写 Unit Test 基本流程 ...
- Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note
题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...
- Oracle资源
ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 user:1603869780@qq.compass:qwe123QWE现在直接点击不能下载了 要经过ora ...