結果

問題 No.778 クリスマスツリー
ユーザー leaf_1415leaf_1415
提出日時 2019-01-11 03:47:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 806 bytes
コンパイル時間 892 ms
コンパイル使用メモリ 64,092 KB
実行使用メモリ 25,048 KB
最終ジャッジ日時 2024-04-22 03:05:43
合計ジャッジ時間 1,977 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,340 KB
testcase_01 AC 5 ms
9,408 KB
testcase_02 AC 4 ms
9,400 KB
testcase_03 AC 5 ms
9,556 KB
testcase_04 AC 4 ms
9,340 KB
testcase_05 AC 4 ms
9,400 KB
testcase_06 AC 83 ms
24,936 KB
testcase_07 AC 38 ms
10,684 KB
testcase_08 AC 108 ms
17,156 KB
testcase_09 AC 100 ms
12,632 KB
testcase_10 AC 101 ms
12,768 KB
testcase_11 AC 97 ms
12,660 KB
testcase_12 AC 99 ms
12,600 KB
testcase_13 AC 71 ms
12,428 KB
testcase_14 AC 81 ms
25,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#define llint long long

using namespace std;

struct BIT{
	int size;
	vector<llint> bit;
	BIT(){size = 0;}
	BIT(int s){
		size = s;
		bit.resize(size+1);
		init();
	}
	void init(){
		for(int i = 1; i <= size; i++) bit[i] = 0;
	}
	llint query(int i){
		int ret = 0;
		while(i > 0){
			ret += bit[i];
			i -= i&(-i);
		}
		return ret;
	}
	void add(int i, llint x){
		while(i <= size){
			bit[i] += x;
			i += i&(-i);
		}
	}
};

llint n;
vector<int> G[200005];
BIT bit(200005);

llint ans;
void dfs(int v)
{
	ans += bit.query(v);
	bit.add(v+1, 1);
	for(int i = 0; i < G[v].size(); i++) dfs(G[v][i]);
	bit.add(v+1, -1);
}

int main(void)
{
	cin >> n;
	int p;
	for(int i = 1; i < n; i++){
		cin >> p;
		G[p].push_back(i);
	}
	
	dfs(0);
	cout << ans << endl;
	
	return 0;
}
0