Just a Hook

来源:互联网 发布:编程语言薪资排行 编辑:程序博客网 时间:2024/05/29 16:56

Just a Hook

TimeLimit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K(Java/Others)
Total Submission(s): 7206 Accepted Submission(s):3451


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

Just <wbr>a <wbr>Hook


Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 toN. For each operation, Pudge can change the consecutive metallicsticks, numbered from X to Y, into cupreous sticks, silver sticksor golden sticks.
The total value of the hook is calculated as the sum of values of Nmetallic sticks. More precisely, the value for each kind of stickis 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 performingthe operations.
You may consider the original hook is made up of cupreoussticks.

Input
The input consists of several testcases. 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 ofthe sticks of Pudge’s meat hook and the second line contains aninteger Q, 0<=Q<=100,000, which isthe 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, whereZ=1 represents the cupreous kind, Z=2 represents the silver kindand Z=3 represents the golden kind.

Output
For each case, print a number in a linerepresenting the total value of the hook after the operations. Usethe format in the example.

Sample Input
1 10 2 1 52 5 9 3

Sample Output
Case 1: Thetotal value of the hook is 24.
 
 
 
代码:
 
#include<stdio.h>
#include<math.h>
struct node
{
    intl,r,color,cover;
};
node a[300001];
int z,ans;
void creattree(int i,int x,inty)  
{
    intmid;
   a[i].l=x;
   a[i].r=y;
   a[i].color=a[i].cover=1;
   if(x==y)return;
    else{
   mid=(x+y)/2;
   creattree(i*2,x,mid);
   creattree(i*2+1,mid+1,y);
    }
}
void update(int i,int x,inty)  
{
    intmid;
    if(a[i].l==x&& a[i].r==y)
    {
       a[i].color=z;
       a[i].cover=1;
       return ;
    }
   if(a[i].cover)//cover 的值为1时代表此区间的值color有效
      //此时需要更新 此区间的 左右此区间的 color值 一直到最小一个区间
       a[i*2].color=a[i*2+1].color=a[i].color;
       a[i*2].cover=a[i*2+1].cover=1;
       a[i].cover=0;
    }
   mid=(a[i].l+a[i].r)/2;
   if(y<=mid)update(i*2,x,y);
    elseif(x>mid)update(i*2+1,x,y);
    else
    {
       update(i*2,x,mid);
       update(i*2+1,mid+1,y);
    }
}
int count(int i)
{
   if(a[i].cover==1)return (a[i].r-a[i].l+1)*a[i].color;
    elsereturn   count(i*2)+count(i*2+1);
}
int main()
{
    intn,num,cases,q,x,y,ans;
   scanf("%d",&n);
   cases=1;
   while(n--)
    {
       scanf("%d %d",&num,&q);
       creattree(1,1,num);
       ans=0;
       while(q--)
       {
           scanf("%d %d%d",&x,&y,&z);
           update(1,x,y);
       }
       ans=count(1);
       printf("Case %d: The total value of the hook is%d.\n",cases++,ans);
    }
    return0;
}
原创粉丝点击