結果
問題 | No.778 クリスマスツリー |
ユーザー |
|
提出日時 | 2024-11-30 13:33:31 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 198 ms / 2,000 ms |
コード長 | 1,389 bytes |
コンパイル時間 | 3,409 ms |
コンパイル使用メモリ | 253,516 KB |
実行使用メモリ | 40,088 KB |
最終ジャッジ日時 | 2024-11-30 13:33:37 |
合計ジャッジ時間 | 5,325 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#ifndef LOCAL#include <bits/stdc++.h>using namespace std;#define debug(...) (void(0))#else#include "algo/debug.h"#endif#include <atcoder/segtree>using S = int64_t;S op(S a, S b) { return a + b; }S e() { return 0; }namespace std {template <class Fun>class y_combinator_result {Fun fun_;public:template <class T>explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}template <class... Args>decltype(auto) operator()(Args &&...args) {return fun_(std::ref(*this), std::forward<Args>(args)...);}};template <class Fun>decltype(auto) y_combinator(Fun &&fun) {return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));}} // namespace stdvoid solve() {int N; cin >> N;vector<vector<int>> G(N);for(int i = 1; i < N; i++) {int a; cin >> a;G[a].push_back(i);G[i].push_back(a);}atcoder::segtree<S, op, e> seg(N);S ans = 0;y_combinator([&](auto self, int v, int p = -1) -> void {seg.set(v, 1);for(const auto&to: G[v]) if(to != p) {self(to, v);}ans += seg.prod(0, v);seg.set(v, 0);})(0);cout << ans << endl;}int main() {std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int tt = 1;// std::cin >> tt;while(tt--) {solve();}}