线段树(堆式)[单点更新, 区间查询]


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

first blood of seg-tree  单点增减 区间求和

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=60000+123;
int T[maxn*2];
int M;

int bit(int x)/// get highest 1 in bit-number
{
    if(x==0)return 0;
    int n=1;
    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 32-n-(x>>31);
}

void Updata(int x, int v)
{
    for (T[x+=M]+=v, x>>=1; x; x>>=1) //
        T[x]=T[x<<1]+T[(x<<1)+1]; /// updata father by their children
}

int request(int s, int t)
{
    int res=0;
    for (s+=M-1, t+=M+1; s^t^1; s>>=1, t>>=1)/// change [,] to (,); s^t==1 refer to the interval is empty
    {
        if(~s&1) res+=T[s^1];
        if( t&1) res+=T[t^1];
    }
    return res;
}

int main ()
{
    int cas;
    scanf("%d", &cas);
    for (int I=1; I<=cas; ++I)
    {
        printf("Case %d:\n", I);
        int n;
        ///initialization
        scanf("%d", &n);
        M=1<<(bit(n));
//        printf("---%d  %d\n", M, bit(n));
        memset (T, 0, sizeof(T));
        for (int i=0; i<n; ++i)
            scanf("%d",  &T[i+M]);
        for (int i=(n+M-1)/2; i>0; --i)/// (n+m-1)/2 is largest non-leaf node
            T[i]=T[2*i]+T[2*i+1];

        char op[20];
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值