結果

問題 No.778 クリスマスツリー
ユーザー utouto97utouto97
提出日時 2019-03-29 12:02:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 1,503 bytes
コンパイル時間 1,328 ms
コンパイル使用メモリ 162,872 KB
実行使用メモリ 21,824 KB
最終ジャッジ日時 2024-04-22 03:07:38
合計ジャッジ時間 2,785 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,080 KB
testcase_01 AC 3 ms
8,120 KB
testcase_02 AC 3 ms
8,108 KB
testcase_03 AC 2 ms
8,060 KB
testcase_04 AC 4 ms
8,056 KB
testcase_05 AC 4 ms
8,028 KB
testcase_06 AC 80 ms
21,824 KB
testcase_07 AC 39 ms
11,168 KB
testcase_08 AC 99 ms
15,652 KB
testcase_09 AC 91 ms
13,220 KB
testcase_10 AC 102 ms
13,196 KB
testcase_11 AC 96 ms
13,108 KB
testcase_12 AC 91 ms
13,224 KB
testcase_13 AC 74 ms
12,540 KB
testcase_14 AC 82 ms
21,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
}*/

template<class T>
class BinaryIndexedTree
{
public:
  int n;
  vector<T> bit;

  BinaryIndexedTree():n(-1){}
  BinaryIndexedTree(int n_):n(n_),bit(n_+1,0){}
  BinaryIndexedTree(int n_, T d):n(n_),bit(n_+1,d){}

  T sum(int i)
  {
    T s = 0;
    while(i > 0){
      s += bit[i];
      i -= i&-i;
    }
    return s;
  }

  void add(int i, T x)
  {
    if(i == 0) return;
    while(i <= n){
      bit[i] += x;
      i += i&-i;
    }
  }

  T sum0(int i)
  {
    return sum(i+1);
  }

  void add0(int i, T x)
  {
    add(i+1, x);
  }

  T query(int l, int r)
  {
    return sum(r-1)-sum(l-1);
  }

  T query0(int l, int r)
  {
    return sum(r)-sum(l);
  }
};

int n;
vi g[200000];
BinaryIndexedTree<int> bit;
int ans;

void dfs(int v)
{
  ans += bit.query0(0, v);
  bit.add0(v, 1);
  for(int to : g[v]){
    dfs(to);
  }
  bit.add0(v, -1);
}

signed main()
{
  cin >> n;
  rep(i, 1, n){
    int a;
    cin >> a;
    g[a].pb(i);
  }

  bit = BinaryIndexedTree<int>(n);
  dfs(0);

  cout << ans << endl;

  return 0;
}
0