답안 #943223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943223 2024-03-11T09:25:49 Z 12345678 Transport (COCI19_transport) C++17
0 / 130
521 ms 17868 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 x)
    {
        while (i<nx) d[i]+=x, 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, ll pre, bool mode)
{
    if (sm>=0&&pre>=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, sm+vl[v]-w, min(0ll, pre+(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[v]+left, mode, add);
}
 
void decomposition(ll u)
{
    u=findcentroid(u, u, dfssz(u, u));
    used[u]=1;
    mp.clear();
    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, 0);
    for (auto &[x, y]:mp) y=++t;
    f.add(mp[vl[u]], 1);
    for (auto [v, w]:d[u]) if (!used[v]) dfsquery(v, u, u, -w+vl[v], 0, 1), dfsadd(v, u, w, 0, 0, 1, 1);
    f.add(mp[vl[u]], -1);
    for (auto [v, w]:d[u]) if (!used[v]) dfsadd(v, u, w, 0, 0, 1, -1);
    reverse(d[u].begin(), d[u].end());
    for (auto [v, w]:d[u]) if (!used[v]) dfsquery(v, u, u, -w+vl[v], 0, 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);
    //cout<<u<<' '<<ans<<'\n';
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 4700 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 5208 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 180 ms 11932 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 214 ms 13908 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 326 ms 17868 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 138 ms 7768 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 126 ms 10076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 288 ms 11496 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 364 ms 13396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 521 ms 15296 KB Output isn't correct
2 Halted 0 ms 0 KB -