zoukankan      html  css  js  c++  java
  • 线段树专题—HDU1698 Just a Hook

    题意:t组数据,给一个n。m表示n长度的钩和m次操作。初始钩子的每单位长度的价值为1,接下来输入 x,y,k 的操作把钩子[x,y]区间的价值替换为k,求m次操作后钩子的价值为多少


    分析:成段替换。最后仅仅要求第一个区间就能够了,使用不用写query询问


    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include<vector>
    #pragma comment(linker,"/STACK:1024000000,1024000000")
    using namespace std;
    const int maxn = 1e5+5;
    int sum[maxn*4];
    int lazy[maxn*4];
    void up(int rt){
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void down(int l,int r,int rt){
        if(!lazy[rt]) return;
        int mid=(l+r)>>1;
        lazy[rt<<1]=lazy[rt];lazy[rt<<1|1]=lazy[rt];
        sum[rt<<1]=lazy[rt]*(mid-l+1);
        sum[rt<<1|1]=lazy[rt]*(r-mid);
        lazy[rt]=0;
    }
    void build(int l,int r,int rt){
        sum[rt]=lazy[rt]=0;
        if(l==r){
            sum[rt]=1;
            return;
        }
        int mid=(l+r)>>1;
        build(l,mid,rt<<1);
        build(mid+1,r,rt<<1|1);
        up(rt);
    }
    void update(int L,int R,int k,int l,int r,int rt){
        if(L<=l&&r<=R){
            sum[rt]=k*(r-l+1);
            lazy[rt]=k;
            return;
        }
        down(l,r,rt);
        int mid=(l+r)>>1;
        if(L<=mid) update(L,R,k,l,mid,rt<<1);
        if(R>mid) update(L,R,k,mid+1,r,rt<<1|1);
        up(rt);
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int cas=1;cas<=t;cas++){
            int n,m;
            scanf("%d%d",&n,&m);
            build(1,n,1);
            while(m--){
                int x,y,k;
                scanf("%d%d%d",&x,&y,&k);
                update(x,y,k,1,n,1);
            }
            printf("Case %d: The total value of the hook is %d.
    ",cas,sum[1]);
        }
    }
    


  • 相关阅读:
    前端如何进阶架构师
    NPOI使用记录
    ArcGis 中空间数据的插入与更新
    (转载).net 缓存处理
    ASP.NET(c#)实现重定向的三种方法的总结
    数据库关联表之间的更新语句
    C#net多线程多文件压缩下载
    关于写文件流的情况
    C# Class获取项目的绝对路径
    C# .net中DatailsView的JS简易版
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7219864.html
Copyright © 2011-2022 走看看