hdu 4092 线段树



链接:戳这里


Nice boat

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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 


题意:

 1 l r x 表示区间[l,r]的值变成x

2 l r x 表示区间[l,r]>=x的变成与x的最大公约数


思路:

线段树记录区间相同的数值

操作2:当前区间一样则判断是否>=x ,如果满足的话直接取gcd然后return ,否则return 

操作1:直接更新区间值就好了


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}
ll a[500100],same[500100];
void pushup(int root){
    if(same[root*2]==same[root*2+1]) same[root]=same[root*2];
    else same[root]=-1;
}
void pushdown(int root){
    if(~same[root]){
        same[root*2]=same[root*2+1]=same[root];
        same[root]=-1;
    }
}
void build(int root,int l,int r){
    same[root]=-1;
    if(l==r){
        same[root]=a[l];
        return ;
    }
    int mid=(l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    pushup(root);
}
void update1(int root,int l,int r,int x,int y,ll v){
    if(x<=l && y>=r){
        same[root]=v;
        return ;
    }
    int mid=(l+r)/2;
    pushdown(root);
    if(y<=mid) update1(root*2,l,mid,x,y,v);
    else if(x>mid) update1(root*2+1,mid+1,r,x,y,v);
    else {
        update1(root*2,l,mid,x,mid,v);
        update1(root*2+1,mid+1,r,mid+1,y,v);
    }
    pushup(root);
}
void update2(int root,int l,int r,int x,int y,ll v){
    if(x<=l && y>=r){
        if(~same[root]) {
            if(same[root]>=v) same[root]=gcd(same[root],v);
            return ;
        }
    }
    int mid=(l+r)/2;
    pushdown(root);
    if(y<=mid) update2(root*2,l,mid,x,y,v);
    else if(x>mid) update2(root*2+1,mid+1,r,x,y,v);
    else {
        update2(root*2,l,mid,x,mid,v);
        update2(root*2+1,mid+1,r,mid+1,y,v);
    }
    pushup(root);
}
void query(int root,int l,int r){
    if(l==r) {
        printf("%I64d ",same[root]);
        return ;
    }
    pushdown(root);
    int mid=(l+r)/2;
    query(root*2,l,mid);
    query(root*2+1,mid+1,r);
}
int n,m;
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        build(1,1,n);
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            int t,x,y;
            ll v;
            scanf("%d%d%d%I64d",&t,&x,&y,&v);
            if(t==1) update1(1,1,n,x,y,v);
            else update2(1,1,n,x,y,v);
        }
        query(1,1,n);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值