• 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): 11425    Accepted Submission(s): 5650


    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

    题意:dota中的英雄 Pudge 有一hook 这hook 可由三种材料构成 分别如下
    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.
    现输入 x y z 表示x到y的hook 的值变为z 。z如上如述
    求最后,hook的值

    思路:线段树 普通的线段树会超时,得成段更新 用到了延迟覆盖

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=100010;
    
    #define L(rt) (rt<<1)
    #define R(rt) (rt<<1|1)
    
    struct Tree{
        int l,r;
        int val,sum;
        int cover;
    }tree[N<<2];
    
    void PushUp(int rt){
        tree[rt].sum=tree[L(rt)].sum+tree[R(rt)].sum;
    }
    
    void build(int L,int R,int rt){
        tree[rt].l=L;
        tree[rt].r=R;
        tree[rt].cover=1;
        tree[rt].val=1;
        if(tree[rt].l==tree[rt].r){
            tree[rt].sum=1;
            return ;
        }
        int mid=(L+R)>>1;
        build(L,mid,L(rt));
        build(mid+1,R,R(rt));
        PushUp(rt);
    }
    
    void PushDown(int rt){
        tree[L(rt)].val=tree[rt].val;
        tree[L(rt)].sum=(tree[L(rt)].r-tree[L(rt)].l+1)*tree[rt].val;
        tree[L(rt)].cover=1;
        tree[R(rt)].val=tree[rt].val;
        tree[R(rt)].sum=(tree[R(rt)].r-tree[R(rt)].l+1)*tree[rt].val;
        tree[R(rt)].cover=1;
        tree[rt].cover=0;
    }
    
    void update(int val,int L,int R,int rt){
        if(tree[rt].l==L && tree[rt].r==R){
            tree[rt].cover=1;
            tree[rt].val=val;
            tree[rt].sum=(tree[rt].r-tree[rt].l+1)*val;
            return ;
        }
        if(tree[rt].cover)
            PushDown(rt);
        if(tree[rt].val==val && tree[rt].cover) //当这一整段已为val,且覆盖整段,则继续向下已没有意义
            return ;
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(R<=mid)
            update(val,L,R,L(rt));
        else if(L>=mid+1)
            update(val,L,R,R(rt));
        else{
            update(val,L,mid,L(rt));
            update(val,mid+1,R,R(rt));
        }
        PushUp(rt);
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n,q;
        int cases=0;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&q);
            build(1,n,1);
            int a,b,z;
            while(q--){
                scanf("%d%d%d",&a,&b,&z);
                update(z,a,b,1);
            }
            printf("Case %d: The total value of the hook is %d.\n",++cases,tree[1].sum);
        }
        return 0;
    }

     

  • 相关阅读:
    Centos7安装gitlab私服
    SpringBoot2.x开发WebSocket
    java中 DecimalFormat格式的定义
    Docker部署Reids实战单机、集群
    CCF地铁修建
    C题:A Water Problem(dp||搜索)
    A题:Common Substrings(KMP应用)
    HDU 1879 继续畅通工程(Prim||Kruscal模板题)
    魔兽世界之一:备战(模拟)
    F题:等差区间(RMQ||线段树)
  • 原文地址:https://www.cnblogs.com/jackge/p/3041558.html
Copyright © 2020-2023  润新知