#include<bits/stdc++.h>
using namespace std;
#define int long long
using i64 = long long;
const int maxn = 2e5;
std::vector<pair<int,i64>> graph[maxn+2];
int a[maxn+2] , n;
i64 BIT[maxn+2];
void upd(int pos , int val)
{
for (; pos <= maxn; pos += pos&-pos)
BIT[pos] += val;
return;
}
i64 get(int id)
{
i64 sum = 0;
for (; id ; id -= id&-id)
sum += BIT[id];
return sum;
}
i64 sum_range(int l , int r)
{
return get(r) - get(l);
}
int sub[maxn+2] ;
bool mark[maxn+2];
std::vector<pair<i64,int>> cost;
std::vector<i64> down;
int id[maxn+2];
i64 answer = 0;
int get_centroid(int v , int p , int half)
{
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (mark[to] || to == p) continue;
if (sub[to] > half) return get_centroid(to,v,half);
}
return v;
}
void dfssize(int v , int p)
{
sub[v]=1;
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (to==p||mark[to]) continue;
dfssize(to,v);
sub[v]+=sub[to];
}
return;
}
void downforces(int v , int p , i64 mn , i64 used)
{
down.push_back(mn);
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (mark[to] || to == p) continue;
downforces(to , v , min({mn , used + a[v] - w }) , used + a[v] - w);
}
return;
}
void build(int v , int p , i64 sum , i64 mncost)
{
if (mncost >= 0)
cost.push_back({sum , v});
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (to==p || mark[to]) continue;
build(to , v , sum + a[to] - w , min(mncost + a[to] - w , a[to] - w));
}
return;
}
void centroid(int v)
{
dfssize(v,v);
v = get_centroid(v,v,sub[v]/2);
mark[v] = true;
// DP
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (mark[to]) continue;
build(to , v , a[to] - w , a[to] - w);
}
vector<i64> allcost ;
std::sort(cost.begin() , cost.end());
for (int i = 0; i < cost.size(); ++i)
{
id[cost[i].second] = i + 1;
upd(i+1 , 1);
allcost.push_back(cost[i].first);
++answer;
}
cost.clear();
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (mark[to]) continue;
// cout << "VERTEX IS : " << to << '\n';
build(to , v , a[to] - w , a[to] - w);
for (auto& yy: cost)
{
int y = yy.second;
upd(id[y] , -1);
}
downforces(to , v , a[v] - w , a[v] - w);
for (auto& x : down)
{
if (x >= 0) ++answer;
int pos = lower_bound(allcost.begin() , allcost.end() , -x) - allcost.begin() ;
answer += sum_range(pos , allcost.size());
}
down.clear();
for (auto& yy: cost)
{
int y = yy.second;
upd(id[y] , 1);
}
cost.clear();
}
for (int i = 0; i < allcost.size(); ++i) upd(i + 1 , -1);
for (auto& xx : graph[v])
{
int to = xx.first , w = xx.second;
if (!mark[to]) centroid(to);
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n ;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i)
{
int u , v , w; cin >> u >> v >> w;
graph[u].push_back({v , w});
graph[v].push_back({u , w});
}
centroid(1);
cout << answer;
}
Compilation message
transport.cpp: In function 'long long int get_centroid(long long int, long long int, long long int)':
transport.cpp:40:24: warning: unused variable 'w' [-Wunused-variable]
40 | int to = xx.first , w = xx.second;
| ^
transport.cpp: In function 'void dfssize(long long int, long long int)':
transport.cpp:52:24: warning: unused variable 'w' [-Wunused-variable]
52 | int to = xx.first , w = xx.second;
| ^
transport.cpp: In function 'void centroid(long long int)':
transport.cpp:101:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | for (int i = 0; i < cost.size(); ++i)
| ~~^~~~~~~~~~~~~
transport.cpp:137:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for (int i = 0; i < allcost.size(); ++i) upd(i + 1 , -1);
| ~~^~~~~~~~~~~~~~~~
transport.cpp:141:24: warning: unused variable 'w' [-Wunused-variable]
141 | int to = xx.first , w = xx.second;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
11612 KB |
Output is correct |
2 |
Correct |
4 ms |
11848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11868 KB |
Output is correct |
2 |
Correct |
5 ms |
12200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
17108 KB |
Output is correct |
2 |
Correct |
39 ms |
17116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
18780 KB |
Output is correct |
2 |
Correct |
63 ms |
20944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
21708 KB |
Output is correct |
2 |
Correct |
95 ms |
25524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
13640 KB |
Output is correct |
2 |
Correct |
20 ms |
13656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
14548 KB |
Output is correct |
2 |
Correct |
57 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
15564 KB |
Output is correct |
2 |
Correct |
69 ms |
16920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
16892 KB |
Output is correct |
2 |
Correct |
96 ms |
18656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
18640 KB |
Output is correct |
2 |
Correct |
138 ms |
19408 KB |
Output is correct |