結果

問題 No.900 aδδitivee
ユーザー latte0119latte0119
提出日時 2019-10-04 22:17:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,580 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 165,668 KB
実行使用メモリ 17,344 KB
最終ジャッジ日時 2024-04-14 11:02:20
合計ジャッジ時間 5,696 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,880 KB
testcase_01 AC 5 ms
8,004 KB
testcase_02 AC 4 ms
7,880 KB
testcase_03 AC 4 ms
9,028 KB
testcase_04 AC 5 ms
8,900 KB
testcase_05 AC 5 ms
8,008 KB
testcase_06 AC 5 ms
8,388 KB
testcase_07 AC 102 ms
13,512 KB
testcase_08 AC 105 ms
13,508 KB
testcase_09 AC 101 ms
13,508 KB
testcase_10 AC 104 ms
13,508 KB
testcase_11 AC 105 ms
13,380 KB
testcase_12 AC 106 ms
13,380 KB
testcase_13 AC 109 ms
13,376 KB
testcase_14 AC 103 ms
13,384 KB
testcase_15 AC 105 ms
13,504 KB
testcase_16 AC 106 ms
13,380 KB
testcase_17 AC 105 ms
13,636 KB
testcase_18 AC 107 ms
13,376 KB
testcase_19 AC 103 ms
13,512 KB
testcase_20 AC 104 ms
13,380 KB
testcase_21 AC 110 ms
13,508 KB
testcase_22 AC 101 ms
17,224 KB
testcase_23 AC 101 ms
17,088 KB
testcase_24 AC 100 ms
17,220 KB
testcase_25 AC 97 ms
17,220 KB
testcase_26 AC 96 ms
17,220 KB
testcase_27 AC 98 ms
17,344 KB
testcase_28 AC 97 ms
17,220 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:60:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   60 |         scanf("%lld",&N);
      |         ~~~~~^~~~~~~~~~~
main.cpp:63:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |                 scanf("%lld%lld%lld",&a,&b,&c);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:70:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |         int Q;scanf("%lld",&Q);
      |               ~~~~~^~~~~~~~~~~
main.cpp:72:28: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |                 int t;scanf("%lld",&t);
      |                       ~~~~~^~~~~~~~~~~
main.cpp:75:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |                         scanf("%lld%lld",&a,&x);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:80:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   80 |                         int b;scanf("%lld",&b);
      |                               ~~~~~^~~~~~~~~~~

ソースコード

diff #

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N;
vpint G[111111];
int dep[111111];
int dist[111111];
int tt,tin[111111],tout[111111];

void dfs(int v,int p,int d,int c){
	tin[v]=tt++;
	dep[v]=d;
	dist[v]=c;
	for(auto e:G[v]){
		if(e.fi==p)continue;
		dfs(e.fi,v,d+1,c+e.se);
	}
	tout[v]=tt;
}


/*
0-indexed
add(k,x): a[k]+=x
sum(k): sum(a[0,k])
space:O(N)
time:O(logN) per query
*/
struct BinaryIndexedTree{
	int n;
	vector<int>dat;
	BinaryIndexedTree(int n=0):n(n){
		dat.resize(n+1);
	}
	void add(int k,int x){
		for(k++;k<=n;k+=k&-k)dat[k]+=x;
	}
	int sum(int k){
		int ret=0;
		for(k++;k;k-=k&-k)ret+=dat[k];
		return ret;
	}
};

signed main(){
	scanf("%lld",&N);
	rep(i,N-1){
		int a,b,c;
		scanf("%lld%lld%lld",&a,&b,&c);
		G[a].pb({b,c});
	}	
	dfs(0,-1,0,0);

	BinaryIndexedTree bit0(111111),bit1(111111);

	int Q;scanf("%lld",&Q);
	while(Q--){
		int t;scanf("%lld",&t);
		if(t==1){
			int a,x;
			scanf("%lld%lld",&a,&x);
			bit1.add(tin[a],x);bit1.add(tout[a],-x);
			bit0.add(tin[a],-dep[a]*x);bit0.add(tout[a],dep[a]*x);
		}
		else{
			int b;scanf("%lld",&b);
			int ans=dist[b];
			ans+=bit0.sum(tin[b]);
			ans+=bit1.sum(tin[b])*dep[b];
			printf("%lld\n",ans);
		}
	}
	return 0;
}
0