HDU 4902 Nice boat

ll gcd(ll a,ll b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
struct Date
{
    int t,l,r,x;
}date[maxn];

struct node {
    ll l,r,val;
 //   ll flag;
    bool temp;
    vector<ll>v;
    ll mid(){
    return (l+r)>>1;}
}tree[maxn*4];
int Time=0;

void build(ll l,ll r,ll rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].val=0;
    tree[rt].temp=false;
    if(l==r){
             tree[rt].v.clear();
            scanf("%I64d",&tree[rt].val);
            return ;
    }
    ll mid=tree[rt].mid();
    build(l,mid,LL(rt));
    build(mid+1,r,RR(rt));
    tree[rt].temp=tree[LL(rt)].temp&&tree[RR(rt)].temp;
}

ll query(ll l,ll r,ll rt){
    if(l==r)
    {
       // if(Time)printf(" ");
        printf("%I64d ",tree[rt].val);
       // Time++;
        return 0;
    }
    ll mid=tree[rt].mid();
    query(l,mid,LL(rt));
    query(mid+1,r,RR(rt));
}


void chage(ll l,ll r,ll rt,ll val)
{
    //cout<<rt<<" ";
    if(tree[rt].temp)return ;
    if(tree[rt].l==l&&tree[rt].r==r&&tree[rt].l==tree[rt].r)
    {
        tree[rt].val=val;
        tree[rt].temp=true;
        return;
    }
    ll mid=tree[rt].mid();
    if(r<=mid)chage(l,r,LL(rt),val);
    else if(l>mid)chage(l,r,RR(rt),val);
    else {
        chage(l,mid,LL(rt),val);
        chage(mid+1,r,RR(rt),val);
    }
    tree[rt].temp=tree[LL(rt)].temp&&tree[RR(rt)].temp;
}
void update(ll l,ll r,ll rt,ll val)
{
    if(tree[rt].temp)return ;
    if(tree[rt].l==l&&tree[rt].r==r&&tree[rt].l==tree[rt].r)
    {
        tree[rt].v.push_back(val);
        tree[rt].temp=false;
        return;
    }
    ll mid=tree[rt].mid();
     if(r<=mid)update(l,r,LL(rt),val);
    else if(l>mid)update(l,r,RR(rt),val);
    else {
        update(l,mid,LL(rt),val);
        update(mid+1,r,RR(rt),val);
    }
    tree[rt].temp=tree[LL(rt)].temp&&tree[RR(rt)].temp;
}
void doit(ll l, ll r ,ll rt)
{
   // cout<<2<<endl;
    if(l==r)
    {
        if(tree[rt].v.size()==0)return ;
        else
        {
            int i,j,k;
            for(i=tree[rt].v.size()-1;i>=0;i--)
            {
                if(tree[rt].val>tree[rt].v[i]){
                 //   cout<<1<<endl;
                   tree[rt].val=gcd(tree[rt].val,tree[rt].v[i]);
                }
            }
        }
        tree[rt].v.clear();
        return ;
    }
    ll mid(tree[rt].mid());
    doit(l,mid,LL(rt));
    doit(mid+1,r,RR(rt));
}
int main()
{
 //  freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        Time=0;
        int n;
        cin>>n;
        build(1,n,1);
        int p;
        scanf("%d",&p);
        int ip=0;
         ll t,l,r,x;
        for(int i=1;i<=p;i++){

            scanf("%I64d%I64d%I64d%I64d",&t,&l,&r,&x);
            date[ip].t=t;
            date[ip].l=l;
            date[ip].r=r;
            date[ip].x=x;
            ip++;
        }
        for(int i=ip-1;i>=0;i--)
        {
            t= date[i].t;
            l= date[i].l;
            r=date[i].r;
            x=date[i].x;
            if(t==1)
            {
                chage(l,r,1,x);
            }
            else
            {
                update(l,r,1,x);
            }
        }
        doit(1,n,1);
        query(1,n,1);
        cout<<endl;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章