This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;
void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}
const int maxn = 100050;
int n, a[maxn], nxt[maxn], l[maxn], r[maxn];
vector<pii> adj[maxn];
ll ans;
void explore(int u, int fa, int cur)
{
if(cur < 0) return;
if(fa) ++ ans;
for(auto p : adj[u])
if(p.fi != fa)
explore(p.fi, u, cur + a[u] - p.se);
}
int main()
{
#ifdef MPS
fp("1.in","r",stdin);
fp("1.out","w",stdout);
#endif
sf("%d",&n);
fo(i,1,n) sf("%d",&a[i]);
bool chk = true;
fo(i,2,n)
{
int u, v, w; sf("%d%d%d",&u,&v,&w);
if(u > v) swap(u, v);
chk &= (abs(u-v)==1);
nxt[u] = w;
adj[u].pb(mp(v,w)); adj[v].pb(mp(u,w));
}
if(n <= 5000)
{
fo(i,1,n) explore(i,0,0);
pf("%lld\n",ans);
}
else if(chk)
{
fo(i,1,n) l[i] = r[i] = i;
int cur = 0;
fo(i,1,n)
{
if(cur + a[i] - nxt[i] >= 0)
{
cur = cur + a[i] - nxt[i];
l[i+1] = l[i];
}
else cur = 0;
}
cur = 0;
fd(i,n-1,1)
{
if(cur + a[i+1] - nxt[i] >= 0)
{
cur = cur + a[i+1] - nxt[i];
r[i] = r[i+1];
}else cur = 0;
}
fo(i,1,n) ans += (r[i] - l[i]);
pf("%lld\n",ans);
}
return 0;
}
Compilation message (stderr)
transport.cpp: In function 'int main()':
transport.cpp:53:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
sf("%d",&n);
^
transport.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fo(i,1,n) sf("%d",&a[i]);
^
transport.cpp:58:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v, w; sf("%d%d%d",&u,&v,&w);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |