#bitset优化,莫队#洛谷 5355 [Ynoi2017] 由乃的玉米田
分析
只针对除法而言,如果商很大直接用bitset判断是否存在,
否则直接预处理最近的答案判断是否在区间内即可,注意0要特判
代码
#include <cstdio>
#include <cctype>
#include <bitset>
#include <algorithm>
#define rr register
using namespace std;
const int N=100011,base=316; bitset<N>uk,ku;
struct five{int opt,l,r,x,rk,Is;}q[N];
int kuai[N],Sqrt[N],a[N],ans[N],m,n,Q,CNT[N],last[N],mx[N];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
bool cmp(five a,five b){
if (a.Is&&b.Is) return a.x>b.x;
if (a.Is||b.Is) return b.Is;
if (kuai[a.l]^kuai[b.l]) return a.l<b.l;
if (kuai[a.r]^kuai[b.r]) return kuai[a.l]&1?a.r<b.r:a.r>b.r;
return (kuai[a.l]^kuai[a.r])&1?a.x<b.x:a.x>b.x;
}
inline signed max(int a,int b){return a>b?a:b;}
inline void add(int now){if (++CNT[now]==1) uk[now]=ku[N-now-1]=1;}
inline void del(int now){if (--CNT[now]==0) uk[now]=ku[N-now-1]=0;}
signed main(){
n=iut(); Q=iut();
for (rr int i=1;i<=base;++i) Sqrt[i*i]=i;
for (rr int i=1;i<N;++i) if (!Sqrt[i]) Sqrt[i]=Sqrt[i-1];
for (rr int i=1;i<=n;++i) a[i]=iut(),kuai[i]=(i-1)/base+1;
for (rr int i=1;i<=Q;++i) q[i]=(five){iut(),iut(),iut(),iut(),i,0},q[i].Is=q[i].opt==4&&q[i].x<=base;
sort(q+1,q+1+Q,cmp),m=Q; for (;m&&q[m].Is;--m);
for (rr int l=m+1,r;l<=Q;l=r+1){
for (r=l;r<=Q&&q[r].x==q[l].x;++r); --r;
if (!q[l].x){
for (rr int i=1,now=0;i<=n;++i){
if (!a[i]) now=i;
mx[i]=now;
}
for (rr int i=l;i<=r;++i){
if (q[i].l==q[i].r) continue;
if (q[i].l<=mx[q[i].r])
ans[q[i].rk]=1;
}
continue;
}
for (rr int i=0;i<N;++i) last[i]=0;
for (rr int i=1,now=0;i<=n;++i){
last[a[i]]=i;
if (a[i]*q[l].x<N) now=max(now,last[a[i]*q[l].x]);
if (a[i]%q[l].x==0) now=max(now,last[a[i]/q[l].x]);
mx[i]=now;
}
for (rr int i=l;i<=r;++i)
if (q[i].l<=mx[q[i].r])
ans[q[i].rk]=1;
}
for (rr int i=1,L=q[1].l,R=L-1;i<=m;++i){
while (L>q[i].l) add(a[--L]);
while (L<q[i].l) del(a[L++]);
while (R>q[i].r) del(a[R--]);
while (R<q[i].r) add(a[++R]);
switch (q[i].opt){
case 1:ans[q[i].rk]=(uk&(uk<<q[i].x)).any(); break;
case 2:ans[q[i].rk]=(uk&(ku>>(N-q[i].x-1))).any(); break;
case 3:{
if (!q[i].x&&uk[q[i].x]) {ans[q[i].rk]=1; break;}
for (rr int j=1;j<=Sqrt[q[i].x];++j)
if (q[i].x%j==0&&uk[j]&&uk[q[i].x/j]){
ans[q[i].rk]=1; break;
}
break;
}
case 4:{
rr int t=(N-1)/q[i].x;
for (rr int j=1;j<=t;++j)
if (uk[j]&&uk[j*q[i].x]){
ans[q[i].rk]=1; break;
}
break;
}
}
}
for (rr int i=1;i<=Q;++i)
if (ans[i]) printf("yuno\n");
else printf("yumi\n");
return 0;
}