結果

問題 No.1038 TreeAddQuery
ユーザー beetbeet
提出日時 2020-03-02 20:19:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,433 ms / 4,000 ms
コード長 4,479 bytes
コンパイル時間 3,250 ms
コンパイル使用メモリ 226,064 KB
実行使用メモリ 22,420 KB
最終ジャッジ日時 2023-08-16 14:51:00
合計ジャッジ時間 19,994 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 11 ms
4,376 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 12 ms
4,376 KB
testcase_08 AC 692 ms
16,584 KB
testcase_09 AC 731 ms
16,496 KB
testcase_10 AC 755 ms
16,648 KB
testcase_11 AC 760 ms
16,644 KB
testcase_12 AC 785 ms
16,552 KB
testcase_13 AC 1,361 ms
22,400 KB
testcase_14 AC 1,152 ms
18,108 KB
testcase_15 AC 1,074 ms
17,716 KB
testcase_16 AC 1,030 ms
17,096 KB
testcase_17 AC 1,017 ms
17,004 KB
testcase_18 AC 122 ms
17,044 KB
testcase_19 AC 157 ms
17,052 KB
testcase_20 AC 150 ms
16,996 KB
testcase_21 AC 195 ms
16,948 KB
testcase_22 AC 280 ms
16,980 KB
testcase_23 AC 304 ms
17,080 KB
testcase_24 AC 505 ms
16,904 KB
testcase_25 AC 1,433 ms
22,420 KB
testcase_26 AC 695 ms
21,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


struct Centroid{
  vector<int> sz,dead;
  vector< vector<int> > G;
  Centroid(){}
  Centroid(int n):sz(n,1),dead(n,0),G(n){}

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

  int dfs(int v,int p){
    sz[v]=1;
    for(int u:G[v])
      if(u!=p&&!dead[u]) sz[v]+=dfs(u,v);
    return sz[v];
  }

  void find(int v,int p,int tmp,vector<int> &cs) {
    int ok=1;
    for (int u:G[v]){
      if(u==p||dead[u]) continue;
      find(u,v,tmp,cs);
      ok&=(sz[u]<=tmp/2);
    }
    ok&=(tmp-sz[v]<=tmp/2);
    if(ok) cs.push_back(v);
  }

  vector<int> build(int r) {
    int tmp=dfs(r,-1);
    vector<int> cs;
    find(r,-1,tmp,cs);
    return cs;
  }

  void disable(int v){
    dead[v]=1;
  }

  void enable(int v){
    dead[v]=0;
  }

  int alive(int v){
    return !dead[v];
  }
};


template <typename E>
struct SegmentTree{
  using H = function<E(E,E)>;
  int n,height;
  H h;
  E ei;
  vector<E> laz;

  SegmentTree(H h,E ei):h(h),ei(ei){}

  void init(int n_){
    n=1;height=0;
    while(n<n_) n<<=1,height++;
    laz.assign(2*n,ei);
  }

  inline void propagate(int k){
    if(laz[k]==ei) return;
    laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
    laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
    laz[k]=ei;
  }

  inline void thrust(int k){
    for(int i=height;i;i--) propagate(k>>i);
  }

  void update(int a,int b,E x){
    if(a>=b) return;
    thrust(a+=n);
    thrust(b+=n-1);
    for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
      if(l&1) laz[l]=h(laz[l],x),l++;
      if(r&1) --r,laz[r]=h(laz[r],x);
    }
  }

  E get_val(int a){
    thrust(a+=n);
    return laz[a];
  }

  void set_val(int a,E x){
    thrust(a+=n);
    laz[a]=x;
  }
};


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  int n,q;
  cin>>n>>q;

  Centroid G(n);
  for(int i=1;i<n;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    G.add_edge(a,b);
  }

  vector<int> xs(q),ys(q),zs(q);
  for(int i=0;i<q;i++) cin>>xs[i]>>ys[i]>>zs[i];

  vector<vector<int>> H(n);
  for(int i=0;i<q;i++){
    xs[i]--;
    H[xs[i]].emplace_back(i);
  }

  vector<int> dep(n);

  using ll = long long;
  vector<ll> ans(q,0);
  auto h=[&](ll a,ll b){return a+b;};
  SegmentTree<ll> seg(h,0);

  queue<int> que;
  que.emplace(G.build(0)[0]);
  while(!que.empty()){
    int r=que.front();que.pop();
    dep[r]=0;

    // add for all
    {
      int len=1;
      vector<int> qs(H[r]);
      for(int t:G.G[r]){
        if(!G.alive(t)) continue;
        MFP([&](auto dfs,int v,int p,int d)->void{
              dep[v]=d;
              chmax(len,d+1);
              for(int i:H[v]) qs.emplace_back(i);
              for(int u:G.G[v]){
                if(u==p) continue;
                if(!G.alive(u)) continue;
                dfs(u,v,d+1);
              }
            })(t,r,1);
      }
      seg.init(len);
      sort(qs.begin(),qs.end());
      for(int i:qs){
        ans[i]+=seg.get_val(dep[xs[i]]);
        if(ys[i]>=dep[xs[i]])
          seg.update(0,min(len,ys[i]-dep[xs[i]]+1),zs[i]);
      }
    }

    // sub for subtree
    {
      for(int t:G.G[r]){
        if(!G.alive(t)) continue;
        int len=1;
        vector<int> qs;
        MFP([&](auto dfs,int v,int p,int d)->void{
              dep[v]=d;
              chmax(len,d+1);
              for(int i:H[v]) qs.emplace_back(i);
              for(int u:G.G[v]){
                if(u==p) continue;
                if(!G.alive(u)) continue;
                dfs(u,v,d+1);
              }
            })(t,r,1);
        seg.init(len);
        sort(qs.begin(),qs.end());
        for(int i:qs){
          ans[i]-=seg.get_val(dep[xs[i]]);
          if(ys[i]>=dep[xs[i]])
            seg.update(0,min(len,ys[i]-dep[xs[i]]+1),zs[i]);
        }
      }
    }

    G.disable(r);
    for(int t:G.G[r])
      if(G.alive(t))
        que.emplace(G.build(t)[0]);
  }

  for(auto a:ans) cout<<a<<"\n";
  cout<<flush;
  return 0;
}
0