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'; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步