标签:des style http color java os strong io
原题http://acm.hdu.edu.cn/showproblem.php?pid=1698
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16935 Accepted Submission(s): 8427
1 10 2 1 5 2 5 9 3
Case 1: The total value of the hook is 24.
//本题是典型的线段树区间更新。为了节省时间用到了laz标记
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <math.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 100000 + 10;
int laz[maxn<<2];
int sum[maxn<<2];
void PushUp(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void PushDown(int rt,int m){//是从父节点向下更新
if(laz[rt]!=0){
laz[rt<<1] = laz[rt];
laz[rt<<1|1] = laz[rt];
sum[rt<<1] = (m-(m>>1))*laz[rt];
sum[rt<<1|1] = (m>>1)*laz[rt];
laz[rt] = 0;
}
}
void build(int l,int r,int rt){
laz[rt] = 0;//每个节点都要标记
if(l == r){
sum[rt] = 1;
return ;
}
int m = (l+r)>>1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l && r<=R){
laz[rt] = c;
sum[rt] = c*(r-l+1);
return ;
}
PushDown(rt,r-l+1);
int m = (l+r)/2;
if(L <= m){
update(L,R,c,lson);
}
if(R > m){
update(L,R,c,rson);
}
PushUp(rt);
}
int main(){
int t,n,m,x,y,z,cas;
while(~scanf("%d",&t)){
for(cas=1;cas<=t;cas++){
scanf("%d",&n);
build(1,n,1);
scanf("%d",&m);
while(m--){
scanf("%d%d%d",&x,&y,&z);
update(x,y,z,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",cas,sum[1]);
}
}
return 0;
}
标签:des style http color java os strong io
原文地址:http://blog.csdn.net/zcr_7/article/details/38338491