답안 #232846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
232846 2020-05-18T11:00:15 Z Vimmer Transport (COCI19_transport) C++14
26 / 130
1000 ms 15092 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")

#define sz(x) ll(x.size())
#define N 100015
#define base 1000000
#define M ll(1e9+7)
#define F first
#define S second
#define pb push_back
#define in insert
#define eb emplace_back
#define ed "\n"

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef short int si;

//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


ll ans, t[N];

int a[N], n, siz[N], kol;

vector <pair <int, int> > g[N];

bool mk[N];

vector <ll> se;

void add(int v) {for (; v < N; v = (v | (v + 1))) t[v] ++;}

ll sum(int v) {ll res = 0; for (; v >= 0; v = (v & (v + 1)) - 1) res += t[v]; return res;}

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

    for (auto it : g[v])
    {
        if (it.F == p || mk[it.F]) continue;

        dfs(it.F, v);

        siz[v] += siz[it.F];
    }
}

void rec(int v, int p, ll cur, ll sm)
{
    cur += ll(a[v]);

    se.pb(sm);

    for (auto it : g[v])
    {
        if (it.F == p || mk[it.F]) continue;

        rec(it.F, v, max(0ll, cur - ll(it.S)), sm + max(0ll, ll(it.S) - cur));
    }
}

void rec_add(int v, int p, ll cur, ll sm)
{
    cur += ll(a[v]);

    add(upper_bound(se.begin(), se.end(), sm) - se.begin() - 1);

    for (auto it : g[v])
    {
        if (it.F == p || mk[it.F]) continue;

        rec_add(it.F, v, max(0ll, cur - ll(it.S)), sm + max(0ll, ll(it.S) - cur));
    }
}

int fnd_cent(int v, int p)
{
    if (p != -1)
    {
        siz[p] -= siz[v];

        siz[v] += siz[p];
    }

    int nm = -1;

    for (auto it : g[v])
    {
        if (mk[it.F]) continue;

        if (siz[it.F] > siz[v] / 2) {nm = it.F; break;}
    }

    if (nm == -1) return v;

    return fnd_cent(nm, v);
}

void spusk(int v, int p, ll sm, ll cost, ll need)
{
    siz[v] = 1;

    need += cost;

    need = max(0ll, need - a[v]);

    sm += a[v];

    sm -= cost;

    if (need == 0) {kol++; ans += ll(sum(upper_bound(se.begin(), se.end(), sm) - se.begin() - 1));}

    for (auto it : g[v])
    {
        if (it.F == p || mk[it.F]) continue;

        spusk(it.F, v, sm, it.S, need);

        siz[v] += siz[it.F];
    }
}

void calc(int v)
{
    kol = 0;

    mk[v] = 1;

    se.clear();

    memset(t, 0ll, sizeof(t));

    se.pb(-1);

    for (auto it : g[v])
    {
        if (mk[it.F]) continue;

        rec(it.F, -1, 0, it.S);
    }

    sort(se.begin(), se.end());

    se.resize(unique(se.begin(), se.end()) - se.begin());

    for (auto it : g[v])
    {
        if (mk[it.F]) continue;

        spusk(it.F, -1, a[v], it.S, 0);

        rec_add(it.F, -1, 0, it.S);
    }

    memset(t, 0ll, sizeof(t));

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

        if (mk[it.F]) continue;

        spusk(it.F, -1, a[v], it.S, 0);

        rec_add(it.F, -1, 0, it.S);
    }

    ans += kol / 2;

    ans += ll(sum(upper_bound(se.begin(), se.end(), a[v]) - se.begin() - 1));

    for (auto it : g[v])
    {
        if (mk[it.F]) continue;

        dfs(it.F, -1);

        int root = fnd_cent(it.F, -1);

        if (root != -1) calc(root);
    }
}

int main()
{
    //freopen("input.txt","r",stdin);// freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;

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

    for (int i = 1; i < n; i++)
    {
        int x, y, z;

        cin >> x >> y >> z;

        g[x].pb({y, z});

        g[y].pb({x, z});
    }

    dfs(1, -1);

    int root = fnd_cent(1, -1);

    calc(root);

    cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 147 ms 3780 KB Output is correct
2 Correct 194 ms 3904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 4096 KB Output is correct
2 Correct 250 ms 4224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 9592 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1089 ms 11768 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1097 ms 15092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1092 ms 5812 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1038 ms 7548 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 8564 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1090 ms 10096 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1091 ms 12148 KB Time limit exceeded
2 Halted 0 ms 0 KB -