hdu 1698 Just a Hook

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树水题

View Code
 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5 #define L(x)(x<<1)
6 #define R(x)(x<<1|1)
7 #define MID(x,y) ((x+y)>>1)
8 const int MAX=100005;
9 struct Tnode{
10 int sum,left,right;
11 }node[MAX*4];
12 int num[MAX];
13 void init()
14 {
15 memset(node,0,sizeof(node));
16 }
17 void Build(int t,int l,int r)
18 {
19 node[t].left=l;
20 node[t].right=r;
21 if(l+1==r)
22 {
23 node[t].sum=1;
24 return ;
25 }
26 int mid=MID(l,r);
27 Build(L(t),l,mid);
28 Build(R(t),mid,r);
29 }
30 void update(int t,int l,int r,int sum)
31 {
32 if(node[t].left==l&&node[t].right==r)
33 {
34 node[t].sum=sum;
35 return ;
36 }
37 if(node[t].sum>0){
38 node[L(t)].sum=node[t].sum;
39 node[R(t)].sum=node[t].sum;
40 node[t].sum=-1;
41 }
42 int mid=MID(node[t].left,node[t].right);
43 if(l>=mid) update(R(t),l,r,sum);
44 else if(r<=mid) update(L(t),l,r,sum);
45 else {
46 update(L(t),l,mid,sum);
47 update(R(t),mid,r,sum);
48 }
49 }
50 int query(int t,int l,int r)
51 {
52
53 if( node[t].sum>0)
54 {
55 return (r-l)*(node[t].sum);
56 }
57 int mid = MID(node[t].left,node[t].right);
58 if( l >= mid )
59 return query(R(t),l,r);
60 else
61 if( r <= mid )
62 return query(L(t),l,r);
63 else
64 return query(L(t),l,mid)+query(R(t),mid,r);
65
66
67
68 }
69 int main()
70 {
71 int ind = 1,to,m,n,x,y,z,g=0;
72 char s[10];
73 scanf("%d",&to);
74 while(to--)
75 { g++;
76 scanf("%d%d",&n,&m);
77 init();
78 Build(1,0,n);
79
80 while(m--)
81 {
82 scanf("%d%d%d",&x,&y,&z);
83 update(1,x-1,y,z);
84 }
85
86 printf("Case %d: The total value of the hook is %d.\n",g,query(1,0,n));
87
88 }
89 return 0;
90 }

 

posted @ 2012-02-27 17:44  我们一直在努力  阅读(143)  评论(0编辑  收藏  举报