Luogu P3674 The original wish of a small fresh scum

Click me if you want to see the title .
I just found this subject to be very difficult.
Until I learned from Awson that there is an STL called bitset, this problem was easily solved. Poke me for
wondering about this amazing bitset . At first glance, this topic feels like Mo team,
In fact, someone told me that it is not easy to divide into pieces..
Subtraction: ab=x => a=x+b; is to shift x bits to the right on the weight array.
Addition is the same.
As for multiplication, the direct violence to find the factor is the sqrt(n) complexity anyway.
The time complexity is obviously: O(n^2/64+n*sqrt(n))
code:

#include <bits/stdc++.h>
using namespace std;

const int N=100010;
struct ask {
    int opt,l,r,x,ans,id,ord;
}q[N];
bitset <N> S1,S2;
int n,m,a[N],L=1,R,len,cnt[N];

bool cmp1(ask s,ask t)
{
    return s.id==t.id?s.r<t.r:s.id<t.id;
}

bool cmp2(ask s,ask t)
{
    return s.ord<t.ord;
}

void del(int i)
{
    if (!--cnt[i]) S1[i]=S2[N-i]=0;
}

void ins(int i)
{
    if (!cnt[i]++) S1[i]=S2[N-i]=1;
}

void Mo(int i)
{
    while (L<q[i].l) del(a[L++]);
    while (L>q[i].l) ins(a[--L]);
    while (R<q[i].r) ins(a[++R]);
    while (R>q[i].r) del(a[R--]);
    if (q[i].opt==1) q[i].ans=(S1>>q[i].x&S1).any();
    if (q[i].opt==2) q[i].ans=(S2>>(N-q[i].x)&S1).any();
    if (q[i].opt==3) {
        for (int j=1;j*j<=q[i].x;j++)
        if (q[i].x%j==0&&(S1[j]&S1[q[i].x/j])) {
            q[i].ans=1;break;
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    len=sqrt(n);
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int i=1;i<=m;i++) {
        cin>>q[i].opt>>q[i].l>>q[i].r>>q[i].x;
        q[i].id=q[i].l/len;q[i].ord=i;
    }
    sort(q+1,q+1+m,cmp1);
    for (int i=1;i<=m;i++) Mo(i);
    sort(q+1,q+1+m,cmp2);
    for (int i=1;i<=m;i++)
    q[i].ans?puts("hana"):puts("bi");
    return 0;
}

Bitset Dafa is good!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324825138&siteId=291194637