結果

問題 No.778 クリスマスツリー
ユーザー peroonperoon
提出日時 2019-06-26 01:33:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 2,211 bytes
コンパイル時間 1,938 ms
コンパイル使用メモリ 175,928 KB
実行使用メモリ 28,540 KB
最終ジャッジ日時 2024-04-22 03:08:42
合計ジャッジ時間 3,664 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,400 KB
testcase_01 AC 3 ms
6,528 KB
testcase_02 AC 4 ms
6,528 KB
testcase_03 AC 4 ms
6,400 KB
testcase_04 AC 4 ms
6,400 KB
testcase_05 AC 4 ms
6,528 KB
testcase_06 AC 67 ms
28,540 KB
testcase_07 AC 48 ms
23,532 KB
testcase_08 AC 152 ms
27,128 KB
testcase_09 AC 132 ms
22,332 KB
testcase_10 AC 130 ms
22,120 KB
testcase_11 AC 132 ms
22,332 KB
testcase_12 AC 128 ms
22,204 KB
testcase_13 AC 63 ms
22,544 KB
testcase_14 AC 66 ms
28,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using VV = vector<vector<ll> >;

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;

VV G;

struct EulerTour{
    // オイラーツアー
    vector<int> tour;
    // 各頂点番号に対する、tourの最左/最右の位置
    vector<int> L, R;

    EulerTour(){}
    EulerTour(int N){
       initialize(N); 
    }

    void initialize(int N){
        L.resize(N);
        R.resize(N);
        dfs(0, -1);
    }

    void dfs(int i, int prev){
        L[i] = tour.size();
        tour.push_back(i);
        for(int j : G[i]){
          if(j==prev) continue;
          dfs(j, i);
          tour.push_back(i);
        }
        R[i] = tour.size()-1;
    }

    void print(){
      for(ll a : tour){
        cout << a << ' ';
      }
      cout << endl;
    }
};

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

  T range_sum(int left, int right){
    return sum(right) - sum(left-1);
  }
};

auto bit = BinaryIndexedTree<ll>(400010);
ll cnt = 0;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    // input
    ll N;
    cin >> N;
    G.resize(N);

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

    auto et = EulerTour(N);
    // et.print();

    for(int i=N-1; i>=0; i--){
      ll left = et.L[i];
      ll right = et.R[i];
      cnt += bit.range_sum(left, right);
      bit.add(left, 1);
    }

    p(cnt);

    return 0;
}
0