BZOJ 3207 Flora mockery plan I (hash)

The meaning of problems

The array 1e5, 1e5 inquiry, each identical to the length of your (<= 20) of a group number, and (l, r), the number of groups that you ask is not within the substring (l, r)

Thinking

I think the idea is just fine ..
hash out (i, i + k-1 ) of the substring, then the Chairman of each inquiry in the tree, but do not know why wa .. has been a lot of building his own set of data did not throw .. error (the error is a hash modulus stupid)
online stl seen before, found themselves spread over the
first array in the same hash value set thrown in, just to ask each string in the set of hash in lower_bound (l), which determines the presence and <= r-k + 1 can

It seems to sum up: to determine whether there is a value in a range, set in half on the line?

Code

Range in half:

int t;
int n,m,k;
ll a[maxn];
ull pre[maxn];
ull po[maxn];
ll gt(int l, int r){
    return (pre[r]-pre[l-1]*po[r-l+1]);
}
vector<ll>v;
int getid(ll x){
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
set<int>s[maxn];
int main() {
    po[0]=1;
    scanf("%d %d %d", &n, &m, &k);
    for(int i = 1; i <= n; i++)po[i]=po[i-1]*1337;
    for(int i = 1; i <= n; i++){
        scanf("%lld", &a[i]);
        pre[i]=pre[i-1]*1337+a[i];
    }
    for(int i = 1; i+k-1<=n; i++){
        v.pb(gt(i,i+k-1));
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    int ntot = v.size();
    for(int i = 1; i+k-1<=n; i++){
        a[i]=getid(gt(i,i+k-1));
        s[a[i]].insert(i);
    }
    while(m--){
        int x,y;
        ll tmp = 0;
        scanf("%d %d", &x, &y);
        for(int i = 1; i <= k; i++){
            int o;
            scanf("%d", &o);
            tmp=tmp*1337+o;
        }
        int id = getid(tmp);
        if(v[id-1]!=tmp){
            printf("Yes\n");
            continue;
        }
        set<int>::iterator it = s[id].lower_bound(x);
        if(it!=s[id].end()&&*it<=y-k+1)printf("No\n");
        else printf("Yes\n");

    }

    return 0;
}

Chairman of the tree:

int t;
int n,m,k;
ll a[maxn];
ull pre[maxn];
ull po[maxn];
ull gt(int l, int r){
    return (pre[r]-pre[l-1]*po[r-l+1]);
}
vector<ull>v;
int getid(ull x){
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
int ls[40*maxn],rs[40*maxn];
ll dat[40*maxn];
int root[maxn];
int tot;
int insert(int now, int l, int r, int x, int val){
    int p = ++tot;
    ls[p]=ls[now];rs[p]=rs[now];dat[p]=dat[now];
    if(l==r){
        dat[p]=1;return p;
    }
    int mid = l+r>>1;
    if(x<=mid)ls[p]=insert(ls[now],l,mid,x,val);
    else rs[p]=insert(rs[now],mid+1,r,x,val);
    dat[p]=dat[ls[p]]+dat[rs[p]];
    return p;
}
int ck(int x, int y, int l, int r, int k){
    int mid =l+r>>1;
    if(l==r)return dat[y]-dat[x];
    if(k<=mid){
        return ck(ls[x],ls[y],l,mid,k);
    }
    else return ck(rs[x],rs[y],mid+1,r,k);

}

int main() {
    //freopen("wrjAc.in","r",stdin);
    //freopen("wrjMy.out","w",stdout);
    scanf("%d %d %d", &n, &m, &k);
    po[0]=1;
    for(int i = 1; i <= n; i++)po[i]=po[i-1]*1337;
    for(int i = 1; i <= n; i++){
        scanf("%lld", &a[i]);
        pre[i]=pre[i-1]*1337+a[i];
    }
    for(int i = 1; i+k-1<=n; i++){
        v.pb(gt(i,i+k-1));
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    int ntot = v.size();
    for(int i = 1; i+k-1<=n; i++){
        a[i]=getid(gt(i,i+k-1));
        root[i]=insert(root[i-1],1,ntot,a[i],1);
    }
    while(m--){
        int x,y;
        ull tmp = 0;
        scanf("%d %d", &x, &y);
        for(int i = 1; i <= k; i++){
            int o;
            scanf("%d", &o);
            tmp=tmp*1337+o;
        }
        int id = getid(tmp);
        if(v[id-1]!=tmp){
            printf("Yes\n");
            continue;
        }

        int ans = ck(root[x-1],root[y-k+1],1,ntot,id);
        if(ans>0)printf("No\n");
        else printf("Yes\n");

    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wrjlinkkkkkk/p/12596096.html