[模板]树状数组(区间修改单点查询)

本文详细介绍了BIT(Binary Indexed Tree,二进制索引树)算法的基本原理及其应用。通过具体的示例,展示了如何使用BIT进行区间更新和单点查询操作,并提供了完整的代码实现。适用于需要高效处理区间加法和查询问题的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Important

Bit is a good alogrithm.

Problem

Description

Give you some operate.

0 l r w   :fl..r+=w

1 x        :Ask fx

Sample:
Input:
10 10
0 2 8 5
1 7
1 2
0 3 9 8
1 7
1 3
1 5
1 5
0 1 5 6
1 2
Output:
5
5
13
13
13
13
11
Constraints
Datanm
1..3103103
4..10105105
Solution

0:

add(l,w);
add(r+1,-w);

1:

query(x);
Code
int bit[100010],n,t,l,r,h;
void add(int x,int y)
{
    while(x<=n)
    {
        bit[x]+=y;
        x+=x&(-x);
    }
}
int query(int x)
{
    int r=0;
    while(x)
    {
        r+=bit[x];
        x-=x&(-x);
    }
    rt r;
}
int main(){
    n=read();
    t=read();
    while(t--)
        if(!read())
        {
            l=read(),r=read(),h=read();
            add(l,h);
            add(r+1,-h);
        }
        else
            printf("%d\n",query(read()));
    rt 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值