链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1698
分析与总结:
我的第一道线段树成段更新题... 看刘汝佳大白书+傻崽的代码学习的
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((left+right)>>1)
#define lson rt<<1,left,mid
#define rson rt<<1|1,mid+1,right
#define int64 long long
using namespace std;
const int MAXN = 100005;
int n,m;
int sum[MAXN<<2],col[MAXN<<2];
inline void push_up(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
inline void push_down(int rt,int m){
if(col[rt]){
col[rt<<1] = col[rt<<1|1] = col[rt];
sum[rt<<1] = (m-(m>>1)) * col[rt];
sum[rt<<1|1] = (m>>1) * col[rt];
col[rt] = 0;
}
}
void build(int rt,int left,int right){
col[rt] = 0;
sum[rt] = 1;
if(left==right)return;
build(lson);
build(rson);
push_up(rt);
}
void update(int rt,int left,int right,int l,int r,int data){
if(l<=left && right<=r) {
col[rt] = data;
sum[rt] = data*(right-left+1);
return;
}
push_down(rt, right-left+1);
int m = mid;
if(l <= m) update(lson,l,r,data);
if(r > m) update(rson,l,r,data);
push_up(rt);
}
int main(){
int T,x,y,z,cas=1;
scanf("%d", &T);
while(T--){
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i=0; i<m; ++i){
scanf("%d%d%d",&x,&y,&z);
update(1,1,n,x,y,z);
}
printf("Case %d: The total value of the hook is %d.\n",cas++,sum[1]);
}
return 0;
}
—— 生命的意义,在于赋予它意义士。
原创 http://blog.csdn.net/shuangde800 , By D_Double (转载请标明)