hdu 4902 Nice boat(树形结构-线段树)

来源:互联网 发布:日本历史地震数据统计 编辑:程序博客网 时间:2024/06/07 18:41

Nice boat

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


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
11016807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709 101 3 6 742430422 4 8 165317291 3 4 14748331692 1 8 11315709332 7 9 15057953352 3 7 1019292671 4 10 16243791492 2 8 21100106722 6 7 1560917451 2 5 937186357
 

Sample Output
16807 937186357 937186357 937186357 937186357 1 1 1624379149 1624379149 1624379149
 
题意:给n个数,Q组操作,有两种操作1和2,1操作把一个区间的数都变成另一个数,2操作把一个区间内大于t的数变成该数和t的最大公约数,最后把所有数的最终结果输出,注意最后一个数后面要多输出一个空格!

解题思路:线段树,记录两个值,一个是区间的最大值Max,还有一个区间是否为同一个数。

#include <iostream>#include <cstdio>using namespace std;const int maxn = 100010;struct tree{    int l , r , Max , lz;}a[4*maxn];int n , Q , num[maxn];int gcd(int a , int b){    if(b == 0) return a;    return gcd(b , a%b);}void pushdown(int k){    if(a[k].l != a[k].r && a[k].lz != -1){        a[2*k].lz = a[k].lz;        a[2*k+1].lz = a[k].lz;        a[2*k].Max = a[k].lz;        a[2*k+1].Max = a[k].lz;        a[k].lz = -1;    }}void build(int l , int r , int k){    a[k].l = l;    a[k].r = r;    a[k].lz = -1;    a[k].Max = -1;    if(l == r){        a[k].lz = num[l];        a[k].Max = num[l];    }else{        int mid = (l+r)/2;        build(l , mid , 2*k);        build(mid+1 , r, 2*k+1);        a[k].Max = max(a[2*k].Max , a[2*k+1].Max);        if(a[2*k].lz == a[2*k+1].lz) a[k].lz = a[2*k].lz;        else a[k].lz = -1;    }}void add(int l , int r , int k , int type , int t){    if(a[k].l == a[k].r){        if(type == 1){            a[k].lz = t;            a[k].Max = t;        }else{            if(a[k].Max > t){                a[k].lz = gcd(a[k].lz , t);                a[k].Max = a[k].lz;            }        }        return;    }    if(l <= a[k].l && a[k].r <= r){        if(type == 1){            a[k].lz = t;            a[k].Max = t;        }else{            if(a[k].Max > t){                if(a[k].lz == -1){                    int mid = (a[k].l+a[k].r)/2;                    add(l , mid , 2*k , type , t);                    add(mid+1 , r , 2*k+1 , type , t);                    a[k].Max = max(a[2*k].Max , a[2*k+1].Max);                    if(a[2*k].lz == a[2*k+1].lz) a[k].lz = a[2*k].lz;                    else a[k].lz = -1;                }else{                    a[k].lz = gcd(a[k].lz , t);                    a[k].Max = a[k].lz;                }            }        }    }else{        if(type == 2 && a[k].Max <= t) return;        pushdown(k);        int mid = (a[k].l+a[k].r)/2;        if(mid >= r) add(l , r , 2*k , type , t);        else if(mid < l) add(l , r , 2*k+1 , type , t);        else{            add(l , mid , 2*k , type , t);            add(mid+1 , r , 2*k+1 , type , t);        }        a[k].Max = max(a[2*k].Max , a[2*k+1].Max);        if(a[2*k].lz == a[2*k+1].lz) a[k].lz = a[2*k].lz;        else a[k].lz = -1;    }}int query(int l , int r , int k){    if(l <= a[k].l && a[k].r <= r){        return a[k].lz;    }else{        pushdown(k);        int mid = (a[k].l+a[k].r)/2;        if(mid >= r) return query(l , r , 2*k);        else return query(l , r , 2*k+1);    }}void readcase(){    scanf("%d" , &n);    for(int i = 1; i <= n; i++) scanf("%d" , &num[i]);}void computing(){    build(1 , n , 1);    scanf("%d" , &Q);    int l , r, type , t;    while(Q--){        scanf("%d%d%d%d" , &type , &l , &r , &t);        add(l , r , 1 ,type , t);    }    for(int i = 1; i <= n; i++){        printf("%d " , query(i , i , 1));    }    printf("\n");}int main(){    int T;    scanf("%d" , &T);    while(T--){        readcase();        computing();    }    return 0;}


0 0
原创粉丝点击