$CF914D\ Bash\ and\ a\ Tough\ Math\ Puzzle$ 線段樹

正解:線段樹

解題報告:

傳送門$QwQ$c++

彷佛以前$cjk$學長考過,,,?而後由於爆零了很難受因此一直麻油落實$kk$ide

考慮線段樹維護區間的$gcd$,修改很$easy$不說了$QwQ$,而後查詢每次就查區間內有多少個數不是$x$的倍數,若是大於1就無解,不然有解,$over$ ui

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ri register int
#define rc register char
#define rb register bool
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)

const int N=5e5+10,inf=1e9;
int n,q,a[N],tr[N<<1];

il int read()
{
    rc ch=gc;ri x=0;rb y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
int gcd(ri gd,ri gs){return gs?gcd(gs,gd%gs):gd;}
void build(ri nw,ri l,ri r)
{
    if(l==r)return void(tr[nw]=a[l]);
    ri mid=(l+r)>>1;build(nw<<1,l,mid);build(nw<<1|1,mid+1,r);tr[nw]=gcd(tr[nw<<1],tr[nw<<1|1]);
}
void modify(ri nw,ri l,ri r,ri to,ri dat)
{
    if(l==r)return void(tr[nw]=dat);
    ri mid=(l+r)>>1;mid>=to?modify(nw<<1,l,mid,to,dat):modify(nw<<1|1,mid+1,r,to,dat);tr[nw]=gcd(tr[nw<<1],tr[nw<<1|1]);
}
int query(ri nw,ri l,ri r,ri to_l,ri to_r,ri dat)
{
    if(!(tr[nw]%dat))return 0;
    if(l==r)return 1;
    ri mid=(l+r)>>1,ret=0;
    if(mid>=to_l)ret=query(nw<<1,l,mid,to_l,to_r,dat);if(ret>1)return ret;
    if(mid<to_r)ret+=query(nw<<1|1,mid+1,r,to_l,to_r,dat);return ret;
}

int main()
{
    //freopen("914.in","r",stdin);freopen("914.out","w",stdout);
    n=read();rp(i,1,n)a[i]=read();build(1,1,n);
    q=read();
    while(q--)
    {
        ri opt=read();
        if(opt==1){ri l=read(),r=read(),x=read();query(1,1,n,l,r,x)>1?printf("NO\n"):printf("YES\n");continue;}
        ri x=read(),y=read();modify(1,1,n,x,y);
    }
    return 0;
}
碼量挺小的其實$QwQ$
相關文章
相關標籤/搜索