标签:des style blog http java color
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16255 Accepted Submission(s): 8089
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 #define N 100005 7 8 struct node{ 9 int l, r; 10 int val; //val为-1表示杂色 11 }a[N*4]; 12 13 14 void build(int left,int right,int root){ 15 a[root].l=left; 16 a[root].r=right; 17 a[root].val=1; 18 if(left==right) return; 19 int mid=(left+right)/2; 20 build(left,mid,root*2); 21 build(mid+1,right,root*2+1); 22 } 23 24 void update(int left,int right,int val,int root){ 25 if(a[root].val==val) return; //若区间颜色和需要更新的颜色一样则不用更新 26 if(a[root].l==left&&a[root].r==right){ 27 a[root].val=val; 28 return; 29 } 30 if(a[root].val!=-1){ 31 a[root*2].val=a[root*2+1].val=a[root].val; 32 a[root].val=-1; 33 } 34 int mid=(a[root].l+a[root].r)/2; 35 if(left>mid) update(left,right,val,root*2+1); 36 else if(right<=mid) update(left,right,val,root*2); 37 else{ 38 update(left,mid,val,root*2); 39 update(mid+1,right,val,root*2+1); 40 } 41 } 42 43 int get_sum(int root){ 44 if(a[root].val!=-1) return (a[root].r-a[root].l+1)*a[root].val; 45 else return get_sum(root*2)+get_sum(root*2+1); 46 } 47 48 main() 49 { 50 int t, n, m; 51 int i, j; 52 cin>>t; 53 int x, y, z; 54 int kase=1; 55 while(t--){ 56 scanf("%d %d",&n,&m); 57 build(1,n,1); 58 while(m--){ 59 scanf("%d %d %d",&x,&y,&z); 60 update(x,y,z,1); 61 } 62 printf("Case %d: The total value of the hook is %d.\n",kase++,get_sum(1)); 63 } 64 }
HDU 1698 线段树(区间染色),布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://www.cnblogs.com/qq1012662902/p/3856558.html