AcWing 1264. 动态求连续区间和

题目链接:https://www.acwing.com/problem/content/1266/

思路

视频正在路上

代码

#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define PI acos(-1)
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;

int N,Q;
ll tr[maxn];

int lowbit(int x){
    return x &-x;
}
void add(int idx,int v){
    for(int i = idx;i<=N;i += lowbit(i))
        tr[i] += v;
}
ll sum(int r){
    ll res = 0;
    for(int i = r;i>=1;i -= lowbit(i)){
        res += tr[i];
    }
    return res;
}

int main(){
    ios;
    cin>>N>>Q;
    for(int i = 1;i<=N;i++){
        int v;cin>>v;
        add(i,v);
    }
    while(Q--){
        int op,a,b;cin>>op>>a>>b;
        if(op == 0){
            cout<<sum(b) - sum(a-1)<<endl;
        }else{
            add(a,b);
        }
    }
    return 0;
}