結果
問題 | No.399 動的な領主 |
ユーザー | Taiki0715 |
提出日時 | 2023-08-25 16:27:15 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 526 ms / 2,000 ms |
コード長 | 4,222 bytes |
コンパイル時間 | 6,518 ms |
コンパイル使用メモリ | 183,172 KB |
実行使用メモリ | 25,216 KB |
最終ジャッジ日時 | 2024-12-24 01:28:15 |
合計ジャッジ時間 | 12,673 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
#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 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_popcount(x) ll myceil(ll a,ll b){return (a+b-1)/b;} #ifdef LOCAL #include "debug.h" #else #define debug(...) 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 void SOLVE(); int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); #ifdef LOCAL clock_t start=clock(); #endif int testcase=1; //cin>>testcase; for(int i=0;i<testcase;i++){ SOLVE(); } #ifdef LOCAL cout<<"time:"; cout<<(clock()-start)/1000; cout<<"ms\n"; #endif } struct HeavyLightDecomposition{ private: int n; vector<vector<int>>to; int root; vector<int>par,pathtop,in,out; public: vector<int>stsize; private: void st_dfs(int x,int p){ par[x]=p; stsize[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); stsize[x]+=stsize[i]; if(stsize[i]>stsize[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){ 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),stsize(n),par(n),in(n),out(n),root(root),pathtop(n){} void add_edge(int u,int v){ to[u].emplace_back(v); to[v].emplace_back(u); } void build(){ st_dfs(0,-1); pathtop[root]=root; int id=0; hld_dfs(0,-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); } }; struct S{ int sz; ll sum; }; S op(S a,S b){ return {a.sz+b.sz,a.sum+b.sum}; } S e(){return {0,0};} S mapping(ll f,S x){ if(f==0)return x; x.sum+=f*x.sz; return x; } ll composition(ll a,ll b){return a+b;} ll id(){return 0;} void SOLVE(){ int n; cin>>n; HeavyLightDecomposition hld(n); rep(i,n-1){ int u,v; cin>>u>>v; u--,v--; hld.add_edge(u,v); } hld.build(); int q; cin>>q; vector<S>hoge(n,{1,0}); lazy_segtree<S,op,e,ll,mapping,composition,id>seg(hoge); ll ans=0; while(q--){ int u,v; cin>>u>>v; u--,v--; hld.path_query(u,v,[&](int a,int b){seg.apply(a,b,1);}); hld.path_query(u,v,[&](int a,int b){ans+=seg.prod(a,b).sum;}); debug(ans); } cout<<ans<<endl; }