标签:des style blog color java os strong io
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 353 Accepted Submission(s): 169
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
标签:des style blog color java os strong io
原文地址:http://www.cnblogs.com/qq1012662902/p/3883614.html