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;
    }
  • 相关阅读:
    ul>li中自定义属性后取值的问题
    img标签src图片地址找不到显示默认图片
    jquery通配符说明
    jquery控制元素的显示与隐藏
    mysql中行转列与列传行的问题
    页面禁止滚动条
    HttpServletRequest继承字ServletRequest的常用方法
    系统间通信方式
    mysql函数积累
    java 用maven 构建项目时@Override错误的解决办法
  • 原文地址:https://www.cnblogs.com/leotan0321/p/6081408.html
Copyright © 2011-2022 走看看