結果

問題 No.2676 A Tourist
ユーザー じなぺじなぺ
提出日時 2024-03-20 17:59:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 442 ms / 5,000 ms
コード長 3,475 bytes
コンパイル時間 3,462 ms
コンパイル使用メモリ 258,884 KB
実行使用メモリ 35,424 KB
最終ジャッジ日時 2024-03-20 17:59:25
合計ジャッジ時間 15,363 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
13,536 KB
testcase_01 AC 119 ms
16,608 KB
testcase_02 AC 442 ms
22,880 KB
testcase_03 AC 271 ms
22,880 KB
testcase_04 AC 302 ms
22,880 KB
testcase_05 AC 333 ms
22,880 KB
testcase_06 AC 77 ms
16,992 KB
testcase_07 AC 215 ms
23,776 KB
testcase_08 AC 138 ms
23,776 KB
testcase_09 AC 197 ms
23,776 KB
testcase_10 AC 148 ms
23,776 KB
testcase_11 AC 259 ms
22,368 KB
testcase_12 AC 82 ms
20,832 KB
testcase_13 AC 234 ms
35,424 KB
testcase_14 AC 200 ms
35,424 KB
testcase_15 AC 211 ms
35,424 KB
testcase_16 AC 110 ms
16,736 KB
testcase_17 AC 391 ms
23,008 KB
testcase_18 AC 275 ms
23,008 KB
testcase_19 AC 360 ms
23,008 KB
testcase_20 AC 322 ms
23,008 KB
testcase_21 AC 42 ms
13,536 KB
testcase_22 AC 38 ms
13,536 KB
testcase_23 AC 23 ms
13,536 KB
testcase_24 AC 36 ms
13,536 KB
testcase_25 AC 15 ms
13,536 KB
testcase_26 AC 9 ms
13,536 KB
testcase_27 AC 70 ms
16,992 KB
testcase_28 AC 201 ms
23,640 KB
testcase_29 AC 131 ms
23,640 KB
testcase_30 AC 193 ms
23,640 KB
testcase_31 AC 178 ms
23,640 KB
testcase_32 AC 158 ms
35,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
#include<atcoder/fenwicktree>
using namespace atcoder;
//using mint = modint998244353;

//多倍長整数//
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//using Bint = mp::cpp_int;

const int INF = 1e9;
const int MOD = 998244353;
const long long LINF = 4e18;

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvl = vector<vector<long long>>;
using vvvl = vector<vector<vector<long long>>>;
using vvvvl = vector<vector<vector<vector<long long>>>>;
using vvc = vector<vector<char>>;
using vvb = vector<vector<bool>>;
using vvvb = vector<vector<vector<bool>>>;
using vvvvb = vector<vector<vector<vector<bool>>>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define ALL(obj) (obj).begin(),(obj).end()

//https://beet-aizu.github.io/library/tree/heavylightdecomposition.cpp

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;
      }
    }
  }
};

fenwick_tree<ll> fw(2e5);
HLD hld(2e5);
ll ans;

void f(int a,int b){
	ans += fw.sum(a,b);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,q;
	cin >> n >> q;
	vl a(n);
	vl sum(n);
	rep(i,n) cin >> a[i];
	
	rep(i,n - 1){
		int u,v;
		cin >> u >> v;
		u--;v--;
		hld.add_edge(u,v);
	}
	hld.build();
	for(int i = 1;i < n;i++) sum[hld.par[i]] += a[i];
	rep(i,n) fw.add(hld.vid[i],sum[i]);
	for(;q--;){
		int op;
		cin >> op;
		if(op == 0){
			int p;
			ll x;
			cin >> p >> x;
			p--;
			a[p] += x;
			if(p != 0) fw.add(hld.vid[hld.par[p]],x);
		}else{
			int u,v;
			cin >> u >> v;
			u--;v--;
			ans = 0;
			hld.for_each(u,v,f);
			ans += a[hld.lca(u,v)];
			if(hld.lca(u,v) != 0) ans += a[hld.par[hld.lca(u,v)]];
			cout << ans << "\n";
		}
	}
	return 0;
}
0