树状数组
数列操作
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; }