結果
| 問題 |
No.2676 A Tourist
|
| コンテスト | |
| ユーザー |
Jinapetto
|
| 提出日時 | 2024-03-20 17:59:09 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 400 ms / 5,000 ms |
| コード長 | 3,475 bytes |
| コンパイル時間 | 3,123 ms |
| コンパイル使用メモリ | 257,548 KB |
| 実行使用メモリ | 35,328 KB |
| 最終ジャッジ日時 | 2024-09-30 06:09:06 |
| 合計ジャッジ時間 | 13,184 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 31 |
ソースコード
#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;
}
Jinapetto