zoukankan      html  css  js  c++  java
  • hdoj1698【线段树Lazy操作】

    区间更新lazy操作一发。

    #include<cstdio>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    /*
    
    DOTA中屠夫的钩子有N个链子组成,每个链子可有金,银,铜,三种材质做成,
    
    铜链价值1,银价值2,金价值3,
    
    先对钩子进行Q次的更新操作,
    
    求出更新完后钩子的价值。
    
    */
    const int N=100007;
    struct st{
        int left,right;
        int sum;
        int val;
    };
    st q[N*4];
    int n,m;
    
    void build(int num,int L,int R)
    {
        q[num].left=L;
        q[num].right=R;
        if(L==R)
        {
            q[num].sum=1;
            q[num].val=0;
            return;
        }
        build(2*num,L,(L+R)/2);
        build(2*num+1,(L+R)/2+1,R);
        q[num].sum=q[2*num].sum+q[2*num+1].sum;
        q[num].val=0;
    }
    
    void update(int num,int s,int t,int c)
    {
        if(q[num].left>=s&&q[num].right<=t)
        {
            q[num].val=c;
            q[num].sum=c*(q[num].right-q[num].left+1);
            return;
        }
        if(q[num].val)
        {
            q[2*num].val=q[num].val;
            q[2*num].sum=q[num].val*(q[2*num].right-q[2*num].left+1);
            q[2*num+1].val=q[num].val;
            q[2*num+1].sum=q[num].val*(q[2*num+1].right-q[2*num+1].left+1);
            q[num].val=0;
        }
        int mid=(q[num].left+q[num].right)/2;
        if(mid>=t)
            update(2*num,s,t,c);
        else if(mid<s)
            update(2*num+1,s,t,c);
        else
        {
            update(2*num,s,mid,c);
            update(2*num+1,mid+1,t,c);
        }
        q[num].sum=q[2*num+1].sum+q[2*num].sum;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        int cas=1;
        while(t--)
        {
            scanf("%d",&n);
            build(1,1,n);
            scanf("%d",&m);
            while(m--)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                update(1,a,b,c);
            }
            printf("Case %d: The total value of the hook is %d.
    ",cas++,q[1].sum);
        }
        return 0;
    }
  • 相关阅读:
    [剑指Offer]判断一棵树为平衡二叉树(递归)
    [HDOJ]Coin Change(DP)
    01背包、完全背包
    [CodeForces_618C]Constellation
    [Codeforces_713A]Sonya and Queries
    C++位运算符
    [codeforces_597B] Restaurant(贪心)
    [LeetCode_98]Validate Binary Search Tree
    [LeetCode_96] Unique Binary Search Trees
    [LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934387.html
Copyright © 2011-2022 走看看