結果

問題 No.900 aδδitivee
ユーザー tempura_pptempura_pp
提出日時 2019-10-04 21:44:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 422 ms / 2,000 ms
コード長 4,911 bytes
コンパイル時間 1,624 ms
コンパイル使用メモリ 117,216 KB
実行使用メモリ 21,952 KB
最終ジャッジ日時 2024-04-14 10:26:08
合計ジャッジ時間 12,073 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,800 KB
testcase_01 AC 7 ms
11,564 KB
testcase_02 AC 6 ms
11,528 KB
testcase_03 AC 7 ms
11,720 KB
testcase_04 AC 6 ms
11,648 KB
testcase_05 AC 6 ms
11,504 KB
testcase_06 AC 6 ms
11,568 KB
testcase_07 AC 419 ms
21,380 KB
testcase_08 AC 422 ms
21,600 KB
testcase_09 AC 416 ms
21,384 KB
testcase_10 AC 405 ms
21,404 KB
testcase_11 AC 421 ms
21,516 KB
testcase_12 AC 409 ms
21,424 KB
testcase_13 AC 403 ms
21,424 KB
testcase_14 AC 413 ms
21,440 KB
testcase_15 AC 408 ms
21,608 KB
testcase_16 AC 409 ms
21,516 KB
testcase_17 AC 412 ms
21,548 KB
testcase_18 AC 414 ms
21,556 KB
testcase_19 AC 415 ms
21,496 KB
testcase_20 AC 416 ms
21,580 KB
testcase_21 AC 417 ms
21,584 KB
testcase_22 AC 330 ms
21,764 KB
testcase_23 AC 330 ms
21,684 KB
testcase_24 AC 323 ms
21,812 KB
testcase_25 AC 323 ms
21,872 KB
testcase_26 AC 316 ms
21,824 KB
testcase_27 AC 318 ms
21,952 KB
testcase_28 AC 323 ms
21,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1} ;
 
struct LazySegmentTree{
private:
   int n;
   vector<ll> node,lazy;
public:
   LazySegmentTree(int sz,ll init=0){
      n=1;
      while(n<sz)n*=2;
      node.resize(2*n-1,init);
      lazy.resize(2*n-1,0);
   }
 
   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;
   }
      //[a,b)にxを加算
   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(r<=a||b<=l)return;
       if(a<=l&&r<=b){
          lazy[k]+=(r-l)*x;
          eval(k,l,r);
       }
       else {
          add(a,b,x,2*k+1,l,(l+r)/2);
          add(a,b,x,2*k+2,(l+r)/2,r);
          node[k]=node[2*k+1]+node[2*k+2];
        }
   }
   //[a,b)での和を返す
   ll get(int a,int b,int k=0,int l=0,int r=-1){
      if(r<0)r=n;
      eval(k,l,r);
      if(r<=a||b<=l)return 0;
      if(a<=l&&r<=b)return node[k];
      ll xl=get(a,b,2*k+1,l,(l+r)/2);
      ll xr=get(a,b,2*k+2,(l+r)/2,r);
      return xl+xr;
   }
};
 
LazySegmentTree sg(151515);
 
struct HLDecomposition{
    int n,pos;
    vector<vector<int>> v;
    vector<int> idx,head,sz,hvy,par,depth,inv,type;
 
    HLDecomposition(){};
    HLDecomposition(int s):
        n(s),pos(0),v(n),idx(n,-1),head(n),sz(n,1),
        hvy(n,-1),par(n),depth(n),inv(n),type(n){}
     
    void addedge(int x,int y){
        v[x].push_back(y);
        v[y].push_back(x);
    }
 
    void dfs1(int rt){
        par[rt]=-1;
        depth[rt]=0;
        stack<pint> st;
        st.push({rt,0});
        while(!st.empty()){
            int x=st.top().first;
            int& i=st.top().second;
            if(i<(int)v[x].size()){
                int to=v[x][i++];
                if(to==par[x])continue;
                par[to]=x;
                depth[to]=depth[x]+1;
                st.push({to,0});
            }
            else {
                st.pop();
                int res=0;
                for(int to:v[x]){
                    if(to==par[x])continue;
                    sz[x]+=sz[to];
                    if(sz[to]>res)res=sz[to],hvy[x]=to;
                }
            }
        }
    }
    void dfs2(int r,int c){
        int &k=pos;
        stack<int> st;
        st.push(r);
        while(!st.empty()){
            int h=st.top();st.pop();
            for(int x=h;x!=-1;x=hvy[x]){
                type[x]=c;
                head[x]=h;
                idx[x]=k++;
                inv[idx[x]]=x;
                for(int to:v[x])
                    if(to!=par[x]&&to!=hvy[x])st.push(to);
            }
        }
    }
 
    void build(vector<int> rs=vector<int>(1,0)){
        int c=0;
        for(int r:rs){
            dfs1(r);
            dfs2(r,c++);
        }
    }
    ll f(int x,int y){
        //ここに何か書く!!!
        return sg.get(x,y+1);
    }
 
    void for_v(int x,int y){
        while(1){
            if(idx[x]>idx[y])swap(x,y);
            f(max(idx[head[y]],idx[x]),idx[y]);
            if(head[x]!=head[y])y=par[head[y]];
            else break;
        }
    }
 
    ll for_edge(int x,int y){
        ll ret=0;
        while(1){
            if(idx[x]>idx[y])swap(x,y);
            if(head[x]!=head[y]){
                ret+=f(idx[head[y]],idx[y]);
                y=par[head[y]];
            }
            else{
                if(x!=y)ret+=f(idx[x]+1,idx[y]);
                break;
            }
        }
        return ret;
    }
    int lca(int x,int y){
        while(1){
            if(idx[x]>idx[y])swap(x,y);
            if(head[x]==head[y])return x;
            y=par[head[y]];
        }
    }
 
    int dist(int x,int y){
        return depth[x]+depth[y]-2*depth[lca(x,y)];
    }
};
int main(){
    int n,q;
    cin>>n;
    HLDecomposition hl(n);
	vector<int> x(n-1),y(n-1),z(n-1);
    rep(i,n-1){
        scanf("%d %d %d",&x[i],&y[i],&z[i]);
        hl.addedge(x[i], y[i]);
    }
    hl.build();
	rep(i,n-1){
		if(hl.idx[x[i]]<hl.idx[y[i]]){
			sg.add(hl.idx[y[i]],hl.idx[y[i]]+1,z[i]);
		}
		else {
			sg.add(hl.idx[x[i]],hl.idx[x[i]]+1,z[i]);
		}
	}
	cin>>q;
    while(q--){
        int c,x,y;
        scanf("%d",&c);
        if(c==1){
			cin>>x>>y;
            int idx=hl.idx[x];
            sg.add(idx+1,idx+hl.sz[x],y);
        }
        else {
			cin>>x;
            printf("%lld\n",hl.for_edge(0, x));
        }
    }
   return 0;
}
0