• 【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;
    }
  • 相关阅读:
    用PHP判断oicq是否在线的小程序
    我的PHP树的代码,可以嵌套任意层
    用PEAR来写你的下一个php程序(潘凡Night Sailer)(1)
    php在线文本编辑器
    分析HTML,并将结果存到一个数组中。看看里面的注释吧。:)
    聊天室php
    基于文件、数据库的计数器
    hust The mell hell
    UVA 10003 Cutting Sticks(区间DP)
    zoj 3197 Google Book(最小区间覆盖)
  • 原文地址:https://www.cnblogs.com/leotan0321/p/6081408.html
Copyright © 2020-2023  润新知