HDU 4902 Nice boat(数据结构-线段树)

Nice boat

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.

Let us continue our story, z*p(actually you) defeat the 'MengMengDa' party's leader, and the 'MengMengDa' party dissolved. z*p becomes the most famous guy among the princess's knight party. 

One day, the people in the party find that z*p has died. As what he has done in the past, people just say 'Oh, what a nice boat' and don't care about why he died.

Since then, many people died but no one knows why and everyone is fine about that. Meanwhile, the devil sends her knight to challenge you with Algorithm contest.

There is a hard data structure problem in the contest:

There are n numbers a_1,a_2,...,a_n on a line, everytime you can change every number in a segment [l,r] into a number x(type 1), or change every number a_i in a segment [l,r] which is bigger than x to gcd(a_i,x) (type 2).

You should output the final sequence.
 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains a integers n.
The next line contains n integers a_1,a_2,...,a_n separated by a single space.
The next line contains an integer Q, denoting the number of the operations.
The next Q line contains 4 integers t,l,r,x. t denotes the operation type.

T<=2,n,Q<=100000
a_i,x >=0
a_i,x is in the range of int32(C++)
 

Output
For each test case, output a line with n integers separated by a single space representing the final sequence.
Please output a single more space after end of the sequence
 

Sample Input
 
  
1 10 16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 10 1 3 6 74243042 2 4 8 16531729 1 3 4 1474833169 2 1 8 1131570933 2 7 9 1505795335 2 3 7 101929267 1 4 10 1624379149 2 2 8 2110010672 2 6 7 156091745 1 2 5 937186357
 

Sample Output
 
  
16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149
 

Author
WJMZBMR
 

Source
 

Recommend
We have carefully selected several similar problems for you:   4955  4954  4953  4952  4951 
 


题目大意:

给定n个数,m个操作,”1 L R X“ 表示把LR区间的数同时置为X,"2 L R X "表示把LR区间大于X的数比如Y置为gcd(X,Y)。


解题思路:

区间操作,一下子就想到了线段树,但是注意线段树的优化,只要维护记录最大值的maxc,以及bool记录这段是否相等这两个变量即可,详细还请参照我的代码。


解题代码:

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn=110000;
int n,m,d[maxn];

struct tree{
    int l,r,maxc;
    bool flag;
}a[maxn*4];

int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

void push_down(int k){
    if(a[k].flag){
        a[2*k+1].flag=a[2*k].flag=true;
        a[2*k+1].maxc=a[2*k].maxc=a[k].maxc;
    }
}

void push_up(int k){
    a[k].maxc=max(a[2*k].maxc,a[2*k+1].maxc);
    if(a[2*k].flag && a[2*k+1].flag && a[2*k].maxc==a[2*k+1].maxc) a[k].flag=true;
    else a[k].flag=false;
}

void build(int l,int r,int k){
    a[k].l=l;
    a[k].r=r;
    if(l==r){
        a[k].maxc=d[r];
        a[k].flag=true;
    }else{
        int mid=(l+r)/2;
        build(l,mid,2*k);
        build(mid+1,r,2*k+1);
        push_up(k);
    }
}

void insert1(int l,int r,int k,int x){//change data in l~r to x
    if(l<=a[k].l && a[k].r<=r){
        a[k].maxc=x;
        a[k].flag=true;
    }else{
        push_down(k);
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) insert1(l,r,2*k,x);
        else if(l>=mid+1) insert1(l,r,2*k+1,x);
        else{
            insert1(l,mid,2*k,x);
            insert1(mid+1,r,2*k+1,x);
        }
        push_up(k);
    }
}

void insert2(int l,int r,int k,int x){//change data >x to gcd(data,x)
    if(l<=a[k].l && a[k].r<=r){
        if(a[k].maxc<=x) return;
        if(a[k].flag){
            a[k].maxc=gcd(a[k].maxc,x);
        }else{
            push_down(k);
            int mid=(a[k].l+a[k].r)/2;
            insert2(l,mid,2*k,x);
            insert2(mid+1,r,2*k+1,x);
            push_up(k);
        }
    }else{
        push_down(k);
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) insert2(l,r,2*k,x);
        else if(l>=mid+1) insert2(l,r,2*k+1,x);
        else{
            insert2(l,mid,2*k,x);
            insert2(mid+1,r,2*k+1,x);
        }
        push_up(k);
    }
}

void query(int l,int k){
    if(a[k].l==a[k].r) printf("%d ",a[k].maxc);
    else{
        push_down(k);
        int mid=(a[k].l+a[k].r)/2;
        if(l<=mid) query(l,2*k);
        else query(l,2*k+1);
        push_up(k);
    }
}

void solve(){
    build(0,n-1,1);
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        int op,l,r,x;
        scanf("%d%d%d%d",&op,&l,&r,&x);
        if(op==1) insert1(l-1,r-1,1,x);
        else insert2(l-1,r-1,1,x);
    }
    for(int i=0;i<n;i++){
        query(i,1);
    }
    printf("\n");
}

int main(){
    int T;
    scanf("%d",&T);
    while(T-- >0){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%d",&d[i]);
        solve();
    }
    return 0;
}






版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/toyking/p/4950993.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值