Just a Hook ——线段树(区间更新)

Just a Hook

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


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
 

理解题意:

        有一根铜制的棍子,可以看作N段每段价值为1,总价值为N. 

         现在有三种操作:1)把 X 到 Y 段镀银,使 X 到 Y 段每段价值为2;

                                    2)把 X 到 Y 段镀金,使 X 到 Y 段每段价值为3;

                                    3)把 X 到 Y 段镀铜,使 X 到 Y 段每段价值为1;

        求:最后这根棍子的总价值。

注意点:

        1)多组输入,下推数组要清0;

        2)因为最后求的是棍子的总价值,所以不用写询问,直接输出ans【1】。

代码:

扫描二维码关注公众号,回复: 2294827 查看本文章
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MM=100005;
LL ans[MM<<2],lazy[MM<<2];      //ans【】模拟线段树维护区间,lazy【】下推标记
void PushUp(int rt)            //更新节点信息
{
    ans[rt]=ans[rt<<1]+ans[rt<<1|1];
}
void Build(int l,int r,int rt)         //建树
{
    if(l==r)
    {
        ans[rt]=1;
        return;
    }
    int mid=(l+r)>>1;
    Build(l,mid,rt<<1);
    Build(mid+1,r,rt<<1|1);
    PushUp(rt);
}
void PushDown(int l,int r,int rt)       //下推
{
    if(lazy[rt])
    {
        lazy[rt<<1]=lazy[rt];
        lazy[rt<<1|1]=lazy[rt];
        ans[rt<<1]=lazy[rt]*l;
        ans[rt<<1|1]=lazy[rt]*r;
        lazy[rt]=0;
    }
}
void Qxiu(int L,int R,int C,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        ans[rt]=C*(r-l+1);
        lazy[rt]=C;
        return;
    }
    int mid=(l+r)>>1;
    PushDown(mid-l+1,r-mid,rt);
    if(L<=mid)
        Qxiu(L,R,C,l,mid,rt<<1);
    if(R>mid)
        Qxiu(L,R,C,mid+1,r,rt<<1|1);
    PushUp(rt);
}
int main()
{
    int ca,cas=1;
    scanf("%d",&ca);
    while(ca--)
    {
        memset(lazy,0,sizeof(lazy));
        int n,m,x,y,c;
        scanf("%d%d",&n,&m);
        Build(1,n,1);
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d%d",&x,&y,&c);
            Qxiu(x,y,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %lld.\n",cas++,ans[1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/team39/article/details/81044892