1. 程式人生 > >[模板]樹狀數組2

[模板]樹狀數組2

www using one 分享圖片 lap name 圖片 clas pac

https://www.luogu.org/problemnew/show/P3368 技術分享圖片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 #define lowbit(a) (a&(-a))
 6 
 7 int n, m, t[500010], bas[500010];
 8 
 9 inline void update(int x, int k){
10     while(x <= n){
11         t[x] += k;
12 x += lowbit(x); 13 } 14 } 15 16 inline int Sum(int x){ 17 int ans = 0; 18 while(x){ 19 ans += t[x]; 20 x -= lowbit(x); 21 } 22 return ans; 23 } 24 25 int main(void){ 26 scanf("%d%d", &n, &m); 27 for(int i = 1; i <= n; ++i) scanf("
%d", bas+i); 28 while(m--){ 29 int bs, x, y, k; 30 scanf("%d", &bs); 31 if (bs == 1) { 32 scanf("%d%d%d", &x, &y, &k); 33 update(x,k), update(y+1,-k); 34 } 35 else { 36 scanf("%d", &x); 37 printf("
%d\n", bas[x] + Sum(x)); 38 } 39 } 40 return 0; 41 }
View Code

[模板]樹狀數組2