Submission #302248

# Submission time Handle Problem Language Result Execution time Memory
302248 2020-09-18T14:46:33 Z HynDuf Transport (COCI19_transport) C++11
130 / 130
600 ms 15344 KB
#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 mncur, ll cur, int add) // from root -> down
{
    if (add != 0)
    {
        int pos = lower_bound(all(allPath), mncur) - 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(mncur, cur + f[u] - v.S), 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, 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, 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, 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, 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
1 Correct 10 ms 2944 KB Output is correct
2 Correct 9 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 3072 KB Output is correct
2 Correct 17 ms 3328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 7156 KB Output is correct
2 Correct 146 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 220 ms 8740 KB Output is correct
2 Correct 263 ms 11508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 354 ms 10868 KB Output is correct
2 Correct 407 ms 15344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 4352 KB Output is correct
2 Correct 62 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 5620 KB Output is correct
2 Correct 206 ms 6388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 276 ms 6128 KB Output is correct
2 Correct 305 ms 8048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 412 ms 7280 KB Output is correct
2 Correct 431 ms 9460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 600 ms 8684 KB Output is correct
2 Correct 538 ms 10548 KB Output is correct