跳到正文

个人记录

树状数组

Reset_OIer约 1 分钟
原文
文章信息与版本

文章信息

发布
2026年8月14日
收录
2026年8月20日
更新
2026年9月5日
篇幅
约 1 分钟

版本

  1. v1当前

#include<bits/stdc++.h> #define int long long using namespace std; const int N=5e5+5; int n,m; int a[N],c[N]; int lowbit (int x) { return x&(-x); } void add(int x,int d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int ask(int x) { int sum=0; while(x>0) { sum+=c[x]; x-=lowbit(x); } return sum; } signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>m; for (int i=1;i<=n;i++)cin>>a[i],add(i,a[i]); for (int i=1;i<=m;i++) { int op,l,r; cin>>op>>l>>r; if (op==1) { add(l,r); } else { cout<<ask(r)-ask(l-1)<<endl; } } return 0; }

原文