标签:mes ++ lld == logs tree bsp uil ase
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1698
题意:用1,2,3三种价值的颜色去给棒子区间涂色,问最后整个棒子的价值为多少,一开始整个都涂上价值为1的颜色
题解:区间更新 区间求和
1 //HDU 1689 Just a Hook 2 //区间更新 区间求和 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long LL; 9 const int MAX=100000+10; 10 LL n,m; 11 12 struct Tree 13 { 14 LL l,r; 15 LL sum,idx; 16 }; 17 Tree tree[4*MAX]; 18 19 void pushup(LL x) //向上更新 20 { 21 LL tmp=x<<1; 22 tree[x].sum=tree[tmp].sum+tree[tmp+1].sum; 23 } 24 25 void pushdown(LL x) //向下更新 26 { 27 LL tmp=x<<1; 28 tree[tmp].idx=tree[x].idx; 29 tree[tmp+1].idx=tree[x].idx; 30 tree[tmp].sum=tree[x].idx*(tree[tmp].r-tree[tmp].l+1); 31 tree[tmp+1].sum=tree[x].idx*(tree[tmp+1].r-tree[tmp+1].l+1); 32 tree[x].idx=0; 33 } 34 35 void build(LL l,LL r,LL x) 36 { 37 tree[x].l=l; 38 tree[x].r=r; 39 tree[x].idx=0; 40 if(l==r) 41 { 42 tree[x].sum=1; 43 return ; 44 } 45 LL tmp=x<<1; 46 LL mid=(l+r)>>1; 47 build(l,mid,tmp); 48 build(mid+1,r,tmp+1); 49 pushup(x); 50 } 51 52 void update(LL l,LL r,LL c,LL x) 53 { 54 if(r<tree[x].l||l>tree[x].r) return ; 55 if(l<=tree[x].l&&r>=tree[x].r) 56 { 57 tree[x].idx=c; 58 tree[x].sum=c*(tree[x].r-tree[x].l+1); 59 return ; 60 } 61 if(tree[x].idx) pushdown(x); 62 LL tmp=x<<1; 63 update(l,r,c,tmp); 64 update(l,r,c,tmp+1); 65 pushup(x); 66 } 67 68 int main(){ 69 LL t,A,B,C; 70 scanf("%lld",&t); 71 for(int k=1;k<=t;k++){ 72 scanf("%lld",&n); 73 scanf("%lld",&m); 74 build(1,n,1); 75 for(int i=1;i<=m;i++){ 76 scanf("%lld%lld%lld",&A,&B,&C); 77 update(A,B,C,1); 78 } 79 printf("Case %d: The total value of the hook is %lld.\n",k,tree[1].sum); 80 } 81 return 0; 82 }
HDU 1689 Just a Hook(线段是 区间更新+区间求和)
标签:mes ++ lld == logs tree bsp uil ase
原文地址:http://www.cnblogs.com/Leonard-/p/7624810.html