Luo Valley # P3674 # small fresh scum of the willing

Small fresh scum of the willing

Given a length \ (n-\) array \ (A \) , there are three query:
1. inquire about the \ (A_i A_j = X + (I, J \ in [L, R & lt]) \)
2. inquire about the \ (A_i-A_j = X (I, J \ in [L, R & lt]) \)
3. inquire about the \ (A_i × A_j = x ( i, j \ in [l, r]) \)

solution

We use Mo team to solve this question
for subtraction, \ (A_i-A_j = X \) reduces to \ (A_i = A_j + x \
) maintains some value within a range bitset memory, equivalent to s & (s < <x) the presence of a \ (1 \)
for the addition, \ (A_i A_j = X + \) reduces to \ (A_i- (N-A_j)
= xN \) and subtraction, as a safeguard memory bitset \ (Nx of \ ) whether to appear, \ (N \) takes the maximum value
for the multiplication factor can be enumerated violence

ac Code

#include<bits/stdc++.h>
using namespace std;
bitset<100010>bi,bii;
int n,m,K,a[100010],to[100010],cnt[100010],ans[100010];
struct node
{
    int opt,l,r,x,id;
    void init(int i){scanf("%d%d%d%d",&opt,&l,&r,&x),id=i;}
    bool operator<(const node&a)const{return to[l]==to[a.l]?r<a.r:l<a.l;}
}q[100010];
void add(int x){if(cnt[x]++==0)bi[x]=1,bii[100000-x]=1;}
void del(int x){if(--cnt[x]==0)bi[x]=0,bii[100000-x]=0;}
int main()
{
    scanf("%d%d",&n,&m),K=sqrt(n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]),to[i]=(i-1)/K+1;
    for(int i=1;i<=n;i++)q[i].init(i);
    sort(q+1,q+m+1);
    int l=1,r=0;
    for(int i=1;i<=m;i++)
    {
        while(q[i].l<l)add(a[--l]);
        while(q[i].r>r)add(a[++r]);
        while(q[i].l>l)del(a[l++]);
        while(q[i].r<r)del(a[r--]);
        if(q[i].opt==1)
        {
            if((bi&(bi<<q[i].x)).count())
                ans[q[i].id]=1;
        }
        else if(q[i].opt==2)
        {
            if((bi&(bii>>(100000-q[i].x))).count())
                ans[q[i].id]=1;
        }
        else if(q[i].opt==3)
        {
            int y=sqrt(q[i].x);
            for(int j=1;j<=y;j++)
                if(q[i].x%j==0&&bi[j]&&bi[q[i].x/j])
                    ans[q[i].id]=1;
        }
    }
    for(int i=1;i<=m;i++)
        if(ans[i])puts("hana");
        else puts("bi");
    return 0;
}

Guess you like

Origin www.cnblogs.com/muronglin/p/luogu-p3674.html