#include<iostream>
using namespace std;
#define LL(x)  ((x)<<1)
#define RR(x)  ((x)<<1|1)
#define FF(i,n)  for(int i = 0 ; i < n ; i ++)
int n;
int cas=1;
struct Seg_Tree
{
 int left,right,col;
}tt[300000];
 
void build(int left,int right,int idx)
{
 tt[idx].left = left;
 tt[idx].right = right;
 tt[idx].col = 1;
 if(left == right) return ;
 int mid = (left + right)/2;
 build(left,mid,LL(idx));
 build(mid+1,right,RR(idx));
}
 
void update(int left,int right,int col,int idx)
{
 if(left <= tt[idx].left && right >= tt[idx].right)
 {
  tt[idx].col = col;
  return ;
 }
 if(tt[idx].col != -1)
 {
  tt[LL(idx)].col = tt[RR(idx)].col = tt[idx].col;
  tt[idx].col = -1;
 }
 int mid = (tt[idx].left + tt[idx].right)/2;
 if(left <= mid)
  update(left,right,col,LL(idx));
 if(mid < right)
  update(left,right,col,RR(idx));
 
}
 
int query(int left,int right,int idx)
 {
 if(left == tt[idx].left && right == tt[idx].right)
 {
  if(tt[idx].col != -1)
  {
   return (right - left + 1) * tt[idx].col;
  } else
  {
   int mid = (tt[idx].left + tt[idx].right)/2;
   return query(left,mid,LL(idx)) + query(mid+1,right,RR(idx));
  }
 }
 int mid = (tt[idx].left + tt[idx].right)/2;
 if(right <= mid)
 {
  return query(left,right,LL(idx));
 }
 else if(left > mid)
 {
  return query(left,right,RR(idx));
 }
 else
 {
  return query(left,mid,LL(idx)) + query(mid+1,right,RR(idx));
 }
}
 
int main()
{
 int T;
 scanf("%d",&T);
 while(T--)
 {
  int n , m ;
  scanf("%d",&n);
  build(1,n,1);
  scanf("%d",&m);
  while(m --)
  {
   int left,right,col;
   scanf("%d%d%d",&left,&right,&col);
   update(left,right,col,1);
  }
  printf("Case %d: The total value of the hook is %d.\n",cas++,query(1,n,1));
 }
 return 0;
}

//还有一种比较简便的做法

#include <iostream>
using namespace std;
int b[100001][3];
int main()
{
 int t,n,q,i,j,sum,cas,temp;
 cin>>t;
 cas=0;
 while(t--)
 {
  cas++;
  sum=0;
  scanf("%d%d",&n,&q);
  for(i=1;i<=q;i++)
  {
   scanf("%d%d%d",&b[i][0],&b[i][1],&b[i][2]);
  }
  for(i=1;i<=n;i++)
  {
   temp=1;
   for(j=q;j>=1;j--)
   {
    if(i>=b[j][0]&&i<=b[j][1])
    {
     temp=b[j][2];
     break;//关键就是这个break;;处理过的就break;
    }
   }
   sum+=temp;
  }
  cout<<"Case "<<cas<<": The total value of the hook is "<<sum<<"."<<endl;
 }
 return 0;
}

posted on 2011-08-11 09:25  →木头←  阅读(257)  评论(0编辑  收藏  举报