树状数组

数列操作

https://ac.nowcoder.com/acm/problem/50427

#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;
const int N=1e6+5;
LL tr[N];
int n, q;

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

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

int main(){
    scanf("%d%d", &n, &q);
    for(int i=1; i<=n; ++i){ // 初始化
        LL v; scanf("%lld", &v);
        add(i, v);
    }
    int op; 
    LL x, y;
    while(q--){
        scanf("%d%lld%lld", &op, &x, &y);
        if(op==1) add(x, y);
        else if(op==2) printf("%lld\n", sum(y)-sum(x-1));
    }
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:25
点赞 评论 收藏
分享
仁者伍敌:牛子这些人还会点一个自动回复,boss都不带回复的
点赞 评论 收藏
分享
程序员小白条:你不是有一段实习了吗,现在找中大厂实习?过段时间要秋招了
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务