zoukankan      html  css  js  c++  java
  • 1820-Just a Hook(线段树区间延时更新)

    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.

    题意:一段长为n的链子,由n块金属连接而成,每块金属可能是金银铜三种材质。每种材质有一定价值。现在给出m次更新,每次更新对【L,R】区间内的金属更新成金->3 银->2 铜->1三种材质,最后输出整条链子的总价值。

    线段树区间更新、区间求和模板题。注意此处的更新时直接赋值而不是加加减减。
    记住如果Mark延时标记如果放结构体中一定要在一开始赋值l和r的建树部分初始化。否则后果很严重。如果单独放mark值也要记得memset清空。

    #include<stdio.h>
    #include<string.h>
    const int N = 100010;
    int n,m,k;
    struct node
    {
        int l,r,sum;
        int mark;
    } tree[N<<2];
    
    void build(int rt, int l, int r)
    {
        tree[rt].l=l;
        tree[rt].r=r;
        tree[rt].mark=0;///注意mark延时标记的初始化位置
        if(l==r)
        {
            tree[rt].sum=1;
            return ;
        }
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build(rt<<1|1,mid+1,r);
        tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
        return ;
    }
    
    void pushdown(int rt)
    {
        if(tree[rt].mark)
        {
            tree[rt<<1|1].sum=(tree[rt<<1|1].r-tree[rt<<1|1].l+1)*tree[rt].mark;///这里的push_down是直接赋值,而非增加
            tree[rt<<1].sum=(tree[rt<<1].r-tree[rt<<1].l+1)*tree[rt].mark;
            tree[rt<<1|1].mark=tree[rt].mark;
            tree[rt<<1].mark=tree[rt].mark;
            tree[rt].mark=0;
        }
        return ;
    }
    void update(int rt, int l, int r, int val)
    {
        if(tree[rt].l>=l&&tree[rt].r<=r)
        {
            tree[rt].sum=(tree[rt].r-tree[rt].l+1)*val;
            tree[rt].mark=val;
            return ;
        }
        pushdown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(l<=mid) update(rt<<1,l,r,val);
        if(r>mid) update(rt<<1|1,l,r,val);
        tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
        return ;
    }
    
    int query(int rt, int l, int r)
    {
        int ans=0;
        if(tree[rt].l>=l&&tree[rt].r<=r) return tree[rt].sum;
        pushdown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(l<=mid) ans+=query(rt<<1,l,r);
        if(r>mid) ans+=query(rt<<1|1,l,r);
        return ans;
    }
    
    int main()
    {
        int ca=0,t,l,r,val;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            build(1,1,n);
            while(m--)
            {
                scanf("%d%d%d",&l,&r,&val);
                update(1,l,r,val);
            }
            printf("Case %d: The total value of the hook is %d.
    ",++ca,query(1,1,n));
        }
        return 0;
    }
  • 相关阅读:
    LintCode 27. 拓扑排序 DFS实现
    LintCode 155. 二叉树的最小深度
    LintCode 90. k数和 II
    LintCode 33. N皇后问题
    Oracle分组后取某列最大值的行数据
    Oracle日期范围
    Mongo可视化工具基本操作
    修改winform安装包写日志文件权限
    Winform安装包出现无法访问网络位置
    ComboBox的真实值和显示值
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135772.html
Copyright © 2011-2022 走看看