結果
| 問題 |
No.778 クリスマスツリー
|
| コンテスト | |
| ユーザー |
hornistyf
|
| 提出日時 | 2020-01-13 12:09:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 121 ms / 2,000 ms |
| コード長 | 1,335 bytes |
| コンパイル時間 | 887 ms |
| コンパイル使用メモリ | 94,608 KB |
| 実行使用メモリ | 36,800 KB |
| 最終ジャッジ日時 | 2024-10-14 02:00:51 |
| 合計ジャッジ時間 | 2,455 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
//yuki778.cpp
//Mon Jan 13 11:11:32 2020
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;
vector<int> graph[200010];
vector<int> tour;
int l[200010];
int r[200010];
ll bit[1000000];
void add(int x, int y){
while(x<=tour.size()+1){
bit[x] += y;
x += x & -x;
}
}
int sum(int i){
int ans = 0;
while (i>0){
ans += bit[i];
i -= i & -i;
}
return ans;
}
void dfs(int x){
tour.push_back(x);
if (l[x]==-1){
l[x]=tour.size()-1;
}else{
r[x]=tour.size()-1;
}
for (int i=0;i<graph[x].size();i++){
dfs(graph[x][i]);
tour.push_back(x);
r[x]=tour.size()-1;
}
}
int main(){
ll n;
cin >> n;
for (int i=1;i<n;i++){
int temp;
cin >> temp;
graph[temp].push_back(i);
}
fill(l,l+200010,-1);
fill(r,r+200010,-1);
dfs(0);
fill(bit,bit+tour.size()+1,0);
ll ans = 0;
for (int i=n-1;i>=0;i--){
if (r[i]!=-1){
ans += sum(r[i]+1)-sum(l[i]);
}
add(l[i]+1,1);
}
// for (int i=0;i<tour.size();i++){
// cout << tour[i] << " ";
// }
// printf("\n");
// for (int i=0;i<n;i++){
// cout << i << " " << l[i] << " " << r[i] << endl;
// }
cout << ans << endl;
}
hornistyf