結果
問題 | No.399 動的な領主 |
ユーザー | yuji9511 |
提出日時 | 2020-05-04 14:11:23 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 452 ms / 2,000 ms |
コード長 | 5,361 bytes |
コンパイル時間 | 2,670 ms |
コンパイル使用メモリ | 186,304 KB |
実行使用メモリ | 24,704 KB |
最終ジャッジ日時 | 2024-06-24 08:25:00 |
合計ジャッジ時間 | 6,921 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 4 ms
6,940 KB |
testcase_05 | AC | 27 ms
6,940 KB |
testcase_06 | AC | 433 ms
20,352 KB |
testcase_07 | AC | 439 ms
20,480 KB |
testcase_08 | AC | 428 ms
20,400 KB |
testcase_09 | AC | 421 ms
20,608 KB |
testcase_10 | AC | 5 ms
6,940 KB |
testcase_11 | AC | 20 ms
6,944 KB |
testcase_12 | AC | 303 ms
20,780 KB |
testcase_13 | AC | 292 ms
20,864 KB |
testcase_14 | AC | 90 ms
24,704 KB |
testcase_15 | AC | 140 ms
24,704 KB |
testcase_16 | AC | 192 ms
23,040 KB |
testcase_17 | AC | 452 ms
20,608 KB |
testcase_18 | AC | 430 ms
20,608 KB |
ソースコード
/*** author: yuji9511 ***/ #include <bits/stdc++.h> using namespace std; using ll = long long; using lpair = pair<ll, ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} struct AddLazySegmentTree { private: ll n; vector<ll> node, lazy; public: AddLazySegmentTree(vector<ll> v) { ll sz = (ll)v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1); lazy.resize(2*n-1, 0); for(ll i=0; i<sz; i++) node[i+n-1] = v[i]; for(ll i=n-2; i>=0; i--) node[i] = node[i*2+1] + node[i*2+2]; } void eval(ll k, ll l, ll r) { if(lazy[k] != 0) { node[k] += lazy[k]; if(r - l > 1) { lazy[2*k+1] += lazy[k] / 2; lazy[2*k+2] += lazy[k] / 2; } lazy[k] = 0; } } void add(ll a, ll b, ll x, ll k=0, ll l=0, ll r=-1) { if(r < 0) r = n; eval(k, l, r); if(b <= l || r <= a) return; if(a <= l && r <= b) { lazy[k] += (r - l) * x; eval(k, l, r); } else { add(a, b, x, 2*k+1, l, (l+r)/2); add(a, b, x, 2*k+2, (l+r)/2, r); node[k] = node[2*k+1] + node[2*k+2]; } } ll getsum(ll a, ll b, ll k=0, ll l=0, ll r=-1) { if(r < 0) r = n; eval(k, l, r); if(b <= l || r <= a) return 0; if(a <= l && r <= b) return node[k]; ll vl = getsum(a, b, 2*k+1, l, (l+r)/2); ll vr = getsum(a, b, 2*k+2, (l+r)/2, r); return vl + vr; } }; class HLD { private: void dfs_sz(ll v) { for(ll &u:G[v]) if(u==par[v]) swap(u,G[v].back()); if(~par[v]) G[v].pop_back(); for(ll &u:G[v]){ par[u]=v; dep[u]=dep[v]+1; dfs_sz(u); sub[v]+=sub[u]; if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]); } } void dfs_hld(ll v,ll c,ll &pos) { vid[v]=pos++; inv[vid[v]]=v; type[v]=c; for(ll u:G[v]){ if(u==par[v]) continue; head[u]=(u==G[v][0]?head[v]:u); dfs_hld(u,c,pos); } } public: vector< vector<ll> > G; vector<ll> vid, head, sub, par, dep, inv, type; HLD(ll n): G(n),vid(n,-1),head(n),sub(n,1), par(n,-1),dep(n,0),inv(n),type(n){} void add_edge(ll u,ll v) { G[u].emplace_back(v); G[v].emplace_back(u); } void build(vector<ll> rs={0}) { ll c=0,pos=0; for(ll r:rs){ dfs_sz(r); head[r]=r; dfs_hld(r,c++,pos); } } ll lca(ll u,ll v){ while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]==head[v]) return u; v=par[head[v]]; } } ll distance(ll u,ll v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } // for_each(vertex) // [l, r) <- attention!! template<typename F> void for_each(ll u, ll v, const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); f(max(vid[head[v]],vid[u]),vid[v]+1); if(head[u]!=head[v]) v=par[head[v]]; else break; } } vector<lpair> query(ll u, ll v){ vector<lpair> res; while(1){ if(vid[u]>vid[v]) swap(u,v); res.push_back({max(vid[head[v]],vid[u]),vid[v]+1}); // f(max(vid[head[v]],vid[u]),vid[v]+1); if(head[u]!=head[v]) v=par[head[v]]; else break; } return res; } template<typename T,typename Q,typename F> T for_each(ll u,ll v,T ti,const Q &q,const F &f){ T l=ti,r=ti; while(1){ if(vid[u]>vid[v]){ swap(u,v); swap(l,r); } l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1)); if(head[u]!=head[v]) v=par[head[v]]; else break; } return f(l,r); } // for_each(edge) // [l, r) <- attention!! template<typename F> void for_each_edge(ll u, ll v,const F& f) { while(1){ if(vid[u]>vid[v]) swap(u,v); if(head[u]!=head[v]){ f(vid[head[v]],vid[v]+1); v=par[head[v]]; }else{ if(u!=v) f(vid[u]+1,vid[v]+1); break; } } } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; HLD hld(N); rep(i,0,N-1){ ll u,v; cin >> u >> v; u--; v--; hld.add_edge(u,v); } hld.build(); ll Q; cin >> Q; vector<ll> v(N,0); AddLazySegmentTree sg(v); while(Q--){ ll a,b; cin >> a >> b; a--; b--; auto q = hld.query(a,b); for(auto &e: q){ sg.add(e.first, e.second, 1); } // hld.for_each(a, b, [&](ll v1, ll v2){ // sg.add(v1,v2,1); // return; // }); } ll ans = 0; rep(i,0,N){ ll r = sg.getsum(i,i+1); ans += r * (r+1) / 2; } print(ans); }