Just a Hook

#include<stdio.h>
#define MAX 1000001
struct node
{

   int
z;
   int
data;
}
sum[MAX*4];
void
js(int k,int j,int d)
{

   sum[d].z=1;
   sum[d].data=j-k+1;
   if
(j==k) return;
   int
m;
   m=(j+k)/2;
   js(k,m,d*2);
   js(m+1,j,d*2+1);
}

void
ch(int a,int b,int c,int k,int j,int d)
{

    if
(a<=k&&j<=b)
    {

       sum[d].z=c;
       sum[d].data=(j-k+1)*c;
       return
;
    }

    int
m;
    m=(k+j)/2;
    if
(sum[d].z>0)
    {

       sum[d*2].z=sum[d].z;sum[d*2+1].z=sum[d].z;sum[d].z=0;
       sum[d*2].data=(m-k+1)*sum[d*2].z;
       sum[d*2+1].data=(j-m)*sum[d*2+1].z;
    }

    if
(a<=m) ch(a,b,c,k,m,d*2);
    if
(b>m)  ch(a,b,c,m+1,j,d*2+1);
    sum[d].data=sum[d*2].data+sum[d*2+1].data;
}

int
main()
{

    int
t,n,m,cnt=1;
    int
a,b,c;
    scanf("%d",&t);
    while
(t--)
    {

       scanf("%d",&n);
       js(1,n,1);
       scanf("%d",&m);
       while
(m--)
       {

          scanf("%d %d %d",&a,&b,&c);
          ch(a,b,c,1,n,1);
       }

       printf("Case %d: The total value of the hook is %d.\n",cnt++,sum[1].data);
    }

    return
0;
}

posted @ 2012-07-10 09:14  zhuiy  阅读(158)  评论(0编辑  收藏  举报