結果
| 問題 |
No.778 クリスマスツリー
|
| コンテスト | |
| ユーザー |
peroon
|
| 提出日時 | 2019-06-26 01:33:01 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 102 ms / 2,000 ms |
| コード長 | 2,211 bytes |
| コンパイル時間 | 1,738 ms |
| コンパイル使用メモリ | 178,752 KB |
| 実行使用メモリ | 28,548 KB |
| 最終ジャッジ日時 | 2024-10-14 01:59:50 |
| 合計ジャッジ時間 | 3,164 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#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;
}
peroon