首页 > 代码库 > HDU-4902 Nice boat

HDU-4902 Nice boat

Nice boat

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


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
 

 

Author
WJMZBMR
 线段树,区间更新,1,给你区间,在那区间全都变为值给定的值,2,给你一个区间,在那每个区间的值ai大于给定的值s那么就把这个值改为gcd(ai,s),否则不变。

最后输出变化后的一组数。

用线段树写的,主要是利用mark标记 ,全部变成x的话直接把这段变成x,然后加个tree[i].mark就行了,如果往下更新,要把标记和数一起往下传具体看pushdown函数,查询的时候如果延迟标记存在的话直接把这一段输出,因为标记存在这一段肯定是相同的数。。

第2步操作主要是,如果什么已经标记了则全都是相同的数,比较一下求公约数。

 如果没标记就要tree[i].left==tree[i].right。比较一下这个数,就行了。

#include<iostream>#include<cstring>#include<cstdio>const int maxx=100010;using namespace std;int a[2*maxx];struct tree{    int right,left;    int mark,num;}tree[4*maxx];int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}void build(int i,int left,int right){    tree[i].left=left;    tree[i].right=right;    tree[i].mark=0;    if(left==right)          {             tree[i].num=a[left];              return ;          }    int mid=(left+right)/2;    build(2*i,left,mid);    build(2*i+1,mid+1,right);}void pushdown(int r){   if(tree[r].mark)   {       tree[2*r].num=tree[2*r+1].num=tree[r].num;       tree[2*r].mark=tree[2*r+1].mark=tree[r].mark;       tree[r].mark=0;   }}void update(int r,int i,int j,int k){    int mid;    mid=(tree[r].left+tree[r].right)/2;    if(tree[r].left>=i&&tree[r].right<=j)              {                  tree[r].num=k;                  tree[r].mark=1;                    return;              }     pushdown(r);//向下传递。    if(i>mid)       update(2*r+1,i,j,k);      else if(j<=mid)           update(r*2,i,j,k);     else     {           update(2*r,i,mid,k);            update(2*r+1,mid+1,j,k);     } }void change(int r,int i,int j,int x){    int mid;    mid=(tree[r].left+tree[r].right)/2;     if(tree[r].mark)     {         if(i<=tree[r].left&&j>=tree[r].right)          {            if(tree[r].num<=x)                  return ;             else              {                 tree[r].num=gcd(tree[r].num,x);                      return ;              }          }      }      if(tree[r].left==tree[r].right)                {                     if(tree[r].num>x)                     {                      tree[r].num=gcd(tree[r].num,x);                      return ;                     }                     else                         return ;                }      pushdown(r);     if(i>mid)        change(2*r+1,i,j,x);      else if(j<=mid)            change(r*2,i,j,x);     else     {            change(2*r,i,mid,x);            change(2*r+1,mid+1,j,x);     } }void prit(int r){      int i;    if(tree[r].left==tree[r].right)      {         printf("%d ",tree[r].num);             return ;      }//     if(tree[r].mark)       {           for(i=tree[r].left;i<=tree[r].right;i++)                printf("%d ",tree[r].num);          return;       }    prit(2*r);    prit(2*r+1);}int main(){    int t,n,x,y,m,k,v,i;    scanf("%d",&t);    while(t--)    {      scanf("%d",&n);      for(i=1;i<=n;i++)          scanf("%d",&a[i]);        scanf("%d",&m);      build(1,1,n);      for(i=1;i<=m;i++)      {          scanf("%d",&k);             if(k==1)          {              scanf("%d%d%d",&x,&y,&v);              update(1,x,y,v);          }          else          {             scanf("%d%d%d",&x,&y,&v);              change(1,x,y,v);          }      }      prit(1);      printf("\n");    }    return 0;}