結果

問題 No.778 クリスマスツリー
ユーザー ngtkanangtkana
提出日時 2020-03-30 23:34:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 612 ms / 2,000 ms
コード長 906 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 205,040 KB
実行使用メモリ 31,248 KB
最終ジャッジ日時 2023-09-05 11:19:17
合計ジャッジ時間 8,619 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 564 ms
31,248 KB
testcase_07 AC 527 ms
10,952 KB
testcase_08 AC 612 ms
20,404 KB
testcase_09 AC 605 ms
13,000 KB
testcase_10 AC 600 ms
13,000 KB
testcase_11 AC 604 ms
13,068 KB
testcase_12 AC 593 ms
12,972 KB
testcase_13 AC 545 ms
12,436 KB
testcase_14 AC 565 ms
31,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 k=std::sqrt(n);
    std::vector<lint>a(n),table((n+k-1)/k);
    auto add=[&](lint i,lint x){
        a.at(i)+=x;
        table.at(i/k)+=x;
    };
    auto query=[&](lint r){
        lint ans=0;
        while(r%k)ans+=a.at(--r);
        for(r/=k;r;)ans+=table.at(--r);
        return ans;
    };
    lint ans=0;
    auto dfs=[&](auto&&dfs, lint x)->void{
        add(x,1);
        ans+=query(x);
        for(lint y:g.at(x)){
            dfs(dfs,y);
        }
        add(x,-1);
    };
    dfs(dfs,0);
    std::cout<<ans<<'\n';
}

0