結果

問題 No.778 クリスマスツリー
ユーザー chocoruskchocorusk
提出日時 2018-12-25 00:18:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 818 bytes
コンパイル時間 680 ms
コンパイル使用メモリ 97,076 KB
実行使用メモリ 24,448 KB
最終ジャッジ日時 2024-04-22 03:02:40
合計ジャッジ時間 2,224 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,064 KB
testcase_01 AC 4 ms
8,064 KB
testcase_02 AC 4 ms
8,064 KB
testcase_03 AC 4 ms
8,064 KB
testcase_04 AC 4 ms
8,064 KB
testcase_05 AC 3 ms
8,064 KB
testcase_06 AC 88 ms
24,320 KB
testcase_07 AC 41 ms
9,628 KB
testcase_08 AC 103 ms
16,512 KB
testcase_09 AC 106 ms
12,032 KB
testcase_10 AC 105 ms
12,032 KB
testcase_11 AC 112 ms
12,016 KB
testcase_12 AC 101 ms
12,032 KB
testcase_13 AC 75 ms
11,904 KB
testcase_14 AC 87 ms
24,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
int bit[200001], n;
int sum(int i){
	int s=0;
	while(i>0){
		s+=bit[i];
		i-=(i&(-i));
	}
	return s;
}
void add(int i, int x){
	while(i<=n){
		bit[i]+=x;
		i+=(i&(-i));
	}
}
vector<int> g[200001];
ll ans;
void dfs(int x){
	add(x, 1);
	ans+=(ll)sum(x-1);
	for(auto y:g[x]) dfs(y);
	add(x, -1);
}
int main()
{
	cin>>n;
	for(int i=1; i<n; i++){
		int a; cin>>a;
		g[a+1].push_back(i+1);
	}
	dfs(1);
	cout<<ans<<endl;
	return 0;
}
0