码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4902 线段树(区间更新)

时间:2014-07-31 23:35:40      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   java   os   strong   io   

Nice boat

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


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
 
 
题目意思:
给出长度为n的数组,m个操作,操作有两种:1、1, l, r, x即把[l,r]段的元素全变为x。2、2,l,r,x即把[l,r]段的大于x的元素全变成该元素与x的最大公约数。
 
思路:
区间更新老套路了,用maxh记录每段的最大值,val记录该段颜色,当进行1操作时,把tree[l,r]的val变成x;当进行2操作时看该段的maxh是否大于x,若小于则不操作,反之在看val,若val和x相等,val=gcd(val,x)即可,反之向下找子段,一直找到子段是一种颜色进行上述操作即可。
 
 
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 using namespace std;
  6 
  7 #define N 100005
  8 #define ll root<<1
  9 #define rr root<<1|1
 10 #define mid (a[root].l+a[root].r)/2
 11 
 12 int shu[N];
 13 
 14 struct node{
 15     int l, r;
 16     int maxh;
 17     int val;
 18 }a[N*4];
 19 
 20 int gcd(int a,int b){
 21     if(b==0) return a;
 22     else return gcd(b,a%b);
 23 }
 24 
 25 /*int gcd(int a,int b){
 26     if(b>a)swap(a,b);
 27     if(b==0)return 0;
 28     int k=a%b;
 29     while(k!=0){
 30         a=b;b=k;k=a%b;
 31     }
 32     return b;
 33 }
 34 */
 35 void build(int l,int r,int root){
 36     a[root].l=l;
 37     a[root].r=r;
 38     a[root].val=-1;           //初始化颜色为-1 
 39     if(l==r){
 40     a[root].maxh=a[root].val=shu[l];return;
 41     }
 42     build(l,mid,ll);
 43     build(mid+1,r,rr);
 44     a[root].maxh=max(a[ll].maxh,a[rr].maxh); //更新maxh 
 45 }
 46 
 47 void change(int val,int root){             //进行2操作的函数 
 48     if(a[root].maxh<=val) return;
 49     if(a[root].val!=-1){                           //当该段的颜色是一种而不是多种 
 50         a[root].maxh=a[root].val=gcd(a[root].val,val);
 51         return;
 52     }
 53     change(val,ll);
 54     change(val,rr);
 55     a[root].maxh=max(a[ll].maxh,a[rr].maxh);
 56 }
 57 
 58 void update(int z,int l,int r,int val,int root){ 
 59     if(a[root].l==l&&a[root].r==r){
 60         if(z==1){                                //若进行1操作,把该段val=x即可 
 61             a[root].maxh=a[root].val=val;
 62         }
 63         else{
 64             change(val,root);                //进行2操作 
 65         }
 66         return;
 67     }
 68     if(a[root].val!=-1){                   //向下传递 
 69         a[ll].maxh=a[rr].maxh=a[ll].val=a[rr].val=a[root].val;
 70         a[root].val=-1;
 71     } 
 72     if(r<=mid) update(z,l,r,val,ll);
 73     else if(l>mid) update(z,l,r,val,rr);
 74     else{
 75         update(z,l,mid,val,ll);
 76         update(z,mid+1,r,val,rr);
 77     }
 78     a[root].maxh=max(a[ll].maxh,a[rr].maxh);
 79 }
 80 
 81 void out(int root){                   //输出 
 82 
 83     if(a[root].l==a[root].r){
 84         printf("%d ",a[root].val);
 85         return;
 86     }    
 87     if(a[root].val!=-1){           //向下传递 
 88         a[ll].val=a[rr].val=a[root].val;
 89         a[root].val=-1;
 90     } 
 91     
 92     out(ll);
 93     out(rr);
 94 }
 95 
 96 main()
 97 {
 98     int t;
 99     int i, j, k;
100     int z, l, r, x, m, n;
101     cin>>t;
102     while(t--){
103         scanf("%d",&n);
104         for(i=1;i<=n;i++) scanf("%d",&shu[i]);
105         scanf("%d",&m);
106         build(1,n,1);
107         while(m--){
108             scanf("%d %d %d %d",&z,&l,&r,&x);
109             update(z,l,r,x,1);
110         }
111         out(1);
112         //printf("%d",shu[1]);
113         //for(i=2;i<=n;i++) printf(" %d",shu[i]);
114         cout<<endl;
115     }
116 }

 

 

HDU 4902 线段树(区间更新),布布扣,bubuko.com

HDU 4902 线段树(区间更新)

标签:des   style   blog   color   java   os   strong   io   

原文地址:http://www.cnblogs.com/qq1012662902/p/3883614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!