結果
問題 | No.778 クリスマスツリー |
ユーザー |
![]() |
提出日時 | 2019-01-01 23:56:07 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 129 ms / 2,000 ms |
コード長 | 814 bytes |
コンパイル時間 | 577 ms |
コンパイル使用メモリ | 70,528 KB |
実行使用メモリ | 24,460 KB |
最終ジャッジ日時 | 2024-10-14 01:55:22 |
合計ジャッジ時間 | 2,238 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <iostream> #include <vector> using namespace std; class BIT { const int size; vector<int> v; public: BIT(int n) : size(n), v(n + 1) {} void add(int pos, int val) { ++pos; while (pos <= size) { v[pos] += val; pos += pos & (-pos); } } int query(int right) { int sum = 0; int pos = right; while (pos > 0) { sum += v[pos]; pos -= pos & (-pos); } return sum; } }; int n; vector<int> G[200000]; BIT* bit; long DFS(int from) { long sum = 0; sum += bit->query(from); bit->add(from, 1); for (int to : G[from]) { sum += DFS(to); } bit->add(from, -1); return sum; } int main() { cin >> n; bit = new BIT(n); for (int i = 1; i < n; ++i) { int p; cin >> p; G[p].push_back(i); } cout << DFS(0) << endl; }