HDU 1698 Just a Hook 【线段树】【区间更新】

Just a Hook

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


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

  
11021 5 25 9 3
 
Sample Output

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

Source
 

Recommend
wangye

这题属于线段树的成段更新,但是并不是加上某一个数,是直接全部赋值为某一个数。所以在写的时候要特别注意下。


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN=100005;
int CASE=1;
int num[MAXN];
struct Node
{
    int l,r;
    int len;
    int sum;
    int late;
} tree[MAXN<<2];
void pushup(int root)
{
    tree[root].sum = tree[root<<1].sum+tree[root<<1|1].sum;
}
void build(int root,int l,int r)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].sum=0;
    tree[root].len=r-l+1;
    if(tree[root].l==tree[root].r)
    {
        tree[root].sum=num[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);

    pushup(root);
}
void pushdown(int root)
{
    if(tree[root].late)
    {
        tree[root<<1].sum=0;///先清零
        tree[root<<1|1].sum=0;///先清零
        tree[root<<1].sum +=tree[root].late*tree[root<<1].len;
        tree[root<<1|1].sum+=tree[root].late*tree[root<<1|1].len;
        tree[root<<1].late=tree[root].late;
        tree[root<<1|1].late=tree[root].late;
        tree[root].late = 0;
    }
}
void update(int root,int l, int r,int late)
{
    //printf("---------%d\n",tree[1].sum);
    if(tree[root].l==l&&tree[root].r==r)
    {
        tree[root].sum=0;///先清零
        tree[root].sum+=(tree[root].len*late);

        tree[root].late=late;
        return ;
    }
    pushdown(root);
    int mid = (tree[root].l+tree[root].r)>>1;
    if(mid>=r)
    {
        update(root<<1,l,r,late);
    }
    else if(l>mid)
    {
        update(root<<1|1,l,r,late);
    }
    else
    {
        update(root<<1,l,mid,late);
        update(root<<1|1,mid+1,r,late);
    }
    pushup(root);
}
int query(int root,int l,int r)
{
   // printf("L==%d r==%d\n",l,r);

    if(tree[root].l==l&&tree[root].r==r)
    {
        return tree[root].sum;
    }
    pushdown(root);
    int mid  = (tree[root].l+tree[root].r)>>1;
    if(mid>=r)
    {
        return query(root<<1,l,r);
    }
    else if(l>mid)
    {
        return query(root<<1|1,l,r);
    }
    else
    {
        return (query(root<<1,l,mid)+query(root<<1|1,mid+1,r));
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        M(tree,0);
        int n;
        int m;
        scanf("%d %d",&n,&m);
        for(int i=1; i<=n; i++)
        {
           num[i]=1;
        }
        build(1,1,n);


        int x,y,z;

        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            if(z==1)
            {
                update(1,x,y,1);
            }
            else if(z==2)
            {
                update(1,x,y,2);
            }
            else
            {
                update(1,x,y,3);
            }
        }

        printf("Case %d: The total value of the hook is %d.\n",CASE++,query(1,1,n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37405320/article/details/80193647