結果

問題 No.778 クリスマスツリー
ユーザー eQeeQe
提出日時 2019-06-14 20:48:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,195 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 161,508 KB
実行使用メモリ 31,348 KB
最終ジャッジ日時 2024-04-22 03:08:21
合計ジャッジ時間 3,192 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,448 KB
testcase_01 AC 6 ms
9,372 KB
testcase_02 AC 6 ms
9,560 KB
testcase_03 AC 5 ms
9,356 KB
testcase_04 AC 5 ms
9,280 KB
testcase_05 AC 6 ms
9,312 KB
testcase_06 AC 100 ms
31,280 KB
testcase_07 AC 45 ms
11,572 KB
testcase_08 AC 153 ms
20,236 KB
testcase_09 AC 146 ms
13,072 KB
testcase_10 AC 142 ms
13,084 KB
testcase_11 AC 142 ms
13,212 KB
testcase_12 AC 136 ms
13,172 KB
testcase_13 AC 81 ms
12,432 KB
testcase_14 AC 100 ms
31,348 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    }
  }
};

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

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








0