线段树(堆式)[区间更新]

区间更新是个坎。。。 堆式的区间更新是个大坎。。。

hdu1698 Just a Hook

解法1 还没理解自底向上的更新方法, 还是自顶向下的递归更新, 跑了859ms,悲剧。。。

代码很丑, 不过自创了一个debug函数,可以看树的结构, 不错,不错。

#include <cstdio>
#include <string.h>
#include <algorithm>

using namespace std;
typedef long long ll;

const int maxn=100000+123;
int T[maxn*3];
int M, h;

int bit(int x)/// get highest 1 in bit-number
{
    if(x==0)return 0;
    int n=31;
    if((x>>16)==0){n-=16; x<<=16;}
    if((x>>24)==0){n-=8; x<<=8;}
    if((x>>28)==0){n-=4; x<<=4;}
    if((x>>30)==0){n-=2; x<<=2;}
    return n-(x>>31);
}

inline void merge (const int &x)//合并2个儿子的信息
{
    if(T[x]==0 && T[x<<1]==T[x<<1|1])T[x]=T[x<<1];
}

void init(const int & x)
{
    h=bit(x);
    M=1<<h;
    memset (T, 0, sizeof(T));
    for (int i=M; i<x+M; ++i)
        T[i]=1;
    for (int i=M-1; i>0; --i)
        merge(i);
}

void Updata(int x, int v)
{
    T[x]=v;
    if(x<M)
    {
        T[x<<1]=v;
        T[x<<1|1]=v;
    }
}

void color (int l, int r, int x, int root)
{
    if(T[root]==x)return ;
    if(root>M+M)return;
    if(root>=M){if(root>l && root<r)T[root]=x; return;}
    int t=h-bit(root)+1;
    if((l>>t)<root && (r>>t)>root)
        T[root]=x;
    else
    {
        if(T[root]){T[root<<1]=T[root]; T[root<<1|1]=T[root]; T[root]=0;}
        if((l>>(t-1))<=(root<<1))color(l, r, x, root<<1);
        if((r>>(t-1))>=(root<<1|1))color(l, r, x, root<<1|1);
    }
}

inline void Change(int l, i
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值