結果

問題 No.778 クリスマスツリー
ユーザー peroonperoon
提出日時 2019-06-26 00:27:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,477 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 168,784 KB
実行使用メモリ 28,192 KB
最終ジャッジ日時 2024-04-22 03:08:25
合計ジャッジ時間 2,755 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,304 KB
testcase_01 AC 5 ms
9,592 KB
testcase_02 AC 4 ms
9,564 KB
testcase_03 AC 5 ms
9,512 KB
testcase_04 AC 6 ms
9,300 KB
testcase_05 AC 4 ms
9,464 KB
testcase_06 AC 54 ms
28,116 KB
testcase_07 AC 24 ms
11,728 KB
testcase_08 AC 76 ms
18,984 KB
testcase_09 AC 76 ms
13,060 KB
testcase_10 AC 66 ms
13,224 KB
testcase_11 AC 72 ms
13,188 KB
testcase_12 AC 71 ms
13,028 KB
testcase_13 AC 39 ms
12,564 KB
testcase_14 AC 51 ms
28,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);

    // input
    ll N;
    cin >> N;

    FOR(i, 1, N){
        ll parent; cin >> parent;
        G[parent].push_back(i);
    }

    dfs(0);
    p(cnt);
    
    return 0;
}
0