結果
問題 | No.2676 A Tourist |
ユーザー | Taiki0715 |
提出日時 | 2024-03-15 23:15:53 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 6,617 bytes |
コンパイル時間 | 4,956 ms |
コンパイル使用メモリ | 326,704 KB |
実行使用メモリ | 41,752 KB |
最終ジャッジ日時 | 2024-09-30 02:49:46 |
合計ジャッジ時間 | 17,939 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 195 ms
12,032 KB |
testcase_02 | AC | 646 ms
29,824 KB |
testcase_03 | AC | 242 ms
29,824 KB |
testcase_04 | AC | 355 ms
29,780 KB |
testcase_05 | AC | 443 ms
29,824 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 136 ms
30,696 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | AC | 474 ms
28,080 KB |
testcase_12 | AC | 143 ms
16,000 KB |
testcase_13 | AC | 436 ms
41,752 KB |
testcase_14 | AC | 200 ms
41,728 KB |
testcase_15 | AC | 287 ms
41,684 KB |
testcase_16 | AC | 182 ms
12,160 KB |
testcase_17 | AC | 595 ms
30,044 KB |
testcase_18 | AC | 251 ms
30,068 KB |
testcase_19 | AC | 530 ms
29,972 KB |
testcase_20 | AC | 435 ms
30,044 KB |
testcase_21 | AC | 34 ms
6,816 KB |
testcase_22 | AC | 81 ms
6,816 KB |
testcase_23 | AC | 61 ms
6,816 KB |
testcase_24 | AC | 108 ms
6,816 KB |
testcase_25 | AC | 19 ms
6,820 KB |
testcase_26 | AC | 2 ms
6,816 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 153 ms
30,596 KB |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 711 ms
41,720 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;} istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;} #endif #ifdef LOCAL #include "debug.h" #else #define debug(...) static_cast<void>(0) #define debugg(...) static_cast<void>(0) template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;} #endif using ll=long long; using ull=unsigned long long; using P=pair<ll,ll>; template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>; template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);} template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;} template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;} template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;} template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;} template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;} template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;} #define reps(i,a,n) for(int i=(a);i<(n);i++) #define rep(i,n) reps(i,0,n) #define all(x) x.begin(),x.end() #define pcnt(x) __builtin_popcountll(x) #define fin(x) return cout<<x<<'\n',static_cast<void>(0) ll myceil(ll a,ll b){return (a+b-1)/b;} template<typename T,size_t n,size_t id=0> auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init)); else return init; } void SOLVE(); int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout<<fixed<<setprecision(16); #ifdef LOCAL clock_t start=clock(); #endif int testcase=1; //cin>>testcase; for(int i=0;i<testcase;i++){ SOLVE(); } #ifdef LOCAL cerr<<"time:"; cerr<<(clock()-start)/1000; cerr<<"ms\n"; #endif } struct HeavyLightDecomposition{ int n; vector<vector<int>>to; int root; vector<int>par,pathtop,in,out,dep; vector<int>sub; private: void st_dfs(int x,int p){ par[x]=p; sub[x]=1; for(auto &i:to[x]){ if(i==p){ if(i==to[x].back())break; else swap(i,to[x].back()); } st_dfs(i,x); sub[x]+=sub[i]; if(sub[i]>sub[to[x][0]]){ swap(i,to[x][0]); } } } void hld_dfs(int x,int p,int &id){ in[x]=id++; for(auto i:to[x])if(i!=p){ dep[i]=dep[x]+1; pathtop[i]=(i==to[x][0]?pathtop[x]:i); hld_dfs(i,x,id); } out[x]=id; } public: HeavyLightDecomposition(int n,int root=0):n(n),to(n),sub(n),par(n),in(n),out(n),root(root),pathtop(n),dep(n),g(n){} void add_edge(int u,int v){ to[u].emplace_back(v); to[v].emplace_back(u); } void build(){ dep[root]=0; st_dfs(root,-1); pathtop[root]=root; int id=0; hld_dfs(root,-1,id); } inline int get(int x)const{return in[x];} int lca(int u,int v)const{ int pu=pathtop[u],pv=pathtop[v]; while(pathtop[u]!=pathtop[v]){ if(in[pu]>in[pv])u=par[pu],pu=pathtop[u]; else v=par[pv],pv=pathtop[v]; } return (in[u]>in[v]?v:u); } void subtree_query(int u ,const function<void(int,int)> &f){ f(in[u],out[u]); } void path_query(int u,int v,const function<void(int,int)>&f){ int pu=pathtop[u],pv=pathtop[v]; while(pathtop[u]!=pathtop[v]){ if(in[u]>in[v])f(in[pu],in[u]+1),u=par[pu],pu=pathtop[u]; else f(in[pv],in[v]+1),v=par[pv],pv=pathtop[v]; } if(in[u]>in[v])swap(u,v); f(in[u],in[v]+1); } int distance(int u,int v){ int l=lca(u,v); return dep[u]+dep[v]-dep[l]*2; } void noncommutative_path_query(int u,int v,const function<void(int,int)>&f){ int l=lca(u,v); while(pathtop[u]!=pathtop[l]){ f(in[u]+1,in[pathtop[u]]); u=par[pathtop[u]]; } if(u!=l)f(in[u]+1,in[l]+1); f(in[l],in[l]+1); if(v==l)return; vector<pair<int,int>>query; while(true){ if(pathtop[l]==pathtop[v]){ query.emplace_back(in[l]+1,in[v]+1); break; } query.emplace_back(in[pathtop[v]],in[v]+1); v=par[pathtop[v]]; } reverse(all(query)); for(auto [i,j]:query)f(i,j); } vector<vector<int>>g; int auxiliary_tree(vector<int>v){ sort(all(v),[&](int x,int y){return in[x]<in[y];}); v.reserve(v.size()*2-1); int vs=v.size(); reps(i,1,vs)v.push_back(lca(v[i-1],v[i])); sort(all(v),[&](int x,int y){return in[x]<in[y];}); v.erase(unique(all(v)),v.end()); rep(i,v.size())g[v[i]].clear(); stack<int>st; rep(i,v.size()){ while(!st.empty()&&out[st.top()]<=in[v[i]])st.pop(); if(!st.empty()){ g[st.top()].push_back(v[i]); g[v[i]].push_back(st.top()); } st.push(v[i]); } while(st.size()>1)st.pop(); return st.top(); } }; constexpr int b=2000; void SOLVE(){ int n,q; cin>>n>>q; vector<ll>a(n); cin>>a; HeavyLightDecomposition hld(n); rep(i,n-1){ int u,v; cin>>u>>v; u--,v--; hld.add_edge(u,v); } vector<int>bigv; hld.build(); fenwick_tree<ll>BIT(n),around(n); vector<ll>vertex(n,0); rep(i,n){ int id=hld.get(i); BIT.add(id,a[i]); if(hld.to[i].size()<b)for(auto j:hld.to[i])around.add(hld.get(j),a[i]); else{ vertex[i]+=a[i]; bigv.push_back(i); } } while(q--){ int t; cin>>t; if(t==0){ int u; ll x; cin>>u>>x; u--; BIT.add(hld.get(u),x); if(hld.to[u].size()<b){ for(auto j:hld.to[u])around.add(hld.get(j),x); } else vertex[u]+=x; } else{ int u,v; cin>>u>>v; u--,v--; if(u==v){ cout<<around.sum(hld.get(u),hld.get(v)+1)+BIT.sum(hld.get(u),hld.get(u)+1)<<endl; continue; } ll ans1=0,ans2=0; hld.path_query(u,v,[&](int l,int r){ans1+=BIT.sum(l,r),ans2+=around.sum(l,r);}); debug(ans1,ans2); ll ans=ans2+BIT.sum(hld.get(u),hld.get(u)+1)+BIT.sum(hld.get(v),hld.get(v)+1)-ans1; debug(ans); // ll ans=ans1+(ans2+around.sum(hld.get(u),hld.get(u)+1)+around.sum(hld.get(v),hld.get(v)+1))/2; for(auto i:bigv){ int d1=hld.distance(u,i),d2=hld.distance(i,v),d3=hld.distance(u,v); if(d1+d2==d3)ans+=vertex[i]*2; else if(d1+d2==d3+2)ans+=vertex[i]; } cout<<ans<<endl; } } }