結果

問題 No.778 クリスマスツリー
ユーザー 👑 jupirojupiro
提出日時 2020-01-16 13:03:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,715 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 120,036 KB
実行使用メモリ 28,368 KB
最終ジャッジ日時 2024-04-22 03:10:03
合計ジャッジ時間 2,500 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 61 ms
28,288 KB
testcase_07 AC 43 ms
17,468 KB
testcase_08 AC 116 ms
24,448 KB
testcase_09 AC 94 ms
17,012 KB
testcase_10 AC 92 ms
17,152 KB
testcase_11 AC 85 ms
17,152 KB
testcase_12 AC 86 ms
17,100 KB
testcase_13 AC 55 ms
17,364 KB
testcase_14 AC 61 ms
28,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class BIT {
private:
	ll N;
	vector<ll> node;
public:
	BIT(ll M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum(ll i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum(ll i, ll j) {
		ll l = sum(i - 1);
		ll r = sum(j);
		return r - l;
	}

	void add(ll i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}
};

ll N;
vector<vector<ll>> g;
ll res;
void dfs(ll cur, ll pre, BIT &bit) {
	res += bit.sum(cur);
	bit.add(cur + 1, 1);
	for (ll next : g[cur]) {
		if (next == pre) continue;
		dfs(next, cur, bit);
	}
	bit.add(cur + 1, -1);
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%lld", &N);
	g.resize(N);
	BIT bit(N);
	for (ll i = 1; i < N; i++) {
		ll a; scanf("%lld", &a);
		g[a].push_back(i);
		g[i].push_back(a);
	}
	dfs(0, -1, bit);
	cout << res << endl;
	return 0;
}
0