【BZOJ3207】花神的嘲讽计划I 可持久化线段树/莫队

看到题目就可以想到hash

然后很自然的联想到可持久化权值线段树

WA:base取了偶数

这道题还可以用莫队做,比线段树快一些

可持久化线段树:

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define uint unsigned int
 4 #define ull unsigned long long
 5 #define inf 4294967295
 6 #define N 100005 
 7 #define M 100005
 8 #define base 100003
 9 using namespace std;
10 inline int read(){
11   int x=0,f=1;char ch=getchar();
12   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
13   while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
14   return x*f;
15 }
16 int rt[N],n,m,k,tot,ls[M*40],rs[M*40],sz[M*40];
17 uint hash[N],bas;
18 void add(int pre,int &x,uint l,uint r,uint pos){
19   x=++tot;ls[x]=ls[pre],rs[x]=rs[pre];sz[x]=sz[pre]+1;
20   if(l==r)return;
21   uint mid=l/2+r/2;if((l&1)&&(r&1))mid++; 
22   if(pos<=mid)add(ls[pre],ls[x],l,mid,pos);
23   else add(rs[pre],rs[x],mid+1,r,pos);
24 }
25 int query(int L,int R,uint l,uint r,uint pos){
26   if(l==r)return sz[R]-sz[L];
27   uint mid=l/2+r/2;if((l&1)&&(r&1))mid++;
28   if(pos<=mid)query(ls[L],ls[R],l,mid,pos);
29   else query(rs[L],rs[R],mid+1,r,pos);
30 }
31 int main(){
32   n=read();m=read();k=read();
33   hash[0]=0;
34   for(int i=1;i<=n;i++)hash[i]=hash[i-1]*base+(uint)read();
35   bas=1;
36   for(int i=1;i<=k;i++)bas*=base;
37   for(int i=1;i<=n-k+1;i++)add(rt[i-1],rt[i],1,inf,hash[i+k-1]-hash[i-1]*bas);
38   while(m--){
39     int x=read(),y=read();
40     uint ha=0;
41     for(int i=1;i<=k;i++)ha=ha*base+(uint)read();
42     query(rt[x-1],rt[y-k+1],1,inf,ha)?puts("No"):puts("Yes"); 
43   }
44   return 0;
45 }
View Code

莫队算法:

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 #define N 100005
 4 #define base 100003
 5 using namespace std;
 6 inline int read(){
 7   int x=0,f=1;char ch=getchar();
 8   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 9   while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
10   return x*f;
11 }
12 int n,m,k,a[N],tot,pos[N],cnt[N];
13 bool ans[N];
14 ll hash[N],h[N],bas;
15 struct Node{
16   int l,r,v,id;
17 }q[N];
18 bool cmp(Node a,Node b){
19   if(pos[a.l]==pos[b.l])return a.r<b.r;
20   else return a.l<b.l;
21 }
22 bool find(int x){
23   if(x==-1)return 0;
24   return cnt[x]?1:0;
25 }
26 int main(){
27   n=read();m=read();k=read();
28   for(int i=1;i<=n;i++)hash[i]=hash[i-1]*base+(ll)read();
29   bas=1;
30   for(int i=1;i<=k;i++)bas*=base;
31   for(int i=1;i<=n-k+1;i++)h[++tot]=hash[i+k-1]-hash[i-1]*bas;
32   sort(h+1,h+1+tot);
33   for(int i=1;i<=n-k+1;i++)a[i]=lower_bound(h+1,h+1+tot,hash[i+k-1]-hash[i-1]*bas)-h;
34   int block=sqrt(n);
35   for(int i=1;i<=n;i++)pos[i]=(i-1)/block+1;
36   for(int i=1;i<=m;i++){
37     int x=read(),y=read();y=y-k+1;
38     ll tmp=0;
39     for(int j=1;j<=k;j++)tmp=tmp*base+(ll)read();
40     int t=lower_bound(h+1,h+1+tot,tmp)-h;
41     if(t>n||h[t]!=tmp)t=-1;
42     q[i].l=x,q[i].r=y;q[i].id=i;q[i].v=t;
43   }
44   sort(q+1,q+1+m,cmp);
45   int ql=1,qr=0;
46   for(int i=1;i<=m;i++){
47     while(qr<q[i].r)cnt[a[++qr]]++;
48     while(ql>q[i].l)cnt[a[--ql]]++;
49     while(qr>q[i].r)cnt[a[qr--]]--;
50     while(ql<q[i].l)cnt[a[ql++]]--;
51     ans[q[i].id]=find(q[i].v);
52   }
53   for(int i=1;i<=m;i++)puts(ans[i]?"No":"Yes");
54   return 0;
55 }
View Code

3207: 花神的嘲讽计划Ⅰ

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1627  Solved: 587
[Submit][Status][Discuss]

Description

背景
花神是神,一大癖好就是嘲讽大J,举例如下:
“哎你傻不傻的!【hqz:大笨J】”
“这道题又被J屎过了!!”
“J这程序怎么跑这么快!J要逆袭了!”
……
描述
这一天DJ在给吾等众蒟蒻讲题,花神在一边做题无聊,就跑到了一边跟吾等众蒟蒻一起听。以下是部分摘录:
1.
“J你在讲什么!”
“我在讲XXX!”
“哎你傻不傻的!这么麻烦,直接XXX再XXX就好了!”
“……”
2.
“J你XXX讲过了没?”
“……”
“那个都不讲你就讲这个了?哎你傻不傻的!”
“……”
DJ对这种情景表示非常无语,每每出现这种情况,DJ都是非常尴尬的。
经过众蒟蒻研究,DJ在讲课之前会有一个长度为N方案,我们可以把它看作一个数列;
同样,花神在听课之前也会有一个嘲讽方案,有M个,每次会在x到y的这段时间开始嘲讽,为了减少题目难度,每次嘲讽方案的长度是一定的,为K。
花神嘲讽DJ让DJ尴尬需要的条件:
在x~y的时间内DJ没有讲到花神的嘲讽方案,即J的讲课方案中的x~y没有花神的嘲讽方案【这样花神会嘲讽J不会所以不讲】。
经过众蒟蒻努力,在一次讲课之前得到了花神嘲讽的各次方案,DJ得知了这个消息以后欣喜不已,DJ想知道花神的每次嘲讽是否会让DJ尴尬【说不出话来】。
 

Input

第1行3个数N,M,K;
第2行N个数,意义如上;
第3行到第3+M-1行,每行K+2个数,前两个数为x,y,然后K个数,意义如上;

Output

对于每一个嘲讽做出一个回答会尴尬输出‘Yes’,否则输出‘No’

Sample Input

8 5 3
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

No
Yes
Yes
Yes
No

HINT

题中所有数据不超过2*10^9;保证方案序列的每个数字<=N

2~5中有2 3 4的方案,输出No,表示DJ不会尴尬

1~8中没有3 2 1的方案,输出Yes,表示DJ会尴尬

5~7中没有4 5 6的方案,输出Yes,表示DJ会尴尬

2~5中没有1 2 3的方案,输出Yes,表示DJ会尴尬

1~7中有3 4 5的方案,输出No,表示DJ不会尴尬
原文地址:https://www.cnblogs.com/wjyi/p/5568638.html