結果

問題 No.778 クリスマスツリー
ユーザー first_vilfirst_vil
提出日時 2019-09-11 16:43:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 2,236 bytes
コンパイル時間 1,831 ms
コンパイル使用メモリ 171,420 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2024-04-22 03:09:21
合計ジャッジ時間 2,678 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 48 ms
21,504 KB
testcase_07 AC 23 ms
10,040 KB
testcase_08 AC 67 ms
15,212 KB
testcase_09 AC 70 ms
12,112 KB
testcase_10 AC 64 ms
12,204 KB
testcase_11 AC 61 ms
12,004 KB
testcase_12 AC 57 ms
12,112 KB
testcase_13 AC 37 ms
11,940 KB
testcase_14 AC 46 ms
21,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);(i)++)
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);(i)++)
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);(i)--)
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);(i)--)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; }return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; }return 0; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }

template<class V> class BIT {
    int n = 0; vector<V> bit;
#define def 0
public:
    BIT() {}  // [L, R) 半開区間・0-indexed
    BIT(int _n) { init(_n); }
    void init(int _n) { n = 1; while (n < _n)n *= 2; bit.resize(n, def); }
    V operator[](int e) { V s = 0; e++; while (e) s += bit[e - 1], e -= e & -e; return s; }
    void add(int e, V v) { e++; while (e <= n) bit[e - 1] += v, e += e & -e; }
    int lower_bound(V val) {
        V tv = 0; int i, ent = 0; for (i = n - 1; i >= 0; i--)
            if (tv + bit[ent + (1 << i) - 1] <= val)tv += bit[ent + (1 << i) - 1], ent += (1 << i); return ent;
    }
    V sum(int L, int R) {
        assert(0 <= L); assert(R <= n); assert(L <= R);
        V res = 0; if (R) res += operator[](R - 1); if (L) res -= operator[](L - 1); return res;
    }
    V sum(int R) { return sum(0, R); }
    void clear() { bit.clear(); }
    void resize(int _n) { bit.clear(); init(_n); }
    int size() { return n; }
};

BIT<int> bit;
vector<VI> edge;
ll ans;
void dfs(int i) {
    ans += bit.sum(i);
    bit.add(i, 1);
    for (int j : edge[i])dfs(j);
    bit.add(i, -1);
}

int main() {
    init();

    int n;
    cin >> n;
    edge.resize(n);
    bit.resize(n);
    int in;
    FOR(i, 1, n) {
        cin >> in;
        edge[in].push_back(i);
    }
    dfs(0);
    cout << ans << "\n";
}
0