码迷,mamicode.com
首页 > 编程语言 > 详细

[模板]树状数组2

时间:2018-02-18 10:32:41      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:close   def   aac   int   div   https   技术分享   http   技术   

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

标签:close   def   aac   int   div   https   技术分享   http   技术   

原文地址:https://www.cnblogs.com/Ycrpro/p/8452479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!