結果
問題 | No.778 クリスマスツリー |
ユーザー |
![]() |
提出日時 | 2018-12-25 00:45:25 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 62 ms / 2,000 ms |
コード長 | 1,210 bytes |
コンパイル時間 | 1,446 ms |
コンパイル使用メモリ | 170,308 KB |
実行使用メモリ | 21,184 KB |
最終ジャッジ日時 | 2024-10-14 01:53:17 |
合計ジャッジ時間 | 2,985 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> #define fst(t) std::get<0>(t) #define snd(t) std::get<1>(t) #define thd(t) std::get<2>(t) #define unless(p) if(!(p)) #define until(p) while(!(p)) using ll = long long; using P = std::tuple<int,int>; const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; struct FenwickTree{ int numNode; std::vector<int> data; FenwickTree(int n) : numNode(n), data(n + 1, 0) {} void add(int k, int v){ k += 1; while(k <= numNode){ data[k] += v; k += k & -k; } } int sum(int k){ k += 1; int res = 0; while(k > 0){ res += data[k]; k -= k & -k; } return res; } }; int N; std::vector<int> G[200100]; ll cnt; FenwickTree ft(200100); void dfs(int v){ cnt += ft.sum(v); ft.add(v, +1); for(int w : G[v]){ dfs(w); } ft.add(v, -1); } int main(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cin >> N; for(int i=1;i<=N-1;++i){ int p; std::cin >> p; G[p].emplace_back(i); } dfs(0); std::cout << cnt << std::endl; }