洛谷P3374【模板】树状数组 1(单点更新+区间求和)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #define mem(a,b) memset(a,b,sizeof(a));
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn = 500005;
 8 const ll INF = 0x3f3f3f3f;
 9 int n,m;
10 ll a[maxn];
11 ll lowbit(ll t) {//取出t的最低位1
12     return t&(-t);
13 }
14 ll getsum(ll x) {
15     ll ans = 0;
16     for(int i = x; i > 0; i -= lowbit(i)) {
17         ans += a[i];
18     }
19     return ans;
20 }
21 void update(ll x,ll v) {
22     for(int i = x; i <= n; i+=lowbit(i)) {
23         a[i] += v;
24     }
25 }
26 int main()
27 {
28 
29     cin >> n >> m;
30     ll c;
31     for(int i = 1; i <= n; i++) {
32         cin >> c;
33         update(i,c);
34     }
35     int op;
36     ll x,y,z;
37     for(int i = 1; i <= m; i++) {
38         cin >> op;
39         if(op == 1) {
40             cin >> x >> z;
41             update(x,z);
42         }
43         else {
44             cin >> x >> y;
45             cout << getsum(y) - getsum(x-1) << endl;
46         }
47     }
48     return 0;
49 }

 

posted on 2019-08-12 17:14  一只小毛球  阅读(140)  评论(0编辑  收藏  举报

导航