HDU 4902 Nice boat (线段树)

Nice boat

题目链接:

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

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. 将区间内的值都变为x. 2. 将区间内的大于x的值ai变为gcd(ai,x).
##题解: 很容易想到用线段树维护,关键是如何操作2的gcd. 对于树中的每个结点维护一个equal, 表示当前结点的子节点是否相等. (若相等就等于子节点的值,否则为-1). 当更新到某区间时,若区间内的值都相同,则只更新到这里即可,下面的结点利用pushdown来更新.
##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 101000 #define mod 100000007 #define inf 0x3f3f3f3f #define mid(a,b) ((a+b)>>1) #define IN freopen("in.txt","r",stdin); using namespace std;

LL gcd(LL a, LL b) {
return !b? a:gcd(b, a%b);
}

int n;
LL num[maxn];
struct Tree
{
int left,right;
LL lazy, equl;
}tree[maxn<<2];

void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].lazy=0;

if(left==right){
    tree[i].equl = num[left];
    return ;
}

int mid=mid(left,right);

build(i<<1,left,mid);
build(i<<1|1,mid+1,right);

tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void pushdown(int i)
{
if(tree[i].equl != -1) {
tree[i<<1].equl = tree[i].equl;
tree[i<<1|1].equl = tree[i].equl;
tree[i].lazy = 0;
tree[i<<1].lazy = 0;
tree[i<<1|1].lazy = 0;
}
if(tree[i].lazy) {
tree[i<<1].lazy = tree[i].lazy;
tree[i<<1|1].lazy = tree[i].lazy;
if(tree[i<<1].equl != -1) {
tree[i<<1].equl = tree[i].lazy;
tree[i<<1].lazy = 0;
}
if(tree[i<<1|1].equl != -1) {
tree[i<<1|1].equl = tree[i].lazy;
tree[i<<1|1].lazy = 0;
}
tree[i].lazy = 0;
}
}

void update(int i,int left,int right,LL d)
{
if(tree[i].leftleft&&tree[i].rightright)
{
if(tree[i].equl == -1) tree[i].lazy = d;
else tree[i].equl = d;
return ;
}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update(i<<1,left,right,d);
else if(left>mid) update(i<<1|1,left,right,d);
else {
    update(i<<1,left,mid,d);
    update(i<<1|1,mid+1,right,d);
}

tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

void update_gcd(int i,int left,int right, LL x)
{
if(tree[i].leftleft&&tree[i].rightright && tree[i].equl!=-1)
{
if(tree[i].equl > x) {
tree[i].equl = gcd(tree[i].equl, x);
tree[i].lazy = 0;
}
return ;
}

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) update_gcd(i<<1,left,right,x);
else if(left>mid) update_gcd(i<<1|1,left,right,x);
else {
    update_gcd(i<<1,left,mid,x);
    update_gcd(i<<1|1,mid+1,right,x);
}

tree[i].equl = tree[i<<1].equl==tree[i<<1|1].equl ? tree[i<<1].equl : -1;

}

LL query(int i,int left,int right)
{
if(tree[i].leftleft&&tree[i].rightright)
return tree[i].equl;

pushdown(i);

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);

}

int main(int argc, char const *argv[])
{
//IN;

int t; cin >> t;
while(t--)
{
    int m;
    scanf("%d", &n);
    for(int i=1; i<=n; i++)
        scanf("%lld", &num[i]);
    build(1, 1, n);

    scanf("%d", &m);
    while(m--) {
        int op, l, r; LL x;
        scanf("%d %d %d %lld", &op,&l,&r,&x);
        if(op == 1) {
            update(1, l, r, x);
        }
        else if(op == 2) {
            update_gcd(1, l, r, x);
        }
    }

    for(int i=1; i<=n; i++) {
        printf("%lld ", query(1, i, i));
    }
    printf("
");
}

return 0;

}

原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5766575.html