답안 #232499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
232499 2020-05-17T08:17:03 Z VEGAnn Transport (COCI19_transport) C++14
130 / 130
898 ms 19448 KB
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MP make_pair
#define PB push_back
#define ft first
#define sd second
#define sz(x) ((int)x.size())
#define pil pair<int,ll>
#define pli pair<ll,ll>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int N = 100100;
const int oo = 2e9;
ordered_set<pli> st;
vector<pil> g[N];
ll ans = 0, a[N];
int n, siz[N];
bool mrk[N];

void build_sz(int v, int p){
    siz[v] = 1;

    for (pil u : g[v]){
        if (u.ft == p || mrk[u.ft]) continue;

        build_sz(u.ft, v);

        siz[v] += siz[u.ft];
    }
}

int search(int v, int p, int SZ){
    for (pil u : g[v])
        if (u.ft != p && !mrk[u.ft] && siz[u.ft] > SZ / 2)
            return search(u.ft, v, SZ);
    return v;
}

void check_up(int v, int p, ll sm, int p_ed, ll sf){
    sf = min(sf - p_ed + a[v], 0ll);
    sm += a[v] - p_ed;

    if (sf == 0)
        ans += st.order_of_key(MP(sm, oo));

    for (pil u : g[v]) {
        if (u.ft == p || mrk[u.ft]) continue;
        check_up(u.ft, v, sm, u.sd, sf);
    }
}

void insrt(int v, int p, int p_ed, ll sm, ll mn, ll sta){
    mn = min(mn, sm - p_ed);

    st.insert(MP(-mn, v));

    sm += a[v] - p_ed;

    if (-mn <= sta)
        ans++;

    for (pil u : g[v]){
        if (u.ft == p || mrk[u.ft]) continue;
        insrt(u.ft, v, u.sd, sm, mn, sta);
    }
}

void build_cd(int v){
    build_sz(v, -1);

    v = search(v, -1, siz[v]);

    st.clear();
    st.insert(MP(0, v));

    for (int it = 0; it < sz(g[v]); it++){
        pil u = g[v][it];

        if (mrk[u.ft]) continue;

        check_up(u.ft, v, a[v], u.sd, 0);

        insrt(u.ft, v, u.sd, 0, 0, a[v]);
    }

    st.clear();

    for (int it = sz(g[v]) - 1; it >= 0; it--){
        pil u = g[v][it];

        if (mrk[u.ft]) continue;

        check_up(u.ft, v, a[v], u.sd, 0);

        insrt(u.ft, v, u.sd, 0, 0, -1);
    }

    mrk[v] = 1;

    for (pil u : g[v])
        if (!mrk[u.ft])
            build_cd(u.ft);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> n;

    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = 1; i < n; i++){
        int x, y, w; cin >> x >> y >> w;
        x--; y--;

        g[x].PB(MP(y, w));
        g[y].PB(MP(x, w));
    }

    build_cd(0);

    cout << ans;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3072 KB Output is correct
2 Correct 16 ms 3200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 3328 KB Output is correct
2 Correct 28 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 10104 KB Output is correct
2 Correct 247 ms 9464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 408 ms 12920 KB Output is correct
2 Correct 453 ms 14080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 587 ms 16612 KB Output is correct
2 Correct 689 ms 19448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 186 ms 6264 KB Output is correct
2 Correct 90 ms 5632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 8568 KB Output is correct
2 Correct 300 ms 7980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 440 ms 10500 KB Output is correct
2 Correct 406 ms 10872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 572 ms 12664 KB Output is correct
2 Correct 572 ms 13048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 898 ms 15864 KB Output is correct
2 Correct 789 ms 14660 KB Output is correct