hdu 4902 Nice boat(线段树区间更新lazytag·单点更新)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4902

Nice boat

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1925    Accepted Submission(s): 867


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

分析:我觉得这里面既有区间更新,也有单点更新。前者利用lazytag保证了效率,后者保证了准确率。
one more再一个,再一次. "Please output a single more space after end of the sequence"每个序列末尾会有一个空格。我的英语~~~
#include <iostream>
#include <cstdio>
using namespace std;
#define lt 2*root
#define rt 2*root+1
const int maxn=1e5+10;
struct node{
    int l,r,val,tag;
    int mid(){  return (l+r)/2; }
}tr[maxn*3];
void build(int root,int ll,int rr){
    tr[root].l=ll;
    tr[root].r=rr;
    tr[root].val=0;
    tr[root].tag=-1;
    if(ll==rr) return ;
    int m=tr[root].mid();
    build(lt,ll,m);
    build(rt,m+1,rr);
}
void insert(int root,int dex,int val){
    if(tr[root].l==tr[root].r) {
        tr[root].val=val;
        return ;
    }
    int m=tr[root].mid();
    if(dex<=m) insert(lt,dex,val);   //<= !!
    else insert(rt,dex,val);
}
void update1(int root,int ll,int rr,int x){
    if(ll==tr[root].l&&rr==tr[root].r){
         tr[root].val=x;
         tr[root].tag=1;  //这一区间全是一样的数字
         return ;
    }
    if(tr[root].tag!=-1){
         tr[lt].tag=tr[root].tag;
         tr[lt].val=tr[root].val;
         tr[rt].tag=tr[root].tag;
         tr[rt].val=tr[root].val;
         tr[root].tag=-1;
         tr[root].val=0;
    }
    int m=tr[root].mid();
    if(ll>m) update1(rt,ll,rr,x);
    else if(rr<=m) update1(lt,ll,rr,x);
    else {  update1(lt,ll,m,x);   update1(rt,m+1,rr,x);  }
}
int gcd(int a,int b){
    if(a==0)  return b;
    return b?gcd(b,a%b):a;
}
void update2(int root,int ll,int rr,int x){
    if(ll==tr[root].l&&rr==tr[root].r&&tr[root].tag!=-1){
          if(tr[root].val>x)tr[root].val=gcd(tr[root].val,x);
          return ;
    }
    if(tr[root].l==tr[root].r){
          if(tr[root].val>x)tr[root].val=gcd(tr[root].val,x);
          return ;
    }
    if(tr[root].tag!=-1){
         tr[lt].tag=tr[root].tag;
         tr[lt].val=tr[root].val;
         tr[rt].tag=tr[root].tag;
         tr[rt].val=tr[root].val;
         tr[root].tag=-1;
    }
    int m=tr[root].mid();
    if(ll>m) update2(rt,ll,rr,x);
    else if(rr<=m) update2(lt,ll,rr,x);
    else {  update2(lt,ll,m,x);   update2(rt,m+1,rr,x);  }
}
int psum;
void print(int root,int dex){
    if(tr[root].l!=tr[root].r&&tr[root].tag!=-1){
        int length=tr[root].r-tr[root].l+1;
        for(int i=0;i<length;i++) printf("%d ",tr[root].val);
        psum=psum+length;
        return ;
    }
    if(tr[root].l==tr[root].r){
        printf("%d ",tr[root].val);
        psum++;
        return ;
    }
    int m=tr[root].mid();
    if(dex<=m) print(lt,dex);  //<= !! 左子树包含着mid
    else print(rt,dex);
}
int main()
{
    //freopen("cin.txt","r",stdin);
    int T,n,a,q;
    cin>>T;
    while(T--){
        scanf("%d",&n);
        build(1,1,n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a);
            insert(1,i,a);
        }
        scanf("%d",&q);
        int t,l,r,x,i;
        for(i=0;i<q;i++){
            scanf("%d%d%d%d",&t,&l,&r,&x);
            if(t==1)  update1(1,l,r,x);
            else update2(1,l,r,x);
        }
        for(psum=0;psum<n;){ //psum 自己在不断增加
            print(1,psum+1);
        }
        puts("");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值