結果

問題 No.900 aδδitivee
ユーザー snow39snow39
提出日時 2020-03-06 15:25:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 4,480 bytes
コンパイル時間 1,680 ms
コンパイル使用メモリ 112,720 KB
実行使用メモリ 25,984 KB
最終ジャッジ日時 2024-04-22 03:56:19
合計ジャッジ時間 9,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 265 ms
21,888 KB
testcase_08 AC 264 ms
21,888 KB
testcase_09 AC 267 ms
21,888 KB
testcase_10 AC 265 ms
22,016 KB
testcase_11 AC 271 ms
21,888 KB
testcase_12 AC 279 ms
21,940 KB
testcase_13 AC 263 ms
21,924 KB
testcase_14 AC 263 ms
22,016 KB
testcase_15 AC 265 ms
21,988 KB
testcase_16 AC 271 ms
22,012 KB
testcase_17 AC 275 ms
21,888 KB
testcase_18 AC 263 ms
22,016 KB
testcase_19 AC 265 ms
21,888 KB
testcase_20 AC 263 ms
22,016 KB
testcase_21 AC 273 ms
21,888 KB
testcase_22 AC 198 ms
25,984 KB
testcase_23 AC 194 ms
25,984 KB
testcase_24 AC 191 ms
25,776 KB
testcase_25 AC 194 ms
25,984 KB
testcase_26 AC 196 ms
25,856 KB
testcase_27 AC 200 ms
25,984 KB
testcase_28 AC 195 ms
25,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

class LazySegmentTree{
private:
  int n;
  vector<ll> node,lazy;

public:
  LazySegmentTree(vector<ll> v){
      n=1;
      while(n<v.size()) n*=2;
      node.resize(2*n-1,0);
      lazy.resize(2*n-1,0);
      
      for(int i=0;i<v.size();i++) node[i+n-1]=v[i];
      for(int i=n-2;i>=0;i--) node[i]=node[2*i+1]+node[2*i+2];
  }

  void eval(int k,int l,int r){
      if(lazy[k]!=0){
          node[k]+=lazy[k];
          if(r-l>1){
              lazy[2*k+1]+=lazy[k]/2;
              lazy[2*k+2]+=lazy[k]/2;
          }
          lazy[k]=0;
      }
  }

  void add(int a,int b,ll x,int k=0,int l=0,int r=-1){
      if(r<0) r=n;
      eval(k,l,r);
      if(b<=l||r<=a) return;
      if(a<=l&&r<=b){
          lazy[k]+=(r-l)*x;
          eval(k,l,r);
      }else{
          add(a,b,x,2*k+1,l,(r+l)/2);
          add(a,b,x,2*k+2,(r+l)/2,r);
          node[k]=node[2*k+1]+node[2*k+2];
      }
  }

  ll getSum(int a,int b,int k=0,int l=0,int r=-1){
      if(r<0) r=n;
      eval(k,l,r);
      if(b<=l||r<=a) return 0;
      if(a<=l&&r<=b) return node[k];

      ll vl,vr;
      vl=getSum(a,b,2*k+1,l,(l+r)/2);
      vr=getSum(a,b,2*k+2,(l+r)/2,r);
      return vl+vr;
  }
};

struct Edge{
    int to,id;
    ll dist;
    Edge(int to,ll dist=1,int id=0):to(to),dist(dist),id(id){}
};

// this class's update method is empty.
struct HeavyLightDecomposition{
    vector<vector<Edge>> g;
    vector<int> in,out,head,par,dep,sz;
    int times;
    int root;

    HeavyLightDecomposition(int V,vector<vector<Edge>> &G,int root=0):
        g(G),in(V),out(V),head(V),par(V),dep(V),sz(V),root(root){
            times=0;
            sz_dfs(root,-1);
            hld_dfs(root,-1);
    }
    
    void sz_dfs(int now,int p){
        par[now]=p;
        sz[now]=1;
        if(p==-1) dep[now]=0;
        else dep[now]=dep[p]+1;

        for(auto &e:g[now]){
            if(e.to==p) continue;
            sz_dfs(e.to,now);
            sz[now]+=sz[e.to];
            if(sz[e.to]>sz[g[now][0].to]) swap(e,g[now][0]);
        }
    }

    void hld_dfs(int now,int p){
        in[now]=times++;
        for(auto e:g[now]){
            if(e.to==p) continue;
            head[e.to]=(e.to == g[now][0].to ? head[now] : e.to);
            hld_dfs(e.to,now);
        }
        out[now]=times;
    }


    int lca(int u,int v){
        for(;;v=par[head[v]]){
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) return u;
        }
    }
    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    /*ex)
        hld.update(u,x,[&](int a,int b,ll x){
            seg.add(a,b,x);
        });
    */
    template<class T,class G>
    void update(int v,const T &x,const G &g){//辺の時は0を使わない(INFのまま)
        g(in[v]+1,out[v],x);//部分木(辺)
        //g(in[v],out[v],x);//部分木(頂点)
        //g(in[v],in[v]+1,x);//一点
    }

    /*ex)
        ll ans=0;
        hld.query(u,v,[&](int a,int b){
            ans+=seg.getSum(a,b);
        },true);
    */
    template<class F>
    void query(int u,int v,const F &f,bool isedge){
        for(;;v=par[head[v]]){
            if(in[u]>in[v]) swap(u,v);
            if(head[u]==head[v]) break;
            f(in[head[v]],in[v]+1);
        }
        if(isedge&&u==v) return;
        f(in[u]+isedge,in[v]+1);
    }
};

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

  int N;
  cin>>N;
  vector<int> U(N-1),V(N-1),W(N-1);
  rep(i,N-1) cin>>U[i]>>V[i]>>W[i];

  vector<vector<Edge>> g(N);
  rep(i,N-1) g[U[i]].push_back({V[i],W[i]});
  HeavyLightDecomposition hld(N,g,0);

  vector<ll> v(N,0);
  rep(i,N-1) v[hld.in[V[i]]]=W[i];
  LazySegmentTree seg(v);

  int Q;
  cin>>Q;
  rep(q,Q){
    int t;
    cin>>t;
    if(t==1){
      int a,x;
      cin>>a>>x;
      hld.update(a,x,[&](int a,int b,ll c){
        seg.add(a,b,c);
      });
    }else{
      int b;
      cin>>b;
      
      ll ans=0;
      hld.query(0,b,[&](int a,int b){
        ans+=seg.getSum(a,b);
      },true);
      cout<<ans<<endl;
    }
  }


  return 0;
}
0