(from Luogu) original topic
Lowbit (x) =2^k power, K is the number of x at the end of 0. We can try the lowbit.
(-X) = (~x) +1, take X to +1
void Update (int x,int k) represents A[x]+=k (single point of update)
an int sum (int x) indicates the 1-x interval and
The X-y interval and only sum (y)-sum (x-1) can be
#include <iostream> #include <string> #include <cstring> #define LOWBIT (x) ((x) & (-(x)))// It is also possible to write functions 233using namespace std;int c[524288+1],n,m,x,y,z;void Update (int x,int k)//a[x]+=k;
{for (int i=x;i<=n;i+=lowbit (i)) c[i]+=k;} int sum (int x)//int ans=0;for (int i=1;i<=x;i++) Ans+=a[i];return ans;
{int ans=0; for (int i=x;i>0;i-=lowbit (i)) ans+=c[i]; return ans;} int main ()
{cin >> n >> m; for (int i=1;i<=n;i++) {cin >> x; Update (I,X); } for (int i=1;i<=m;i++) {cin >> x >> y >> z; if (x==1) update (Y,Z); if (x==2) cout << sum (z)-sum (y-1) << Endl; } return 0;}
233
Tree array template single point update interval summation