結果

問題 No.778 クリスマスツリー
ユーザー hornistyfhornistyf
提出日時 2020-01-13 12:09:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 1,335 bytes
コンパイル時間 873 ms
コンパイル使用メモリ 95,812 KB
実行使用メモリ 38,840 KB
最終ジャッジ日時 2024-04-22 03:09:37
合計ジャッジ時間 2,226 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
10,660 KB
testcase_01 AC 3 ms
9,796 KB
testcase_02 AC 3 ms
11,308 KB
testcase_03 AC 3 ms
10,804 KB
testcase_04 AC 4 ms
9,800 KB
testcase_05 AC 4 ms
10,180 KB
testcase_06 AC 98 ms
38,840 KB
testcase_07 AC 38 ms
15,584 KB
testcase_08 AC 110 ms
25,732 KB
testcase_09 AC 100 ms
18,168 KB
testcase_10 AC 96 ms
18,292 KB
testcase_11 AC 96 ms
18,160 KB
testcase_12 AC 93 ms
20,208 KB
testcase_13 AC 80 ms
18,180 KB
testcase_14 AC 95 ms
36,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//yuki778.cpp
//Mon Jan 13 11:11:32 2020

#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;

vector<int> graph[200010];
vector<int> tour;
int l[200010];
int r[200010];
ll bit[1000000];

void add(int x, int y){
	while(x<=tour.size()+1){
		bit[x] += y;
		x += x & -x;
	}
}

int sum(int i){
	int ans = 0;
	while (i>0){
		ans += bit[i];
		i -= i & -i;
	}
	return ans;
}

void dfs(int x){
	tour.push_back(x);
	if (l[x]==-1){
		l[x]=tour.size()-1;
	}else{
		r[x]=tour.size()-1;
	}
	for (int i=0;i<graph[x].size();i++){
		dfs(graph[x][i]);
		tour.push_back(x);
		r[x]=tour.size()-1;
	}
}

int main(){
	ll n;
	cin >> n;

	for (int i=1;i<n;i++){
		int temp;
		cin >> temp;
		graph[temp].push_back(i);
	}

	fill(l,l+200010,-1);
	fill(r,r+200010,-1);
	dfs(0);

	fill(bit,bit+tour.size()+1,0);

	ll ans = 0;
	for (int i=n-1;i>=0;i--){
		if (r[i]!=-1){
			ans += sum(r[i]+1)-sum(l[i]);
		}
		add(l[i]+1,1);
	}

//	for (int i=0;i<tour.size();i++){
//		cout << tour[i] << " ";
//	}
//	printf("\n");
//	for (int i=0;i<n;i++){
//		cout << i << " " << l[i] << " " << r[i] << endl;
//	}

	cout << ans << endl;
}
0