HDOJ 1698 Just a Hook (线段树)

here is the problem,I learned another type of range tree and it’s a recursion version and a little slow than zkw’s tree , I made some change to fit my habit from the initial code

#include <bits/stdc++.h>
#include <cstring>
#define lc l,m,root<<1
#define rc m+1,r,root<<1|1
#define LEN r-l+1
using namespace std;
const int maxn = 100010;
int sum[maxn<<2];
int lazy[maxn<<2];
// extra space
int n;
// L R query range
// l r binary range
/**/
void up(int root)
{
   sum[root] = sum[root<<1]+sum[root<<1|1];
}
void down(int root,int m)
{
    if(lazy[root])
    {
        lazy[root<<1|1]=lazy[root<<1]=lazy[root];

        sum[root<<1]=lazy[root<<1]* (m-(m>>1));
        sum[root<<1|1]=lazy[root<<1|1]*(m>>1);
        // change '=' to '+=' when add or sub
        lazy[root] = 0;
    }
}
void build(int l = 1,int r = n,int root = 1)
{
    lazy[root] = 0;
    if(l == r)
    {
        //the initialization
        sum[root] = 1;
        return ;
    }
    int m = (l+r)>>1;
    build(l,m,root<<1);
    build(m+1,r,root<<1|1);
    up(root);
}
void update(int L,int R,int v,int l = 1,int r = n,int root = 1)
{
    if(L<=l && r <= R)
    {
        lazy[root]=v;
        sum[root]=v*(r-l+1);
        // change '=' to '+=' when add or sub
        return;
    }
    down(root,r-l+1);
    int m = (r+l)>>1;
    //tear down L,R to binary part
    if(L <= m ) update(L,R,v,lc);
    if(R >m ) update(L,R,v,rc);
    up(root);
}

int querySum(int L,int R,int l = 1,int r = n,int root = 1)
{
    if( L<=l && r <= R)
    {
        return sum[root];
    }
    down(root,LEN);
    int m = (l+r)>>1;
    int res = 0;
    if(L <= m) res += querySum(L,R,lc);
    if(R > m) res += querySum(L,R,rc);
    return res;
}
int main()
{
    int T = 0;
    cin>>T;
    for(int t = 1;t<=T;t++)
    {
        int q;
        cin>>n>>q;

        int l,r,v;
        build();
        for(int i = 0;i<q;i++)
        {
            scanf("%d%d%d",&l,&r,&v);
            update(l,r,v);
        }
        cout<<"Case "<<t<<": The total value of the hook is "<<sum[1]<<"."<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值