Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1698

刚开始学线段树,代码差不多是抄的

View Code
 1 //1698
2 #include <stdio.h>
3 const int N=100010;
4 int st[4*N],co[4*N];
5 void newup(int rt)
6 {
7 st[rt]=st[rt*2]+st[rt*2+1];
8 }
9 void newdown(int rt,int cnt)
10 {
11 if (!co[rt]) return;
12 co[rt*2]=co[rt*2+1]=co[rt];
13 st[rt*2]=(cnt-cnt/2)*co[rt];
14 st[rt*2+1]=cnt/2*co[rt];
15 co[rt]=0;
16 }
17 void build(int l,int r,int rt)
18 {
19 co[rt]=0;
20 if (l==r)
21 {
22 st[rt]=1;
23 return;
24 }
25 int m=(l+r)/2;
26 build(l,m,rt*2);
27 build(m+1,r,rt*2+1);
28 newup(rt);
29 }
30 void update(int x,int y,int a,int l,int r,int rt)
31 {
32 if (x<=l && r<=y)
33 {
34 co[rt]=a;
35 st[rt]=a*(r-l+1);
36 return;
37 }
38 newdown(rt,r-l+1);
39 int m=(l+r)/2;
40 if (x<=m) update(x,y,a,l,m,rt*2);
41 if (y>m) update(x,y,a,m+1,r,rt*2+1);
42 newup(rt);
43 }
44 int main()
45 {
46 int T,C=0;
47 scanf("%d",&T);
48 while (T--)
49 {
50 int n,m;
51 scanf("%d%d",&n,&m);
52 build(1,n,1);
53 while (m--)
54 {
55 int x,y,z;
56 scanf("%d%d%d",&x,&y,&z);
57 update(x,y,z,1,n,1);
58 }
59 printf("Case %d: The total value of the hook is %d.\n",++C,st[1]);
60 }
61 return 0;
62 }

 

posted on 2012-02-14 21:57  Qiuqiqiu  阅读(171)  评论(0编辑  收藏  举报