Wrong and rehabilitation training -p3674

Subject to the effect

Given sequence $ a_1, ..., a_n $ ( $ n \ leq10 ^ 5, a \ leq 10 ^ 5 $), there are \ (m \) ( \ (m \ Leq. 5 ^ 10 \) ) the following three months class Inquiry:

(1) gives \ (L, R & lt, K \) ( \ (K \ ^. 5 Leq 10 \) ), asking whether there is a \ (x, y \) makes \ (x \ in [l, r], y \ in [L, R & lt], a_x-a_y = K \)
(2) is given \ (L, R & lt, K \) ( \ (K \ ^. 5 Leq 10 \) ), asking whether there is a \ (x, y \) so \ (X \ in [L, R & lt], Y \ in [L, R & lt], a_x a_y = K + \)
(. 3) is given \ (L, R & lt, K \) ( \ (K \ Leq 10 ^ 5 \) ), asking whether there is a \ (x, y \) makes \ (x \ in [l, r], y \ in [l, r], a_x \ times a_y = k \)

answer

Found \ (a, k \) the range is small, and does not modify or forced online, consider Mo team.
Maintenance bitset represents each value exists in the current interval, denoted \ (c \) .
For (1) operation, is deformed into a formula \ (a_x a_y = K + \) , determines \ ((c << k) | c \) first \ (K \) bit is 1
for (2) Operation , the equation is modified \ (K-a_y a_x = \) , found \ (- a_y \) are not directly calculated, but can maintain a \ ((10-a_y ^. 5) \) , so that the equation becomes \ ( = a_x (10. 5-a_y ^) ^. 5 -10 K + \) , denoted \ ((10 ^ 5-a_y ) \) of \ (D \) , determines \ ((d >> (10 ^ 5-k )) | C \) first \ (K \) bit is 1
for (3) operation, it is difficult to deform, can be considered the \ (K \) is split into \ (\ sqrt k \) th divisor determines \ ( \ sqrt k \) if there exists a divisor in \ (a_l, ..., a_r \ )of. This sounds like Mo team time complexity \ (\ times \ sqrt k = n ^ 2 \) level. But I found so long to write code like this:

rep i 1 to m
    移动左右端点;
    判断根号k个约数;

It is judged divisor \ (\ sqrt k \) and Mo Force \ (\ sqrt n \) is the additive relationship.

Code
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];k!=-1;k=nxt[k])
#define maxn 100007
#define maxk 100000
#define LL long long
using namespace std;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
void write(int x)
{
    if(x==0){putchar('0'),putchar('\n');return;}
    int f=0;char ch[20];
    if(x<0)putchar('-'),x=-x;
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar('\n');
    return;
}
bitset<100001>now,pnow;
int a[maxn],n,m,num[maxn],ans[maxn],blo=300;
struct quest{int f,l,r,x,id;}q[maxn];
bool cmp(quest x,quest y){return (x.l/blo==y.l/blo)?(x.r<y.r):(x.l<y.l);}
void add(int id,int f)
{
    num[a[id]]+=f;
    if(f==1&&num[a[id]]==1){now[a[id]]=1,pnow[maxk-a[id]]=1;}
    if(f==-1&&!num[a[id]]){now[a[id]]=0,pnow[maxk-a[id]]=0;}
}
int main()
{
    n=read(),m=read();
    rep(i,1,n)a[i]=read();
    rep(i,1,m)q[i].f=read(),q[i].l=read(),q[i].r=read(),q[i].x=read(),q[i].id=i;
    int nowl=1,nowr=1;num[a[1]]=1,now[a[1]]=1,pnow[maxk-a[1]]=1;
    sort(q+1,q+m+1,cmp);
    rep(i,1,m)
    {
        while(nowl<q[i].l){add(nowl,-1),nowl++;}
        while(nowr>q[i].r){add(nowr,-1),nowr--;}
        while(nowl>q[i].l){nowl--,add(nowl,1);}
        while(nowr<q[i].r){nowr++,add(nowr,1);}
        if(q[i].f==1){if(((now<<q[i].x)&now).any())ans[q[i].id]=1;}
        else if(q[i].f==2){if(((pnow>>(maxk-q[i].x))&now).any())ans[q[i].id]=1;}
        else
        {
            int lim=sqrt(q[i].x);
            rep(j,1,lim)if(q[i].x%j==0&&now.test(j)&&now.test(q[i].x/j)){ans[q[i].id]=1;break;}
        }
    }
    rep(i,1,m)puts(ans[i]?"hana":"bi");
    return 0;
}
Some thoughts

Before contempt bitset bitset taught a man who was a
bitset common operations:

#include<bitset>//bitset的头文件
bitset<6342>a; //开一个下标从0~6341的bitset
a[63]=1,a[42]=0,a.set(63,1),a.set(42,0),a.reset(42);//赋值
a.set(),a.reset();//全变成1或全变成0
((a<<634)|(a>>23)).any();//bitset可以直接进行位运算,any()返回是否存在一位是1
a.none(),a.all(),a.test(5);//是否任意一位都为0;是否任意一位都为1;是否第5位为1
a.flip(32),a[32].flip();//翻转第32位
a.flip();//翻转全部
a.count(),a.size();//返回一共有几位是1;返回一共有几位
a.to_string(),a.to_ulong(),a.to_ullong();//把bitset变成string;unsigned long;unsigned long long

Guess you like

Origin www.cnblogs.com/xzyf/p/11532017.html