zoukankan      html  css  js  c++  java
  • 洛谷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 }
  • 相关阅读:
    gdb常用命令
    gdb之watch命令
    gdb之x命令
    python's descriptor II
    MacOSX快捷键
    主题敏感词PageRank
    shell调试选项
    shell输出调试信息
    事务时间如何去掉wasted time
    深刻剖析VuGen脚本录制原理
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/11341319.html
Copyright © 2011-2022 走看看