結果

問題 No.778 クリスマスツリー
ユーザー hornistyfhornistyf
提出日時 2020-01-13 12:05:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,337 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 93,504 KB
実行使用メモリ 31,260 KB
最終ジャッジ日時 2023-08-22 12:56:34
合計ジャッジ時間 3,310 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,716 KB
testcase_01 AC 3 ms
9,580 KB
testcase_02 AC 4 ms
9,688 KB
testcase_03 AC 3 ms
9,756 KB
testcase_04 AC 3 ms
9,724 KB
testcase_05 AC 3 ms
9,852 KB
testcase_06 AC 94 ms
31,032 KB
testcase_07 AC 38 ms
13,188 KB
testcase_08 WA -
testcase_09 AC 114 ms
15,700 KB
testcase_10 AC 114 ms
15,844 KB
testcase_11 AC 115 ms
15,600 KB
testcase_12 AC 106 ms
15,568 KB
testcase_13 AC 77 ms
15,480 KB
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
int bit[500010];

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(){
	int 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);

	int 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