BZOJ 3207: 花神的嘲讽计划Ⅰ(莫队+哈希)

传送门

解题思路

  刚开始写了个莫队+哈希+\(map\)\(O(n\sqrt(n)log(n)\)的辣鸡做法,\(T\)飞了。后来看了看别人博客发现其实并不用拿\(map\)当桶存那些哈希值。因为只有\(n-k+1\)个哈希值,可以提前预处理出来,然后离散化。时间复杂度\(O(n\sqrt(n))\),比主席树都快。

代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
 
using namespace std;
typedef unsigned long long ull;
const int N=100005;
const int base=666623333;
 
inline int rd(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) f=ch=='-'?0:1,ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return f?x:-x;
}
 
int n,m,k,ans[N],siz,cnt[N],tot;
ull hsh[N],tb[N],a[N],cpy[N];
 
struct Query{
    int l,r,id;
    ull w;
    friend bool operator<(const Query A,const Query B){
        if(A.l/siz!=B.l/siz) return A.l<B.l;
        if((A.l/siz)&1) return A.r>B.r;
        return A.r<B.r;
    }
}q[N];
 
inline void del(int x){
    if(x+k-1>n) return;
    cnt[a[x]]--;
}
 
inline void add(int x){
    if(x+k-1>n) return;
    cnt[a[x]]++;
}
 
int main(){
    ull HSH; int x,y,pos; bool flag;
    n=rd(),m=rd(),k=rd(); tb[0]=1; siz=sqrt(n);
    for(int i=1;i<=n;i++){
        x=rd();
        hsh[i]=hsh[i-1]*base+x;
    }
    for(int i=1;i<=k;i++) tb[i]=tb[i-1]*base;
    for(int i=1;i<=n-k+1;i++) a[++tot]=hsh[i+k-1]-hsh[i-1]*tb[k],cpy[tot]=a[tot];
    sort(cpy+1,cpy+1+tot); int u=unique(cpy+1,cpy+1+tot)-cpy-1;
    for(int i=1;i<=tot;i++) a[i]=lower_bound(cpy+1,cpy+1+u,a[i])-cpy;
    for(int i=1;i<=m;i++){
        x=rd(),y=rd(); flag=false; HSH=0; y=y-k+1;
        for(register int j=1;j<=k;j++) HSH=HSH*base+rd();
        pos=lower_bound(cpy+1,cpy+1+u,HSH)-cpy;
        if(cpy[pos]!=HSH) q[i].w=0;
        else q[i].w=pos;
        q[i].l=x; q[i].r=y; q[i].id=i;
    }
    sort(q+1,q+1+m); int L=1,R=0;
    for(int i=1;i<=m;i++){
        while(L<q[i].l) {del(L); L++;}
        while(L>q[i].l) {L--; add(L);}
        while(R<q[i].r) {R++; add(R);}
        while(R>q[i].r) {del(R); R--;}
        ans[q[i].id]=(cnt[q[i].w]>0)?0:1;
    }
    for(int i=1;i<=m;i++) puts(ans[i]?"Yes":"No");
    return 0;
}

转载于:https://www.cnblogs.com/sdfzsyq/p/10304169.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值