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>
#define task "T"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, f[N], sz[N];
vii g[N];
ll ans = 0;
vi bit;
void upd(int p, int v)
{
for (; p < SZ(bit); p += p & (-p)) bit[p] += v;
}
int query(int p)
{
if (p >= SZ(bit)) return 0;
int res = 0;
for (; p; p -= p & (-p)) res += bit[p];
return res;
}
bool in[N];
int getsz(int u, int p)
{
sz[u] = 1;
for (ii v : g[u]) if (v.F != p && !in[v.F]) sz[u] += getsz(v.F, u);
return sz[u];
}
int fcen(int u, int p, int nn)
{
for (ii v : g[u]) if (v.F != p && !in[v.F] && sz[v.F] > nn / 2) return fcen(v.F, u, nn);
return u;
}
vl allPath;
void calAllPath(int u, int p, ll cur, int add) // from root -> down
{
if (add != 0)
{
int pos = lower_bound(all(allPath), cur) - allPath.begin() + 1;
upd(1, add);
upd(pos + 1, -add);
}
else allPath.eb(cur);
for (ii v : g[u]) if (v.F != p && !in[v.F]) calAllPath(v.F, u, min(cur, cur + f[u] - v.S), add);
}
void calAns(int u, int p, ll mnCur, ll cur) // curUp : node -> root
{
if (mnCur >= 0)
{
int pos = lower_bound(all(allPath), -cur) - allPath.begin() + 1;
ans += query(pos);
}
for (ii v : g[u]) if (v.F != p && !in[v.F]) calAns(v.F, u, min(mnCur + f[v.F] - v.S, 1LL * f[v.F] - v.S), cur + f[v.F] - v.S);
}
void cd(int u)
{
int nn = getsz(u, 0);
u = fcen(u, 0, nn);
//DB(u);
in[u] = 1;
allPath.clear();
allPath.eb(0);
for (ii v : g[u]) if (!in[v.F]) calAllPath(v.F, u, f[u] - v.S, 0);
sort(all(allPath));
allPath.erase(unique(all(allPath)), allPath.end());
//PR(allPath, 0, SZ(allPath) - 1);
bit.assign(SZ(allPath) + 1, 0);
for (ii v : g[u]) if (!in[v.F]) calAllPath(v.F, u, f[u] - v.S, 1);
int p = lower_bound(all(allPath), 0) - allPath.begin() + 1;
upd(1, 1);
upd(p + 1, -1);
for (ii v : g[u]) if (!in[v.F])
{
calAllPath(v.F, u, f[u] - v.S, -1);
calAns(v.F, u, f[v.F] - v.S, f[v.F] - v.S);
calAllPath(v.F, u, f[u] - v.S, 1);
}
ans += query(p) - 1;
//DB(ans);
for (ii v : g[u]) if (!in[v.F]) cd(v.F);
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n;
rep(i, 1, n) cin >> f[i];
rep(i, 2, n)
{
int u, v, w;
cin >> u >> v >> w;
g[u].eb(v, w);
g[v].eb(u, w);
}
cd(1);
cout << ans;
return 0;
}
# | 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... |