日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树状数组模板1——单点修改区间查询

發布時間:2025/7/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树状数组模板1——单点修改区间查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

樹狀數組的模板,修改單點的值,查詢某個區間

?

1 #include<cstdio> 2 #include<cctype> 3 using namespace std; 4 5 int c[500010],n,m; 6 7 int read() 8 { 9 int x=0,f=1; 10 char c=getchar(); 11 while (!isdigit(c)) 12 f=c=='-'?-1:1,c=getchar(); 13 while (isdigit(c)) 14 x=(x<<1)+(x<<3)+(c^48),c=getchar(); 15 return x*f; 16 } 17 18 int lowbit(int x) 19 { 20 return x&-x; 21 } 22 23 int query(int x) 24 { 25 int ans=0; 26 for (;x;x-=lowbit(x)) 27 ans+=c[x]; 28 return ans; 29 } 30 31 int update(int x,int val) 32 { 33 for (;x<=n;x+=lowbit(x)) 34 c[x]+=val; 35 } 36 37 int main() 38 { 39 int i,j,t; 40 n=read(),m=read(); 41 for (i=1;i<=n;i++) 42 { 43 j=read(); 44 update(i,j); 45 } 46 while (m--) 47 { 48 t=read(); 49 i=read(); 50 j=read(); 51 if (t==1) 52 update(i,j); 53 else 54 printf("%d\n",query(j)-query(i-1)); 55 } 56 return 0; 57 }

?

轉載于:https://www.cnblogs.com/Ronald-MOK1426/p/8848825.html

總結

以上是生活随笔為你收集整理的树状数组模板1——单点修改区间查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。