lowbit 树状数组
总结一下:x&(-x),当x为0时结果为0;x为奇数时,结果为1;x为偶数时,结果为x中2的最大次方的因子。.
#include<bits/stdc++.h> #include<iostream> using namespace std; typedef long long ll; const int NS=1e6+5; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; ll mod=19260817; ll shuzu[NS*4]; int a,b; int lowbit(int a) { return a&(-a); } void update(int x,int y) { while(x<=4*a) { shuzu[x]+=y; x+=lowbit(x); } } ll getsum(int x) { ll sum=0; while(x>0) { sum+=shuzu[x]; x-=lowbit(x); } return sum; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>a>>b; for(int i=1;i<=a;i++) { ll temp; cin>>temp; update(i,temp); } while(b--) { int x,y,z; cin>>x>>y>>z; if(x==1) { update(y,z); } else { cout<<getsum(z)-getsum(y-1)<<'\n'; } } }