제출 #1334985

#제출 시각아이디문제언어결과실행 시간메모리
1334985adscoding공장들 (JOI14_factories)C++20
100 / 100
1907 ms198100 KiB
#include <bits/stdc++.h>
#include "factories.h"
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 5e5 + 3;
int n, q, h[maxn], tin[maxn], tout[maxn], tour[maxn << 1][20], lg[maxn << 1], tdfs;
ll D[maxn], dist[maxn], dp_down[maxn], dp_up[maxn];
vector<pii> g[maxn];
vector<pair<int, ll>> tree[maxn];
bool has[maxn];

// --------------------------------------------------------------------------------------------

void dfs_pre(int u, int p)
{
    tin[u] = ++tdfs;
    tour[tdfs][0] = u;
    for (const auto &e : g[u])
    {
        if (e.fi == p) continue;
        D[e.fi] = D[u] + e.se;
        h[e.fi] = h[u] + 1;
        dfs_pre(e.fi, u);
        tour[++tdfs][0] = u;
    }
    tout[u] = tdfs;
}

#define MIN_H(x, y) (h[x] < h[y] ? x : y)

int lca(int u, int v)
{
    u = tin[u]; v = tin[v];
    if (u > v) swap(u, v);
    int k = lg[v - u + 1];
    return MIN_H(tour[u][k], tour[v - (1 << k) + 1][k]);
}

ll get_dist(int u, int v)
{
    return D[u] + D[v] - 2 * D[lca(u, v)];
}

void dfs_down(int u, int p)
{
    if (has[u])
        dp_down[u] = 0ll;
    for (const auto &e : tree[u])
    {
        if (e.fi == p) continue;
        dfs_down(e.fi, u);
        minimize(dp_down[u], dp_down[e.fi] + e.se);
    }
}

void dfs_up(int u, int p)
{
    if (has[u])
        dp_up[u] = 0ll;
    vector<pair<int, ll>> nodes;
    for (const auto &e : tree[u])
    {
        if (e.fi == p) continue;
        nodes.push_back(e);
    }

    vector<ll> SUFF(int(nodes.size()) + 3, ll(1e18));
    FORD(i, int(nodes.size()) - 1, 0)
    {
        auto &e = nodes[i];
        SUFF[i] = dp_down[e.fi] + e.se;
        minimize(SUFF[i], SUFF[i + 1]);
    }

    ll PRE = ll(1e18);
    FOR(i, 0, int(nodes.size()) - 1)
    {
        auto &e = nodes[i];
        ll dpu = dp_up[u];
        minimize(dpu, SUFF[i + 1]);
        minimize(dpu, PRE);

        dp_up[e.fi] = dpu + e.se;

        minimize(PRE, dp_down[e.fi] + e.se);
    }

    for (const auto &e : nodes)
        dfs_up(e.fi, u);
}

ll Query(int S, int X[], int T, int Y[])
{
    int nA = S, nB = T;
    vector<int> vers, vecA, vecB;
    FOR(i, 0, nA - 1)
    {
        vecA.push_back(X[i]);
        vers.push_back(X[i]);
    }
    FOR(i, 0, nB - 1)
    {
        vecB.push_back(Y[i]);
        vers.push_back(Y[i]);
        has[Y[i]] = 1;
    }
    sort(all(vers), [&](const int &x, const int &y) {
        return tin[x] < tin[y];
    });
    FOR(i, 0, int(vers.size()) - 2)
    {
        vers.push_back(lca(vers[i], vers[i + 1]));
    }
    sort(all(vers), [&](const int &x, const int &y) {
        return tin[x] < tin[y];
    });
    vers.erase(unique(all(vers)), vers.end());

    for (int x : vers)
    {
        dp_down[x] = dp_up[x] = ll(1e18);
    }

    vector<int> st;
    st.push_back(vers[0]);
    FOR(i, 1, int(vers.size()) - 1)
    {
        int u = vers[i];
        while (st.size() && tin[u] > tout[st.back()]) st.pop_back();
        tree[st.back()].push_back({u, get_dist(st.back(), u)});
        st.push_back(u);
    }

    dfs_down(vers[0], -1);
    dfs_up(vers[0], -1);

    ll res = 1e18;
    for (int x : vecA)
    {
        minimize(res, dp_down[x]);
        minimize(res, dp_up[x]);
    }

    for (int x : vers)
    {
        tree[x].clear();
    }
    for (int x : vecB)
        has[x] = 0;

    return res;
}

void Init(int N, int A[], int B[], int D[])
{
    n = N;
    FOR(i, 0, n - 1)
    {
        g[i].clear();
    }
    FOR(i, 0, n - 2)
    {
        int u = A[i], v = B[i], w = D[i];
        if (u > v) swap(u, v);
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }

    tdfs = 0;
    dfs_pre(0, -1);

    FOR(i, 2, tdfs)
        lg[i] = lg[i >> 1] + 1;
    FOR(j, 1, 19)
        FOR(i, 1, tdfs - (1 << j) + 1)
            tour[i][j] = MIN_H(tour[i][j - 1], tour[i + (1 << j - 1)][j - 1]);
}

//signed main()
//{
//    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//    #define TASK "TEST"
//    if (fopen(TASK".INP", "r"))
//    {
//        freopen(TASK".INP", "r", stdin);
//        freopen(TASK".OUT", "w", stdout);
//    }
//    solve();
//    return 0;
//}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...