Submission #480403

# Submission time Handle Problem Language Result Execution time Memory
480403 2021-10-16T06:07:51 Z bonopo Transport (COCI19_transport) C++17
0 / 130
323 ms 14688 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define rc (i<<1)|1
#define lc (i<<1)
#define s second
#define f first
#define el "\n"
#define int long long

typedef long long ll;
const int MM=1e5+5, MOD=1e9+7, LOG=19;
int N, sz[MM], bk[MM], g[MM]; ll ans;
vector<pair<int,int>> adj[MM];
vector<ll> vdw, vup;

void dfs_up(int u, int pa, ll gc, ll lo) {
    if(lo>=0) ++ans, vup.pb(lo);
    for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) {
        dfs_up(e.f, u, gc+g[e.f]-e.s, min(lo+g[e.f]-e.s, g[e.f]-e.s));
    }
}

void dfs_dw(int u, int pa, ll gc, ll hi) {
    if(hi<=0) ++ans;
    vdw.pb(hi);
    for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) {
        dfs_dw(e.f, u, gc-g[u]+e.s, max(hi, gc-g[e.f]+e.s));
    }
}

inline void solve(int rt) {
    vector<ll> gdw, gup;
    for(auto &e:adj[rt]) if(!bk[e.f]) {
        vdw.clear(); vup.clear();
        dfs_dw(e.f, rt, e.s-g[rt], e.s-g[rt]);
        dfs_up(e.f, rt, g[e.f]-e.s, g[e.f]-e.s);
        sort(begin(vup), end(vup));
        sort(begin(vdw), end(vdw));
        for(ll &x:vdw) gdw.pb(x);
        for(ll &x:vup) gup.pb(x);
        for(int i=0, j=0, sz=vup.size(); i<vdw.size(); i++) {
            while(j<sz&&vup[j]<vdw[i]) ++j;
            ans-=sz-j;
        }
    }
    sort(begin(gup), end(gup));
    sort(begin(gdw), end(gdw));
    for(int i=0, j=0, sz=gup.size(); i<gdw.size(); i++) {
        while(j<sz&&gup[j]<gdw[i]) ++j;
        ans+=sz-j;
    }
}

void gsz(int u, int pa) {
    sz[u]=1;
    for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) {
        gsz(e.f, u); sz[u]+=sz[e.f];
    }
}

int crt(int u, int th, int pa) {
    for(auto &e:adj[u]) if(e.f!=pa&&!bk[e.f]) {
        if(sz[e.f]>th) return crt(e.f, th, u); }
    return u;
}

void rec(int u, int nd) {
    gsz(u, 0);
    int ct=crt(u, nd/2, 0);
    bk[ct]=1;

    if(nd==1) return;
    solve(ct); gsz(ct, 0);
    for(auto &e:adj[ct]) if(!bk[e.f]) rec(e.f, sz[e.f]);
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>N;
    for(int i=1; i<=N; i++) cin>>g[i];

    for(int i=1; i<N; i++) {
        int u, v, w; cin>>u>>v>>w;
        adj[u].pb({v,w});
        adj[v].pb({u,w});
    }

    rec(1, N);
    cout<<ans<<el;
}

// MM

Compilation message

transport.cpp: In function 'void solve(long long int)':
transport.cpp:44:43: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for(int i=0, j=0, sz=vup.size(); i<vdw.size(); i++) {
      |                                          ~^~~~~~~~~~~
transport.cpp:51:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |     for(int i=0, j=0, sz=gup.size(); i<gdw.size(); i++) {
      |                                      ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 2892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 3252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 89 ms 9064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 111 ms 11364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 201 ms 14688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 79 ms 5188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 62 ms 6724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 184 ms 7916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 266 ms 9900 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 323 ms 12136 KB Output isn't correct
2 Halted 0 ms 0 KB -