結果

問題 No.778 クリスマスツリー
ユーザー eQeeQe
提出日時 2019-06-14 20:41:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,192 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 163,380 KB
実行使用メモリ 31,612 KB
最終ジャッジ日時 2024-04-26 05:48:28
合計ジャッジ時間 3,399 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 RE -
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;}

ll N;
ll a[MAX*2];

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(N);
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) {
  cin >> N;
  ll i;
  
  for(i=1; i<=N-1; i++) {
    cin >> a[i];
    g[a[i]].pb(i);
  }
  
  dfs(0, -1);
  pt(ans);
}
  








0