結果

問題 No.2676 A Tourist
ユーザー kotatsugamekotatsugame
提出日時 2024-03-15 22:05:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,426 bytes
コンパイル時間 978 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 35,128 KB
最終ジャッジ日時 2024-03-15 22:05:44
合計ジャッジ時間 11,174 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 WA -
testcase_02 AC 436 ms
22,584 KB
testcase_03 AC 300 ms
22,584 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 233 ms
22,840 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 250 ms
21,440 KB
testcase_12 WA -
testcase_13 AC 241 ms
35,128 KB
testcase_14 AC 229 ms
35,128 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 412 ms
22,712 KB
testcase_18 AC 303 ms
22,712 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 3 ms
6,676 KB
testcase_27 WA -
testcase_28 AC 215 ms
22,776 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<atcoder/fenwicktree>
using namespace std;
class HLD{
private:
  void dfs_sz(int v) {
    auto &es=G[v];
    if(~par[v]) es.erase(find(es.begin(),es.end(),par[v]));

    for(int &u:es){
      par[u]=v;
      dfs_sz(u);
      sub[v]+=sub[u];
      if(sub[u]>sub[es[0]]) swap(u,es[0]);
    }
  }

  void dfs_hld(int v,int &pos) {
    vid[v]=pos++;
    inv[vid[v]]=v;
    for(int u:G[v]){
      if(u==par[v]) continue;
      nxt[u]=(u==G[v][0]?nxt[v]:u);
      dfs_hld(u,pos);
    }
  }

public:
  vector< vector<int> > G;

  // vid: vertex -> idx
  // inv: idx -> vertex
  vector<int> vid,nxt,sub,par,inv;

  HLD(int n):G(n),vid(n,-1),nxt(n),sub(n,1),par(n,-1),inv(n){}

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

  void build(int r=0) {
    int pos=0;
    dfs_sz(r);
    nxt[r]=r;
    dfs_hld(r,pos);
  }

  int lca(int u,int v){
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      if(nxt[u]==nxt[v]) return u;
      v=par[nxt[v]];
    }
  }

  template<typename F>
  void for_each(int u,int v,const F& f) {
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      f(max(vid[nxt[v]],vid[u]),vid[v]+1);
      if(nxt[u]!=nxt[v]) v=par[nxt[v]];
      else break;
    }
  }

  template<typename F>
  void for_each_edge(int u,int v,const F& f) {
    while(1){
      if(vid[u]>vid[v]) swap(u,v);
      if(nxt[u]!=nxt[v]){
        f(vid[nxt[v]],vid[v]+1);
        v=par[nxt[v]];
      }else{
        if(u!=v) f(vid[u]+1,vid[v]+1);
        break;
      }
    }
  }
};
int A[2<<17];
long S[2<<17];
int N,Q;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>Q;
	for(int i=0;i<N;i++)cin>>A[i];
	HLD hld(N);
	for(int i=1;i<N;i++)
	{
		int u,v;cin>>u>>v;u--,v--;
		hld.add_edge(u,v);
	}
	hld.build(0);
	for(int i=1;i<N;i++)S[hld.par[i]]+=A[i];
	atcoder::fenwick_tree<long long>BIT(N);
	for(int i=0;i<N;i++)BIT.add(hld.vid[i],S[i]);
	for(int i=0;i<Q;i++)
	{
		int op;cin>>op;
		if(op==0)
		{
			int v,x;cin>>v>>x;
			v--;
			A[v]+=x;
			if(v!=0)
			{
				S[hld.par[v]]+=x;
				BIT.add(hld.vid[hld.par[v]],x);
			}
		}
		else
		{
			int u,v;cin>>u>>v;u--,v--;
			int w=hld.lca(u,v);
			long ans=A[w];
			if(w!=0)ans+=A[hld.par[w]];
			hld.for_each(w,u,[&](int l,int r){ans+=BIT.sum(l,r);});
			hld.for_each(w,v,[&](int l,int r){ans+=BIT.sum(l,r);});
			ans-=S[w];
			cout<<ans<<"\n";
		}
	}
}
0