zoukankan      html  css  js  c++  java
  • HDU 1698 Just a Hook

    http://acm.hdu.edu.cn/showproblem.php?pid=1698

    做的第一道线段树成段更新的题目,lazy标志起到的作用的延迟更新(为了保证O(logN)的效率,不延迟就变成O(N))。这道题反正就询问一次,最后直接输出即可。

    View Code
    #include <iostream>
    using namespace std ; 
    #define lson l,m,rt<<1 
    #define rson m+1,r,rt<<1|1
    const int maxn=100002 ;
    int sum[maxn<<2] ;
    int lazy[maxn<<2] ;
    void pushup(int rt)
    {
        sum[rt]=sum[rt<<1]+sum[rt<<1|1] ;
    }
    void pushdown(int rt,int m)
    {
        if(lazy[rt])
        {
            lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt] ;
            sum[rt<<1]=(m-(m>>1))*lazy[rt] ;
            sum[rt<<1|1]=(m>>1)*lazy[rt] ;
            lazy[rt]=0 ;
        }
    }
    void build(int l,int r,int rt)
    {
        lazy[rt]=0 ;
        sum[rt]=1 ;
        if(l==r)
            return ;
        int m=(l+r)>>1 ;
        build(lson) ;
        build(rson) ;
        pushup(rt) ;
    }
    void update(int L,int R,int p,int l,int r,int rt)
    {
        if(L<=l && R>=r)
        {
            lazy[rt]=p ;
            sum[rt]=p*(r-l+1) ;
            return ;
        }
        pushdown(rt,r-l+1) ;
        int m=(l+r)>>1 ;
        if(L<=m)
            update(L,R,p,lson) ;
        if(R>m)
            update(L,R,p,rson) ;
        pushup(rt) ; 
    }
    int main()
    {
        int t ;
        scanf("%d",&t) ;
        for(int cas=1;cas<=t;cas++)
        {
            int n ;
            scanf("%d",&n) ;
            build(1,n,1) ;
            int q ;
            scanf("%d",&q) ;
            while(q--)
            {
                int a,b,w ;
                scanf("%d%d%d",&a,&b,&w) ;
                update(a,b,w,1,n,1) ;
            }
            printf("Case %d: The total value of the hook is %d.\n",cas,sum[1]) ;
        }
        return 0 ;
    } 
  • 相关阅读:
    DOM操作之获取HTML、文本和值
    DOM操作之属性和样式操作
    DOM节点的增删改查
    其他选择器
    属性过滤选择器
    Linux
    Linux
    Appium自动化(3)
    Appium自动化(2)
    Appium自动化(1)
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/2546842.html
Copyright © 2011-2022 走看看