結果
問題 | No.778 クリスマスツリー |
ユーザー |
![]() |
提出日時 | 2019-06-26 00:27:01 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 71 ms / 2,000 ms |
コード長 | 1,477 bytes |
コンパイル時間 | 1,607 ms |
コンパイル使用メモリ | 170,304 KB |
実行使用メモリ | 28,288 KB |
最終ジャッジ日時 | 2024-10-14 01:59:34 |
合計ジャッジ時間 | 2,862 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include<bits/stdc++.h>using namespace std;using ll = long long;template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }#define FOR(i,a,b) for(ll i=(a);i<(b);++i)#define ALL(v) (v).begin(), (v).end()#define p(s) cout<<(s)<<endl#define p2(s, t) cout << (s) << " " << (t) << endl#define br() p("")#define pn(s) cout << (#s) << " " << (s) << endl#define p_yes() p("Yes")#define p_no() p("No")const ll mod = 1e9 + 7;const ll inf = 1e18;template< typename T >struct BinaryIndexedTree {vector< T > data;BinaryIndexedTree(int sz) {data.assign(++sz, 0);}T sum(int k) {T ret = 0;for(++k; k > 0; k -= k & -k) ret += data[k];return (ret);}void add(int k, T x) {for(++k; k < data.size(); k += k & -k) data[k] += x;}};auto bit = BinaryIndexedTree<ll>(200010);ll cnt = 0;vector<ll> G[200010];void dfs(ll i){// iより値の小さい先祖cnt += bit.sum(i-1);// 自身を先祖に足すbit.add(i, 1);// 子どもたちfor(ll to : G[i]){dfs(to);}// 自身を先祖一覧から取り除くbit.add(i, -1);}int main(){cin.tie(0);ios::sync_with_stdio(false);// inputll N;cin >> N;FOR(i, 1, N){ll parent; cin >> parent;G[parent].push_back(i);}dfs(0);p(cnt);return 0;}