hdu 4902 线段树

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

这数据一眼看上去十分吓人,但是仔细读题之后发现就是 long long的线段树

 

操作一:修改区间内所有的值为 x

操作二:对给定区间的所有值询问,如果大于等于x则改为 gcd(a[i],x),否则不修改

 

这道题的关键就是在lazy上, 操作一是将所有的值改成x,那么我们的lazy可以存个x,放在祖先节点上,到时候pushdown的时候可以直接修改成祖先的值.

操作二:对于还没有pushdown的祖先,我们也可以对此祖先直接求gcd,因为反正改祖先的子节点都需要改的.

 

最后的pushup应该怎么写呢,为了与pushdown对应,那么我们就存下左右子节点相同的值,不相同存个-1.

 

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define  LL long long
#define  ULL unsigned long long
#define mod 100000000
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 1e6 + 5;
int n,m;
LL a[maxn << 2];
LL T[maxn << 2];//这里没有写lazy数组,但是lazy的思想在T[]的祖先节点上了
LL gcd(LL a,LL b){return b == 0 ? a : gcd(b,a%b);}
void pushUp(int rt)
{
    if(T[rt << 1] == T[rt << 1 | 1]) T[rt] = T[rt << 1];//子节点相同,存下来
    else T[rt] = -1;

}
void pushDown(int rt)
{
    if(~T[rt]){//不为-1 下推
      T[rt << 1] = T[rt];
      T[rt << 1 | 1] = T[rt];
      T[rt] = -1;
    }
    return ;
}
void buildTree(int rt,int l,int r)
{
    T[rt] = -1;
    if(l == r){
      T[rt] = a[l];
      return;
    }
    int mid = (l + r) >> 1;
    buildTree(rt << 1,l,mid);
    buildTree(rt << 1 | 1,mid + 1,r);
    pushUp(rt);
}
void upDatef(int rt,int l,int r,int L,int R,LL v)
{
    if(L <= l && R >= r){
      T[rt] = v;
      return;
    }
    int mid = (l + r) >> 1;
    pushDown(rt);
    if(L <= mid) upDatef(rt << 1,l,mid,L,R,v);
    if(R > mid) upDatef(rt << 1 | 1,mid + 1,r,L,R,v);
    pushUp(rt);
}
void upDates(int rt,int l,int r,int L,int R,LL v)
{
    if(L <= l && R >= r){
      if(~T[rt]){
        if(T[rt] >= v) T[rt] = gcd(T[rt],v);
        return;
      }
    }
    int mid = (l + r) >> 1;
    pushDown(rt);
    if(L <= mid) upDates(rt << 1,l,mid,L,R,v);
    if(R > mid) upDates(rt << 1 | 1,mid + 1,r,L,R,v);
    pushUp(rt);
}
void query(int rt,int l,int r)
{
    if(l == r){
      printf("%lld ",T[rt]);
      return;
    }
    int mid = (l + r) >> 1;
    pushDown(rt);
    query(rt << 1,l,mid);
    query(rt << 1 | 1,mid + 1,r);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
      scanf("%d",&n);
      for(int i = 1; i <= n; i++) scanf("%lld",&a[i]);
      buildTree(1,1,n);
      int m;
      scanf("%d",&m);
      while(m--){
        int x,y,opt;
        LL k;
        scanf("%d",&opt);
        if(opt == 1){
          int x,y;
          LL v;
          scanf("%d%d%lld",&x,&y,&v);
          upDatef(1,1,n,x,y,v);
        }
        else if(opt == 2){
          int x,y;
          LL v;
          scanf("%d%d%lld",&x,&y,&v);
          upDates(1,1,n,x,y,v);
        }
      }
      query(1,1,n);
      printf("\n");

    }





  return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章