Submission #943114

# Submission time Handle Problem Language Result Execution time Memory
943114 2024-03-11T08:23:43 Z 12345678 Transport (COCI19_transport) C++17
0 / 130
523 ms 17636 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ll long long
 
const ll nx=1e5+5;
 
ll n, vl[nx], u, v, w, used[nx], sz[nx], ans=0, t;
vector<pair<ll, ll>> d[nx];
map<ll, ll> mp;
 
struct fenwick
{
    ll d[nx];
    void add(ll i, ll vl)
    {
        while (i<nx) d[i]+=vl, i+=(i&-i);
    }
    ll query(ll i)
    {
        ll res=0;
        while (i>0) res+=d[i], i-=(i&-i);
        return res;
    }
} f;
 
ll dfssz(ll u, ll p)
{
    sz[u]=1;
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]) sz[u]+=dfssz(v, u);
    return sz[u]; 
}
 
ll findcentroid(ll u, ll p, ll rtsz)
{
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]&&2*sz[v]>rtsz) return findcentroid(v, u, rtsz);
    return u; 
}
 
void dfsquery(ll u, ll p, ll rt, ll sm, bool mode)
{
    if (sm>=0)
    {
        if (mode) ans+=f.query(mp[sm+vl[rt]]);
        else mp[sm+vl[rt]]=0;
    }
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]) dfsquery(v, u, rt, min(0ll, sm)+vl[v]-w, mode);
}
 
void dfsadd(ll u, ll p, ll w, ll cost, ll left, bool mode, ll add)
{
    if (w>left) cost+=(w-left), left=0;
    else left-=w;
    if (mode) f.add(mp[cost], add);
    else mp[cost]=0;
    for (auto [v, w]:d[u]) if (v!=p&&!used[v]) dfsadd(v, u, w, cost, vl[u]+left, mode, add);
}
 
void decomposition(ll u)
{
    u=findcentroid(u, u, dfssz(u, u));
    used[u]=1;
    mp.clear();
    mp[0]=0;
    mp[vl[u]]=0;
    t=0;
    for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 0, 0), dfsquery(v, u, u, -w+vl[v], 0);
    for (auto &[x, y]:mp) y=++t;
    f.add(mp[0], 1);
    for (ll i=0; i<(ll)d[u].size(); i++) 
    {
        auto [v, w]=d[u][i];
        if (used[v]) continue;
        dfsquery(v, u, u, -w+vl[v], 1);
        dfsadd(v, u, w, 0, 0, 1, 1);
    }
    f.add(mp[0], -1);
    for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 1, -1);
    for (ll i=(ll)d[u].size()-1; i>=0; i--) 
    {
        auto [v, w]=d[u][i];
        if (used[v]) continue;
        dfsquery(v, u, u, -w+vl[v], 1);
        dfsadd(v, u, w, 0, 0, 1, 1);
    }
    ans+=f.query(mp[vl[u]]);
    for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 1, -1);
    for (auto [v, w]:d[u]) if (!used[v]) decomposition(v);
}
 
int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (ll i=1; i<=n; i++) cin>>vl[i];
    for (ll i=1; i<n; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w});
    decomposition(1);
    cout<<ans;
}

/*
5
5 5 5 5 5
1 2 1
2 3 100
3 4 1
4 5 1

6
1 2 3 3 2 1
1 2 4
2 3 4
3 4 1
4 5 4
5 6 4

8
2 3 1 30 12 2 1 3
1 2 4
2 3 8
3 4 100
4 5 7
5 6 3
6 7 1
7 8 10

5
32 1 2 3 4
1 2 3
2 3 5
3 4 1
4 5 10
*/
# Verdict Execution time Memory Grader output
1 Incorrect 10 ms 4696 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 5212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 189 ms 11868 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 230 ms 13872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 342 ms 17636 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 147 ms 7844 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 130 ms 10140 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 281 ms 11500 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 418 ms 13188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 523 ms 15260 KB Output isn't correct
2 Halted 0 ms 0 KB -