HDU 4902 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.

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;
2.把[L,R]區間中比x大的數字變成和x的最大公約數

題目分析:
對於第一個操作,線段樹節點中保存這段區間各個數字的值是否是一樣的。對於第二個操作,需要保存區間的最大值。在更新的時候,第一張操作直接區間修改懶標記記錄。對於第二個操作,如果區間的最大值小於x,直接返回,否則繼續更新下去。

#include<iomanip>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdio>
#include<algorithm>
#include<cctype>
#define mid ((l+r)>>1)
#define lc  root<<1
#define rc  root<<1|1
#define N 100005
using namespace std;
int T,n,t,l,r,q,x,a[N];
//---------------------
struct node{
    int  lzx,val;
}tr[N<<2];
//---------------------
inline int Readint()
{
    int i=0,f=1;char ch;
    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
    if(ch=='-') f=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) i=(i<<1)+(i<<3)+ch-'0';
    return i*f;
}
//---------------------
inline void Push(int root)
{
    tr[root].val=max(tr[lc].val,tr[rc].val);
}
//---------------------
inline void build(int root,int l,int r)
{
    if(l==r){
        tr[root].val=a[l];
        tr[root].lzx=-1;
        return ;
    }   
    tr[root].lzx=-1;
    build(lc,l,mid);
    build(rc,mid+1,r);
    Push(root);
}
//--------------------
void lazy(int root)
{
    if(tr[root].lzx!=-1){
        tr[lc].lzx=tr[root].lzx,tr[rc].lzx=tr[root].lzx;
        tr[lc].val=tr[root].lzx,tr[rc].val=tr[root].lzx;
        tr[root].lzx=-1;
    }
}
//--------------------
inline int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
//--------------------
inline void Update1(int root,int l,int r,int L,int R,int x)
{
    if(l==L && r==R){
        tr[root].lzx=x;
        tr[root].val=x;
        return ;
    }
    lazy(root);
    if(R<=mid) Update1(lc,l,mid,L,R,x);
    else if(L>mid) Update1(rc,mid+1,r,L,R,x);
    else Update1(lc,l,mid,L,mid,x),Update1(rc,mid+1,r,mid+1,R,x);
    Push(root);
}
//---------------------
inline void Update2(int root,int l,int r,int L,int R,int x)
{
    if(l==L && r==R){
        if(tr[root].val<=x) return ;
        if(tr[root].lzx!=-1){
            tr[root].lzx=gcd(x,tr[root].lzx);
            tr[root].val=tr[root].lzx;
            if(l==r) a[l]=tr[root].lzx;
            return ;
        }
        if(l==r){
            if(a[l]>x) a[l]=gcd(x,a[l]);
            return ;
        }
        lazy(root);
        Update2(lc,l,mid,L,mid,x);
        Update2(rc,mid+1,r,mid+1,R,x);
        Push(root);
        return;
    }
    lazy(root);
    if(R<=mid) Update2(lc,l,mid,L,R,x);
    else if(L>mid) Update2(rc,mid+1,r,L,R,x);
    else Update2(lc,l,mid,L,mid,x),Update2(rc,mid+1,r,mid+1,R,x);
    Push(root);
    return ;
}
//--------------------
inline void out(int root,int l,int r)
{
    if(tr[root].lzx!=-1){
        for(int i=l;i<=r;i++)
          cout<<tr[root].lzx<<" ";
          return ;
    }
    if(l==r){
        cout<<a[l]<<" ";
        return ;
    }
    lazy(root);
    out(lc,l,mid);
    out(rc,mid+1,r);
}
//--------------------
int main()
{
    //freopen("lx.in","r",stdin);

    T=Readint();
    while(T--){
        n=Readint();
        for(int i=1;i<=n;i++) a[i]=Readint();
        build(1,1,n);
        q=Readint();
        while(q--){
            t=Readint(),l=Readint(),r=Readint(),x=Readint();
            if(t==1) Update1(1,1,n,l,r,x);
            else Update2(1,1,n,l,r,x);
        }
        out(1,1,n);puts("");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章