結果

問題 No.778 クリスマスツリー
ユーザー nanophoto12nanophoto12
提出日時 2021-05-23 15:07:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 2,912 bytes
コンパイル時間 4,265 ms
コンパイル使用メモリ 259,984 KB
実行使用メモリ 34,560 KB
最終ジャッジ日時 2024-04-20 00:59:12
合計ジャッジ時間 6,231 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 97 ms
34,560 KB
testcase_07 AC 45 ms
10,396 KB
testcase_08 AC 118 ms
22,016 KB
testcase_09 AC 103 ms
12,800 KB
testcase_10 AC 101 ms
12,672 KB
testcase_11 AC 105 ms
12,672 KB
testcase_12 AC 97 ms
12,664 KB
testcase_13 AC 75 ms
12,544 KB
testcase_14 AC 95 ms
34,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

template<typename T>
void chmax(T& reference, T value) {
    reference = max(reference, value);
}

template<typename T>
void chmaxmap(map<T, T>& m, T key, T value) {
    if (m.count(key)) {
        chmax(m[key], value);
    }
    else {
        m[key] = value;
    }
}

template<typename T>
void chmin(T& reference, T value) {
    reference = min(reference, value);
}


#include <atcoder/all>

using namespace atcoder;

typedef modint1000000007 mint;

struct Point {
    ll x, y;

    bool operator <(const Point& p) const {
        return x < p.x || (x == p.x && y < p.y);
    }
};

struct ConvexHullRunner {
    typedef ll coord_t;
    typedef ll coord2_t;

    coord2_t cross(const Point& O, const Point& A, const Point& B)
    {
        return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x);
    }

    vector<Point> convex_hull(vector<Point> P)
    {
        size_t n = P.size(), k = 0;
        if (n <= 3) return P;
        vector<Point> H(2 * n);

        sort(P.begin(), P.end());

        for (size_t i = 0; i < n; ++i) {
            while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--;
            H[k++] = P[i];
        }

        for (size_t i = n - 1, t = k + 1; i > 0; --i) {
            while (k >= t && cross(H[k - 2], H[k - 1], P[i - 1]) <= 0) k--;
            H[k++] = P[i - 1];
        }

        H.resize(k - 1);
        return H;
    }
};

ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); }

static ll raw_cmb(ll n, ll k) {
    ll c = 1;
    for (int i = 1; i <= k; i++) {
        c *= (n + 1 - i);
        c /= i;
    }
    return c;
}

struct BIT {
    typedef ll input_t;

    vector<input_t> bit_;
    const long long n_;

    BIT(long long n) :n_(n) {
        bit_.resize(n + 1LL, 0);
    }

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

    void add(int i, input_t x) {
        while (i <= n_) {
            bit_[i] += x;
            i += (i & (-i));
        }
    }
};


int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n + 1);
    for (int i = 1; i < n;i++) {
        ll p;
        cin >> p;
        g[p + 1].push_back(i + 1);
    }
    BIT bit(n);
    ll sum = 0;
    function<void(ll, ll)> dfs = [&](ll current, ll parent) {
        sum += bit.sum(current);
        bit.add(current, 1);
        for (auto x : g[current]) {
            if (x == parent) continue;
            dfs(x, current);
        }
        bit.add(current, -1);
    };
    dfs(1, -1);
    cout << sum << endl;
    return 0;
}
0