library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/yosupo/point_add_range_sum.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://judge.yosupo.jp/problem/point_add_range_sum

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../datastructure/binaryindexedtree.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,q;
  cin>>n>>q;

  vector<int> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  BIT<long long> bit(n);
  for(int i=0;i<n;i++) bit.add(i,as[i]);

  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    if(t==0){
      int p,x;
      cin>>p>>x;
      bit.add(p,x);
    }
    if(t==1){
      int l,r;
      cin>>l>>r;
      cout<<bit.query(l,r)<<'\n';
    }
  }
  return 0;
}
#line 1 "test/yosupo/point_add_range_sum.test.cpp"
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/point_add_range_sum

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "datastructure/binaryindexedtree.cpp"

#line 3 "datastructure/binaryindexedtree.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename T>
class BIT{
private:
  // \sum_{j < i}  v[j]
  T sum(int i){
    T s(0);
    for(int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }
public:
  int n;
  vector<T> bit;
  BIT(int n_):n(n_+1),bit(n_+2,0){}

  // v[i] += a
  void add(int i,T a){
    for(int x=++i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }

  // \sum_{l <= i < r} v[i]
  T query(int l,int r){return sum(r)-sum(l);}

  // min({x | sum(x) >= w})
  int lower_bound(const T w){
    if(w<=0) return 0;
    T r=w;
    int x=0,p=1;
    while(p<n) p<<=1;
    for(int k=p;k>0;k>>=1){
      if(x+k<=n and bit[x+k]<r){
        r-=bit[x+k];
        x+=k;
      }
    }
    x++;
    assert(sum(x-1)<w and sum(x)>=w);
    return x;
  }

  // min({x | sum(x) > w})
  int upper_bound(T w){return lower_bound(w+1);}
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 8 "test/yosupo/point_add_range_sum.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,q;
  cin>>n>>q;

  vector<int> as(n);
  for(int i=0;i<n;i++) cin>>as[i];

  BIT<long long> bit(n);
  for(int i=0;i<n;i++) bit.add(i,as[i]);

  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    if(t==0){
      int p,x;
      cin>>p>>x;
      bit.add(p,x);
    }
    if(t==1){
      int l,r;
      cin>>l>>r;
      cout<<bit.query(l,r)<<'\n';
    }
  }
  return 0;
}
Back to top page