結果

問題 No.2676 A Tourist
ユーザー 沙耶花沙耶花
提出日時 2024-03-15 22:24:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 832 ms / 5,000 ms
コード長 2,663 bytes
コンパイル時間 4,917 ms
コンパイル使用メモリ 275,140 KB
実行使用メモリ 42,628 KB
最終ジャッジ日時 2024-03-15 22:24:58
合計ジャッジ時間 19,044 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 203 ms
9,760 KB
testcase_02 AC 832 ms
22,280 KB
testcase_03 AC 284 ms
22,280 KB
testcase_04 AC 449 ms
22,280 KB
testcase_05 AC 523 ms
22,280 KB
testcase_06 AC 161 ms
10,080 KB
testcase_07 AC 579 ms
23,124 KB
testcase_08 AC 145 ms
23,120 KB
testcase_09 AC 416 ms
23,124 KB
testcase_10 AC 181 ms
23,124 KB
testcase_11 AC 582 ms
21,100 KB
testcase_12 AC 160 ms
16,412 KB
testcase_13 AC 545 ms
42,628 KB
testcase_14 AC 220 ms
42,628 KB
testcase_15 AC 319 ms
42,628 KB
testcase_16 AC 197 ms
9,788 KB
testcase_17 AC 791 ms
22,444 KB
testcase_18 AC 280 ms
22,464 KB
testcase_19 AC 671 ms
22,476 KB
testcase_20 AC 478 ms
22,424 KB
testcase_21 AC 39 ms
6,676 KB
testcase_22 AC 94 ms
6,676 KB
testcase_23 AC 74 ms
6,676 KB
testcase_24 AC 121 ms
6,676 KB
testcase_25 AC 21 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 160 ms
10,016 KB
testcase_28 AC 514 ms
23,064 KB
testcase_29 AC 140 ms
23,064 KB
testcase_30 AC 480 ms
23,064 KB
testcase_31 AC 399 ms
23,064 KB
testcase_32 AC 162 ms
42,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
struct HLD{
	vector<int> sz,parent,depth,root,pos;
	vector<int> arr;
	HLD(vector<vector<int>> &E){
		sz.resize(E.size(),1);
		parent.resize(E.size(),0);
		depth.resize(E.size(),0);
		root.resize(E.size(),0);
		pos.resize(E.size(),0);
		
		dfs(0,-1,E);
		dfs2(0,-1,E,0);
	}
	
	void dfs(int now,int p,vector<vector<int>> &E){
		parent[now] = p;
		if(p==-1){
			depth[now] = 0;
		}
		else{
			depth[now] = depth[p]+1;
		}
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			dfs(to,now,E);
			sz[now] += sz[to];
		}
	}
	
	void dfs2(int now,int p,vector<vector<int>> &E,int r){
		pos[now] = arr.size();
		arr.push_back(now);
		root[now] = r;
		int maxi = 0;
		int ind = -1;
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p)continue;
			if(maxi<sz[to]){
				maxi = sz[to];
				ind = to;
			}
		}
		if(ind==-1)return;
		dfs2(ind,now,E,r);
		for(int i=0;i<E[now].size();i++){
			int to = E[now][i];
			if(to==p||to==ind)continue;
			dfs2(to,now,E,to);
		}
	}
	
	vector<pair<int,int>> query(int u,int v){
		vector<pair<int,int>> ret;
		int t = 0;
		while(root[u]!=root[v]){
			if(depth[root[u]] <= depth[root[v]]){
				ret.insert(ret.begin()+t,{pos[root[v]], pos[v]});
				v = parent[root[v]];
			}
			else{
				ret.insert(ret.begin()+t,{pos[u],pos[root[u]]});
				u = parent[root[u]];
				t++;
			}
		}
		ret.insert(ret.begin()+t,{pos[u],pos[v]});
		return ret;
	}
	
	int lca(int u,int v){
		for(;;v=parent[root[v]]){
			if(pos[u]>pos[v])swap(u,v);
			if(root[u]==root[v])return u;
		}
	}
	
	int get_distance(int u,int v){
		return depth[u] + depth[v] - 2 * depth[lca(u,v)];
	}
	
};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n,q;
	cin>>n>>q;
	vector<long long> a(n);
	rep(i,n)cin>>a[i];
	
	vector<vector<int>> E(n);
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u--,v--;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	HLD H(E);
	fenwick_tree<long long> F(n);
	rep(i,n){
		if(i!=0)F.add(H.pos[H.parent[i]],a[i]);
	}
	
	rep(_,q){
		int t,x,y;
		cin>>t>>x>>y;
		if(t==0){
			x--;
			if(x!=0){
				F.add(H.pos[H.parent[x]],y);
			}
			a[x] += y;
		}
		else{
			x--,y--;
			auto ret = H.query(x,y);
			long long ans = 0;
			rep(i,ret.size()){
				int l = ret[i].first,r = ret[i].second;
				if(l>r)swap(l,r);
				ans += F.sum(l,r+1);
			}
			int l = H.lca(x,y);
			ans += a[l];
			if(l!=0)ans += a[H.parent[l]];
			cout<<ans<<endl;
		}
	}
	
	return 0;
}
0