結果
問題 |
No.778 クリスマスツリー
|
ユーザー |
![]() |
提出日時 | 2020-03-31 00:14:18 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 134 ms / 2,000 ms |
コード長 | 1,152 bytes |
コンパイル時間 | 5,349 ms |
コンパイル使用メモリ | 201,204 KB |
最終ジャッジ日時 | 2025-01-09 11:12:28 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include<bits/stdc++.h> using lint=long long; using real=long double; int main(){ std::cin.tie(nullptr);std::ios_base::sync_with_stdio(false); std::cout.setf(std::ios_base::fixed);std::cout.precision(15); lint n;std::cin>>n; std::vector<std::vector<lint>>g(n); for(lint i=1;i<n;i++){ lint p;std::cin>>p; g.at(p).push_back(i); } lint D=0; std::vector<lint>ord; auto dfs=[&](auto&&dfs, lint x, lint d)->void{ D+=d; ord.push_back(-x); for(lint y:g.at(x))dfs(dfs,y,d+1); ord.push_back(+x); }; dfs(dfs,0,0); lint inv=0; for(lint d=1;d<2*n;d<<=1){ lint sz=0; std::vector<lint>swp(2*n); for(lint l=0,c,r;l<2*n;l=r){ c=std::min(2*n,l+d); r=std::min(2*n,c+d); for(lint i=l,j=c;i<c||j<r;){ if(j==r||i<c&&ord.at(i)<=ord.at(j)){ swp.at(sz++)=ord.at(i++); }else{ inv+=c-i; swp.at(sz++)=ord.at(j++); } } } swp.swap(ord); } std::cout<<D-(n*(n-1)-inv)/2<<'\n'; }