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/vertex_add_subtree_sum.test.cpp

Depends on

Code

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

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

#define call_from_test
#include "../../tree/eulertourforvertex.cpp"
#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];

  EulerTourForVertex G(n);
  for(int i=1;i<n;i++){
    int p;
    cin>>p;
    G.add_edge(p,i);
  }
  G.build(0);

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

  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    if(t==0){
      int u,x;
      cin>>u>>x;
      bit.add(G.idx(u),x);
    }
    if(t==1){
      int u;
      cin>>u;
      G.exec(u,[&](int l,int r){cout<<bit.query(l,r)<<"\n";});
    }
  }
  cout<<flush;
  return 0;
}
#line 1 "test/yosupo/vertex_add_subtree_sum.test.cpp"
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/vertex_add_subtree_sum

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

#define call_from_test
#line 1 "tree/eulertourforvertex.cpp"

#line 3 "tree/eulertourforvertex.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
class EulerTourForVertex{
private:
  vector<int> ls,rs;
  int pos;

  void dfs(int v,int p){
    ls[v]=pos++;
    for(int u:G[v])
      if(u!=p) dfs(u,v);
    rs[v]=pos;
  }

public:
  vector< vector<int> > G;
  EulerTourForVertex(int n):ls(n),rs(n),G(n){}

  void add_edge(int u,int v){
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  void build(int r=0){
    pos=0;
    dfs(r,-1);
  }

  int idx(int v){return ls[v];}

  template<typename F>
  void exec(int v,F f){
    f(ls[v],rs[v]);
  }
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#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 9 "test/yosupo/vertex_add_subtree_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];

  EulerTourForVertex G(n);
  for(int i=1;i<n;i++){
    int p;
    cin>>p;
    G.add_edge(p,i);
  }
  G.build(0);

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

  for(int i=0;i<q;i++){
    int t;
    cin>>t;
    if(t==0){
      int u,x;
      cin>>u>>x;
      bit.add(G.idx(u),x);
    }
    if(t==1){
      int u;
      cin>>u;
      G.exec(u,[&](int l,int r){cout<<bit.query(l,r)<<"\n";});
    }
  }
  cout<<flush;
  return 0;
}
Back to top page