forked from 21171-somesh/CodeforcesSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path739B.cpp
More file actions
127 lines (116 loc) · 2.18 KB
/
739B.cpp
File metadata and controls
127 lines (116 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//21171_somesh || asomesh999
//AC
#include<bits/stdc++.h>
using namespace std;
#define fr(i,a,b) for(long long i=a;i<b;i++)
#define io ios::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
long int mod=1000000007;
template<typename T>
void debug(T arr) {
for(auto it : arr) {
cout << it << ' ';
}
cout << '\n';
}
int n, l;
int timer;
vl cost, dist, tin, tout, height;
vl dp;
vector<vector<pll>> edges;
vector<vi> up;
void dfs(int pos, int par, int w) {
if(pos != par) {
height[pos] = height[par] + 1;
}
dist[pos] = dist[par] + w;
tin[pos] = ++timer;
up[pos][0] = par;
fr(i, 1, l + 1)
up[pos][i] = up[up[pos][i - 1]][i - 1];
for(auto it : edges[pos]) {
if(par != it.first) {
dfs(it.first, pos, it.second);
}
}
tout[pos] = ++timer;
}
void dfs1(int pos, int par) {
for(auto it : edges[pos]) {
if(par != it.first) {
dfs1(it.first, pos);
dp[pos] += dp[it.first];
}
}
}
bool check(int pos, int par) {
return (dist[pos] - dist[par]) <= cost[pos];
}
void func() {
fr(i, 1, n) {
int low = 0, high = height[i] + 1;
int ans = -1;
while(low <= high) {
int mid = (low + high) / 2;
int pos = i;
for(int j = l + 1; j >= 0; j--) {
if(mid & (1LL << j)) {
pos = up[pos][j];
}
}
if(check(i, pos)) {
low = mid + 1;
ans = pos;
} else {
high = mid - 1;
}
}
if(ans != -1) {
dp[up[i][0]] += 1;
if(ans != 0) {
dp[up[ans][0]] -= 1;
}
}
}
}
int main()
{
io
#ifdef SOMU
clock_t startTime = clock();
freopen("input.txt","r",stdin);
#endif
cin >> n;
l = ceil(log2(n));
dp.resize(n);
tin.resize(n);
tout.resize(n);
cost.resize(n);
dist.resize(n);
edges.resize(n);
height.resize(n);
up.resize(n, vi(l + 2));
fr(i, 0, n) {
cin >> cost[i];
}
fr(i, 0, n - 1) {
int p, w;
cin >> p >> w;
p--;
edges[i + 1].push_back({p, w});
edges[p].push_back({i + 1, w});
}
dfs(0, 0, 0);
func();
dfs1(0, 0);
debug(dp);
#ifdef SOMU
cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
#endif
return 0;
}