#include<bits/stdc++.h>
#define F first
#define S second
#define int long long
#define pb push_back
using namespace std;
const int N=2e5+10,M=2e3,mod=1e9+7;
int cost[N],seg[N*4],ans[N];
vector<pair<int,int>>adj[N];
map<pair<int,int>,int>mp;
void dfs(int node,int par){
for(auto x:adj[node]){
if(x.F==par)
continue;
ans[x.F]=ans[node]+cost[node]-mp[{node,x.F}];
if(ans[x.F]<0)
continue;
dfs(x.F,node);
}
}
void build(int v,int tl,int tr){
if(tl==tr){
seg[v]=ans[tl];
return ;
}
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
seg[v]=min(seg[v*2],seg[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tl>r||tr<l){
return 1e18;
}
if(tl>=l&&tr<=r)
return seg[v];
int tm=(tl+tr)/2;
return min(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
int n;
bool ok(int mid,int val,int start){
if(start+mid-1>n)
return 0;
int ans1=get(1,1,n,start,start+mid-1);
return ans1>=val;
}
main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>cost[i];
for(int i=0;i<n-1;i++){
int x,y,z;
cin>>x>>y>>z;
adj[x].pb({y,z});
adj[y].pb({x,z});
mp[{x,y}]=z;
mp[{y,x}]=z;
}
if(n<=5000){
int anss=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
ans[j]=-1e18;
ans[i]=0;
dfs(i,0);
for(int j=1;j<=n;j++)
if(ans[j]!=1e18&&ans[j]>=0&&i!=j)
anss++;
}
cout<<anss<<endl;
}
else{
ans[1]=0;
for(int i=1;i<=n;i++){
ans[i+1]=ans[i]+cost[i]-mp[{i,i+1}];
}
build(1,1,n);
int anss=0;
for(int i=1;i<=n;i++){
int val=ans[i];
int l=0,r=n+1;
while(l<r){
int mid=(l+r+1)/2;
if(ok(mid,val,i))
l=mid;
else
r=mid-1;
}
anss+=l-1;
}
reverse(cost+1,cost+n+1);
ans[1]=0;
for(int i=1;i<=n;i++){
ans[i+1]=ans[i]+cost[i]-mp[{n-i+1,n-i}];
}
build(1,1,n);
for(int i=1;i<=n;i++){
int val=ans[i];
int l=0,r=n+1;
while(l<r){
int mid=(l+r+1)/2;
if(ok(mid,val,i))
l=mid;
else
r=mid-1;
}
anss+=l-1;
}
cout<<anss<<endl;
}
return 0;
}
Compilation message
transport.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
5588 KB |
Output is correct |
2 |
Correct |
118 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
506 ms |
6228 KB |
Output is correct |
2 |
Correct |
69 ms |
5956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
74 ms |
14040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
102 ms |
17152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
142 ms |
22188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
71 ms |
14008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
143 ms |
20332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
175 ms |
25332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
243 ms |
31760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
327 ms |
38020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |