fafu 1008

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int N=100000+5;
 4 int n, q;
 5 int a[N], tree[N<<2], t[N<<2];
 6 
 7 void build(int rt, int l, int r)
 8 {
 9     if(l==r)
10         tree[rt]=10;
11     else
12     {
13         int m=(l+r)>>1;
14         build(rt<<1, l, m);
15         build((rt<<1)+1, m+1, r);
16         
17         tree[rt]=tree[rt<<1]+tree[(rt<<1)+1];
18     }
19 }
20 
21 void pushDown(int rt, int m)
22 {
23     if(t[rt]!=-1)
24     {
25         t[rt<<1]=t[rt];
26         t[(rt<<1)+1]=t[rt];
27         tree[rt<<1]=t[rt]*(m-(m>>1));
28         tree[(rt<<1)+1]=t[rt]*(m>>1);
29         t[rt]=-1;
30     }
31 }
32 
33 void update(int rt, int l, int r, int x, int y, int v)
34 {
35     if(x<=l && r<=y)
36     {
37         t[rt]=v;
38         tree[rt]=(r-l+1)*v;
39         return;
40     }
41     pushDown(rt, r-l+1);
42     int m=(l+r)>>1;
43     if(x<=m)
44         update(rt<<1, l, m, x, y, v);
45     if(m<y)
46         update((rt<<1)+1, m+1, r, x, y, v);
47     tree[rt]=tree[rt<<1]+tree[(rt<<1)+1];
48 }
49 
50 int main()
51 {
52     int T;
53     scanf("%d", &T);
54     for(int k=1; k<=T; k++)
55     {
56         scanf("%d%d", &n, &q);
57         int x, y, v;
58         build(1, 1, n);
59         memset(t, -1, sizeof(t));
60         for(int i=0; i<q; i++)
61         {
62             scanf("%d%d%d", &x, &y, &v);
63             int mi=x<y?x:y;
64             int ma=x>y?x:y;
65             update(1, 1, n, mi, ma, v);
66         }
67         printf("Case %d: The total value of the stick is %d.\n", k, tree[1]);
68     }
69     return 0;
70 }

 

posted on 2015-03-20 11:27  咋是你呀~  阅读(211)  评论(0编辑  收藏  举报