这是一道大水题

本文介绍了如何使用树状数组进行差分维护前缀和的操作,详细阐述了在2021-2022年全国大学生算法设计与编程挑战赛中的应用实例,通过代码展示了解答过程,涉及动态更新和查询前缀和的高效算法。

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

原题链接:SaikrVj | 【团队赛组】2021-2022年度第三届全国大学生算法设计与编程挑战赛(秋季赛)——热身赛

 

 

树状数组+差分维护前缀和

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int mod = 1e9+7 , INF = 0x3f3f3f3f , N = 2e5 + 10;

LL tr[N];
int n,m;
int lowbit(int x)
{
    return x & -x;
}

void add(int x,int c)
{
    for (int i = x ; i <= n ; i += lowbit(i))
        tr[i] += c;
}

LL query(int x)
{
    LL res = 0;
    for (int i = x ; i ; i -= lowbit(i))
        res += tr[i];
    return res;
}

int main()
{
    cin >> n >> m;
    LL sum = 0;
    while (m -- )
    {
        int type;
        cin >> type;
        if (type)
        {
            int x;
            cin >> x;
            cout << sum - query(x) << endl;
        }
        else
        {
            int l,r,c;
            cin >> l >> r >> c;
            sum += (r - l + 1) * c;
            add(l,(r - l + 1) * c);
            add(r + 1,-(r - l + 1) * c);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值