結果

問題 No.778 クリスマスツリー
ユーザー eQeeQe
提出日時 2019-06-14 20:52:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,216 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 161,396 KB
実行使用メモリ 31,360 KB
最終ジャッジ日時 2024-04-26 06:09:38
合計ジャッジ時間 2,951 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,472 KB
testcase_01 AC 5 ms
9,404 KB
testcase_02 AC 4 ms
9,472 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 92 ms
31,160 KB
testcase_07 AC 43 ms
11,572 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=1e5+7;
static const ll MOD=1e9+7;
ll max(ll a, ll b) {return a>b?a:b;}
ll min(ll a, ll b) {return a<b?a:b;}

class BIT {
public:
  vector<ll> bit;
  ll M;
  
  BIT(ll M): bit(vector<ll>(M+1, 0)), M(M) {}
  
  ll sum(ll i) {
    ll s=0;
    while(i>0) {
      s+=bit[i];
      i-=i&-i;
    }
    return s;
  }
  
  void add(ll i, ll x) {
    while(i<=M) {
      bit[i]+=x;
      i+=i&-i;
    }
  }
};

BIT bit(MAX*2);
vector<ll> g[MAX*2];
ll dfs(ll u, ll p) {
  ll i, v;
  ll res=0;
  for(i=0; i<g[u].size(); i++) {
    v=g[u][i];
    if(v==p) continue;
    
    res+=bit.sum(v);
    bit.add(v, 1);
    dfs(v, u);
    bit.add(v, -1);
  }
  return res;
}

int main(void) {
  ll N;
  cin >> N;
  ll i;
  
  
  for(i=1; i<=N-1; i++) {
    ll a;
    cin >> a;
    g[a].pb(i);
  }
  
  ll ans=dfs(0, -1);
  pt(ans+N-1);
}
  








0