poj 1698 Just a Hook

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8647    Accepted Submission(s): 4195


Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.
 

 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
 

 

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
 

 

Sample Input
1 10 2 1 5 2 5 9 3
 

 

Sample Output
Case 1: The total value of the hook is 24.
 

 

Source
 

 

Recommend
wangye

这是一个区间染色的问题,对于区间染色问题,通常的方法在线段树中定义一个cover域,当cover的值为-1的时候,则表示这个线段的覆盖不是有一个颜色染成的,其中包含了多种颜色,而当cover为一个非-1的值时,例如cover==1时表示该线段是有第一种颜色染成的。 对于这题,由于是成段成段的在跟新线段树,我们自然不能简单的递归到根节点再进行处理,这样毫无疑问是要超时的,所以我们只能成段的跟新。

那么,如何做到成段的跟新呢?这里需要运用一种叫着lazy的思想,lazy顾名思义便是懒惰的意思。该思想大概的意思便是:假设要跟新区间[a,b],那么当我们找到区间[a,b]时,便不再往下进行递归,而是在该区间上标记上一个lazy,表示这里有信息需要向下传递。那么当我们需要跟新区间[a,b/2]时,我们便一定会从区间[a,b]经过,经过的过程中,我们发现区间[a,b]有信息需要向下传递,此时便将区间[a,b]的信息传递给其子区间。这样在必要的时候才一层一层的传递,便节约了很多时间。具体见程序吧,很清晰的

#include <stdio.h>
#define maxn 900000
#define max(xx,yy) xx>yy?xx:yy
#define min(xx,yy) xx<yy?xx:yy
struct node
{
  int l,r;
  int cover;
}tree[maxn];
int ans;
void built( int s,int e,int n)
{
  tree[n].l=s,tree[n].r=e,tree[n].cover=1;//初始颜色为1


  
    if(s==e) return ;
   
  int mid=(s+e)>>1;
  built(s,mid,n*2);
  built(mid+1,e,2*n+1);
 
  
 
}
void modify( int s,int e,int w,int n)//不懂?
{
 
  if(tree[n].l>e||tree[n].r<s) return ;
  if(tree[n].l>=s&&tree[n].r<=e)
  {
  tree[n].cover=w;
  return ;
  }
  if(tree[n].cover!=-1)//该区间有信息需要传递


  {
 tree[2*n].cover=tree[2*n+1].cover=tree[n].cover;
 tree[n].cover=-1;//同时自身取消标记


  }
  modify(s,e,w,2*n);
  modify(s,e,w,2*n+1);

}
void solve( int i)
{
  if(tree[i].cover!=-1)//单色区间,直接取其值


  {
 ans+=(tree[i].r-tree[i].l+1)*tree[i].cover;return ;
  }
  if(tree[i].l==tree[i].r) return;
 solve(i*2);solve(i*2+1);//非单色区间,则向下递归


 
}
int main ( )
{
  int N,M;
  int test,i;
  int x,y,v;
  int t=0;
  scanf("%d",&test);
  while(test--)
  {
 scanf("%d",&N);
 built(1,N,1);
 scanf("%d",&M);
 while(M--)
 {
    scanf("%d%d%d",&x,&y,&v);
    modify(x,y,v,1);
  
 }
  ans=0;
  solve(1);
  printf("Case %d: The total value of the hook is %d.\n",++t,ans);
  }
  return 0;
}

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

posted @ 2012-07-25 18:02  jiai  Views(238)  Comments(0Edit  收藏  举报