luogu3674 小清新人渣的本愿 (bitset+莫队)

对于加减,用bitset维护当前每个数有没有

对于乘,暴力枚举约数

然后莫队

复杂度$O(m(\sqrt{n}+\frac{c}{64}))$

 1 #include<bits/stdc++.h>
 2 #define pa pair<ll,ll>
 3 #define CLR(a,x) memset(a,x,sizeof(a))
 4 #define MP make_pair
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=1e5+10;
 8 
 9 inline char gc(){
10     return getchar();
11     static const int maxs=1<<16;static char buf[maxs],*p1=buf,*p2=buf;
12     return p1==p2&&(p2=(p1=buf)+fread(buf,1,maxs,stdin),p1==p2)?EOF:*p1++;
13 }
14 inline ll rd(){
15     ll x=0;char c=gc();bool neg=0;
16     while(c<'0'||c>'9'){if(c=='-') neg=1;c=gc();}
17     while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-'0',c=gc();
18     return neg?(~x+1):x;
19 }
20 
21 int N,V,NN,M,v[maxn];
22 struct Node{
23     int o,l,r,x,i;
24 }q[maxn];
25 bool ans[maxn];
26 int cnt[maxn];
27 bitset<100005> b1,b2;
28 
29 inline bool cmp(Node a,Node b){
30     return a.l/NN==b.l/NN?((a.l/NN)&1?a.r<b.r:a.r>b.r):a.l<b.l;
31 }
32 
33 inline void solve(int p,int d){
34     if(!cnt[v[p]]) b1[v[p]]=1,b2[V-v[p]]=1;
35     cnt[v[p]]+=d;
36     if(!cnt[v[p]]) b1[v[p]]=0,b2[V-v[p]]=0;
37 }
38 
39 int main(){
40     //freopen("","r",stdin);
41     int i,j,k;
42     N=rd(),NN=sqrt(N),M=rd();
43     for(i=1;i<=N;i++) v[i]=rd(),V=max(V,v[i]);
44     for(i=1;i<=M;i++){
45         q[i].o=rd(),q[i].l=rd(),q[i].r=rd(),q[i].x=rd(),q[i].i=i;
46     }sort(q+1,q+M+1,cmp);
47     int l=1,r=0;
48     for(i=1;i<=M;i++){
49         while(r<q[i].r) solve(++r,1);
50         while(r>q[i].r) solve(r--,-1);
51         while(l<q[i].l) solve(l++,-1);
52         while(l>q[i].l) solve(--l,1);
53         if(q[i].o==1){
54             ans[q[i].i]=(b1&(b1<<q[i].x)).count();
55         }else if(q[i].o==2){
56             ans[q[i].i]=(b1&(q[i].x>V?(b2<<(q[i].x-V)):(b2>>(V-q[i].x)))).count();
57         }else{
58             for(j=1;j*j<=q[i].x;j++){
59                 if(q[i].x%j==0&&b1[j]&&b1[q[i].x/j]){
60                     ans[q[i].i]=1;break;
61                 }
62             }
63         }
64     }
65     for(i=1;i<=M;i++){
66         if(ans[i]) printf("hana\n");
67         else printf("bi\n");
68     }
69     return 0;
70 }

 

posted @ 2018-11-27 19:29  Ressed  阅读(160)  评论(0编辑  收藏  举报