結果
問題 | No.778 クリスマスツリー |
ユーザー | eQe |
提出日時 | 2019-06-14 20:53:27 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 117 ms / 2,000 ms |
コード長 | 1,221 bytes |
コンパイル時間 | 1,324 ms |
コンパイル使用メモリ | 161,444 KB |
実行使用メモリ | 31,372 KB |
最終ジャッジ日時 | 2024-04-22 03:08:01 |
合計ジャッジ時間 | 2,921 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
9,304 KB |
testcase_01 | AC | 4 ms
9,332 KB |
testcase_02 | AC | 5 ms
9,572 KB |
testcase_03 | AC | 5 ms
9,488 KB |
testcase_04 | AC | 4 ms
9,468 KB |
testcase_05 | AC | 4 ms
9,528 KB |
testcase_06 | AC | 87 ms
31,372 KB |
testcase_07 | AC | 42 ms
11,528 KB |
testcase_08 | AC | 117 ms
20,348 KB |
testcase_09 | AC | 105 ms
13,248 KB |
testcase_10 | AC | 103 ms
13,032 KB |
testcase_11 | AC | 106 ms
13,184 KB |
testcase_12 | AC | 100 ms
13,020 KB |
testcase_13 | AC | 74 ms
12,644 KB |
testcase_14 | AC | 91 ms
31,272 KB |
ソースコード
#include <bits/stdc++.h> #define ft first #define sc second #define lb lower_bound #define ub upper_bound #define pb push_back #define pt(sth) cout << sth << "\n" #define chmax(a, b) {if(a<b) a=b;} #define chmin(a, b) {if(a>b) a=b;} #define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD using namespace std; typedef long long ll; typedef pair<ll, ll> P; static const ll INF=1e18; static const ll MAX=1e5+7; static const ll MOD=1e9+7; ll max(ll a, ll b) {return a>b?a:b;} ll min(ll a, ll b) {return a<b?a:b;} class BIT { public: vector<ll> bit; ll M; BIT(ll M): bit(vector<ll>(M+1, 0)), M(M) {} ll sum(ll i) { ll s=0; while(i>0) { s+=bit[i]; i-=i&-i; } return s; } void add(ll i, ll x) { while(i<=M) { bit[i]+=x; i+=i&-i; } } }; BIT bit(MAX*2); vector<ll> g[MAX*2]; ll dfs(ll u, ll p) { ll i, v; ll res=0; for(i=0; i<g[u].size(); i++) { v=g[u][i]; if(v==p) continue; res+=bit.sum(v); bit.add(v, 1); res+=dfs(v, u); bit.add(v, -1); } return res; } int main(void) { ll N; cin >> N; ll i; for(i=1; i<=N-1; i++) { ll a; cin >> a; g[a].pb(i); } ll ans=dfs(0, -1); pt(ans+N-1); }