hdu 4902 Nice boat (线段树)

/*
题意:
给你n个数

q个询问:
q==1时  l到r区间的数更新为x
q==2时  l到r区间的数更新为gcd(a[i],x)(如果x大于a[i])

输出变化之后的n个数
*/

//模拟
# include <stdio.h>
# include <algorithm>
# include <string.h>
# include <stack>
using namespace std;
# define N 100005
int a[N],e[N];
int b[N],c[N],d[N];
int  gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
int main()
{
    int t,i,j,q,n;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            for(i=1; i<=n; i++)
                scanf("%d",&a[i]);
            scanf("%d",&q);
            for(j=1; j<=q; j++)
                scanf("%d%d%d%d",&b[j],&c[j],&d[j],&e[j]);
            for(i=1; i<=n; i++)
            {
                stack<int>s;
                for(j=q; j>=1; j--)
                {
                    if(c[j]<=i&&i<=d[j])
                    {
                        if(b[j]==1)
                        {
                            a[i]=e[j];
                            break;
                        }
                        else
                            s.push(e[j]);
                    }

                }
                while(!s.empty())
                {
                    if(s.top()<a[i])//a[i]大于才更新
                    a[i]=gcd(a[i],s.top());
                    s.pop();
                }
            }
            for(i=1; i<=n; i++)
                printf("%d ",a[i]);
            printf("\n");
        }
    }
    return 0;
}



/*
线段树做法,区间更新,lazy标记,到需要更新的时候才更新。
num[rt] != -1,表示 区间 L[rt]~R[rt] 所有的数都相同。

如果一个线段的值x不为-1,表示该线段全为x,否则表示该线段的值不唯一。
update1是裸的线段树,update2其实就是一直找到某线段的值唯一,当modify1处理。
*/
# include <algorithm>
# include <stdio.h>
# include <string.h>
using namespace std;
# define lson l,m,rt<<1
# define rson m+1,r,rt<<1|1
# define N 100005
int sum[N<<2];
int col[N<<2];
int gcd(int a,int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PushUP(int rt)
{
    if(sum[rt<<1]==sum[rt<<1|1])
        sum[rt]=sum[rt<<1];
    else
        sum[rt]=-1;
}
void PushDown(int rt)
{
    if(col[rt])
    {
        col[rt<<1]=col[rt<<1|1]=1;
        sum[rt<<1]=sum[rt<<1|1]=sum[rt];
        col[rt]=0;///即将修改下面的值,等待重新维护  
    }
}
void build(int l,int r,int rt)
{
    col[rt]=0;
    if(l==r)
    {
        scanf("%d",&sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}
void update1(int a,int b,int c,int l,int r,int rt)
{
    if(a<=l&&b>=r)
    {
        sum[rt]=c;
        col[rt]=1;
        return ;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(a<=m)
        update1(a,b,c,lson);
    if(b>m)
        update1(a,b,c,rson);
    PushUP(rt);
}
void update2(int a,int b,int c,int l,int r,int rt)
{
    if(a<=l&&b>=r&&sum[rt]!=-1)
    {
        if(sum[rt]>c)
        {
            sum[rt]=gcd(sum[rt],c);
            col[rt]=1;
        }
        return ;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(a<=m)
        update2(a,b,c,lson);
    if(b>m)
        update2(a,b,c,rson);
    PushUP(rt);
}
void query(int l,int r,int rt)
{
    if(l==r)
    {
        printf("%d ",sum[rt]);
        return ;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    query(lson);
    query(rson);
}
int main()
{
    int t,n,q,b,c,d,e;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d",&n);
            build(1,n,1);
            scanf("%d",&q);
            while(q--)
            {
                scanf("%d%d%d%d",&b,&c,&d,&e);
                if(b==1)
                    update1(c,d,e,1,n,1);
                else
                    update2(c,d,e,1,n,1);
            }
            query(1,n,1);
            printf("\n");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值