BZOJ3209 花神的嘲讽计划I

5 篇文章 0 订阅
3 篇文章 0 订阅

题目简述:
给你一个模式串 P q个询问,对每个询问回答从 Pl Pr 是否存在与给定串相同的子串,同时有所有的给定串长度均为 k(k20)
数据范围: n,m200000

题目挺裸,想法也很裸,但需要一些注意的地方
首先匹配这种事情肯定是直接 hash 之,因为出现的串数量较大,所以用自然溢出的unsigned long long(其实根据hash killer III的经验,用双质数hash更安全)。然后以原序列的每个长为k的子串的 hash 值建主席树,查询就变成了在区间中是否存在相应数。
然后下面是关键,在求区间中点时不能用(l+r)>>1,因为加起来会炸,所以要换成(l>>1)+(r>>1)+(r&l&1)。

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;
typedef double db;

int getint()
{
    int f=1,g=0;char c=getchar();
    while(c<'0' || c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0' && c<='9')g=(g<<3)+(g<<1)+c-'0',c=getchar();
    return f*g;
}

const int base=103;
const int maxn=200005;

const ll inf=18446744073709551615ul;

ll bin[maxn];
ll ha[maxn];
int n,m,k;

ll hash(int l,int r)
{
    return ha[r]-ha[l-1]*bin[r-l+1];
}

int root[maxn];
int lc[maxn*43];
int rc[maxn*43];
int sum[maxn*43];
int tot;

#define mid ( (l>>1)+(r>>1)+(r&l&1) )

void up(int x){sum[x]=sum[lc[x]]+sum[rc[x]];}
void insert(int &x,int last,ll l,ll r,ll val)
{
    if(!x)x=++tot;
    sum[x]=sum[last];
    if(l==r)
    {
        sum[x]++;
        return;
    }
    if(val<=mid)insert(lc[x],lc[last],l,mid,val),rc[x]=rc[last];
    else insert(rc[x],rc[last],mid+1,r,val),lc[x]=lc[last];
    sum[x]=sum[lc[x]]+sum[rc[x]];
}

int query(int now,int last,ll l,ll r,ll val)
{
    if(sum[now]-sum[last]==0)return 0;
    if(l==r)
    {
        return sum[now]-sum[last];
    }
    if(val<=mid)return query(lc[now],lc[last],l,mid,val);
    else return query(rc[now],rc[last],mid+1,r,val);
}

int main()
{
//  freopen("taunt5.in","r",stdin);
//  freopen("out5.txt","w",stdout);

    n=getint();
    m=getint();
    k=getint();

    ll temp;

    bin[0]=1;
    for(int i=1;i<maxn;i++)bin[i]=bin[i-1]*base;

    for(int i=1;i<=n;i++)
    {
        temp=getint();
        ha[i]=ha[i-1]*base+temp;
    }

    for(int i=1;i<=n-k+1;i++)
    {
        ll temp=hash(i,i+k-1);
        insert(root[i],root[i-1],0,inf,temp);
    }

    for(int i=1;i<=m;i++)
    {
        int l=getint();
        int r=getint();
        r=r-k+1;
        temp=0;
        if(r<l)
        {
            puts("Yes");
            continue;
        }
        for(int j=1;j<=k;j++)
        {
            temp=temp*base+getint();
        }
        if(query(root[r],root[l-1],0,inf,temp))
        {
            puts("No");
        }
        else 
        {
            puts("Yes");
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值