zoukankan      html  css  js  c++  java
  • 【hdu】【线段树入门】Just a Hook

    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.

    Code

    线段树区间修改。和对[l,r]同加同减的区别只在于把+=换成=就可以。

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #define maxn 200000+1000
    #define lson L,mid,rt<<1
    #define rson mid+1,R,rt<<1|1
    #define root L,R,rt
    using namespace std;
    
    struct node{
        int val; 
    }T[3*maxn];
    
    int n,m,a,b;
    char c;
    
    void pushUp(int rt){
        T[rt].val=max(T[rt<<1].val,T[rt<<1|1].val);
        return;
    }
    
    void build(int L,int R,int rt){
        if(L==R){
            scanf("%d",&T[rt].val); return;
        }
        else{
            int mid=(L+R)/2;
            build(lson);
            build(rson);
        }
        pushUp(rt);
        return;
    }
    
    void update(int pos,int num,int L,int R,int rt){
        if(L==R){
            T[rt].val=num;
            return;
        } 
        else{
            int mid=(L+R)/2;
            if(pos<=mid) update(pos,num,lson);
            else update(pos,num,rson);
        }
        pushUp(rt);
        return;
    }
    
    int query(int l,int r,int L,int R,int rt){
        if(l<=L&&r>=R) return T[rt].val;//如果当前结点区间被所求区间完全覆盖 
        else {
            int mid=(L+R)/2;
            int temp=0; 
            if(l<=mid) temp=max(temp,query(l,r,lson));
            if(r>mid) temp=max(temp,query(l,r,rson));//这个地方超级坑!!!!!!!!!!!!!!!!!!!  
            return temp;
        }
    }
    
    
    int main(){
        while(~scanf("%d %d",&n,&m))
        {
            build(1,n,1);
            for(int i=0;i<m;i++)
            {
                scanf("%*c%c%d %d",&c,&a,&b);
                if(c=='Q')          
                    printf("%d
    ",query(a,b,1,n,1));
                else 
                    update(a,b,1,n,1);
            }
        }
        return 0;
    }
  • 相关阅读:
    ECS7天实践进阶训练营Day1:使用阿里云ECS,快速搭建、管理VuePress静态网站
    网络设备配置与管理(华为)基础系列 :VLAN故障排除和GVRP
    网络设备配置与管理(华为)基础系列 20190411
    CentOS7.0小随笔——指令基本操作(Part.B)
    网络设备配置与管理(华为)基础系列 20190404
    CentOS7.0小随笔——指令基本操作(Part.A)
    排球计分员用户故事
    罗辑思维-如何成为一个高手 (观后感)
    第18周个人工作量总结
    第18周项目冲刺流程总结
  • 原文地址:https://www.cnblogs.com/leotan0321/p/6081408.html
Copyright © 2011-2022 走看看