답안 #1109704

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109704 2024-11-07T10:49:50 Z _callmelucian Designated Cities (JOI19_designated_cities) C++14
100 / 100
332 ms 60944 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;

#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())

const int mn = 2e5 + 5;

struct IT {
    vector<pl> tr;
    vector<ll> lazy;

    IT (int sz) : tr(4 * sz), lazy(4 * sz) {}

    void apply (int k, ll val) {
        tr[k].first += val, lazy[k] += val;
    }

    void pushDown (int k) {
        if (lazy[k])
            apply(2 * k, lazy[k]), apply(2 * k + 1, lazy[k]), lazy[k] = 0;
    }

    void indexing (int pos, int val, int k, int l, int r) {
        for (; l < r;) {
            int mid = (l + r) >> 1;
            if (pos <= mid) k <<= 1, r = mid;
            else k <<= 1, k |= 1, l = mid + 1;
        }
        tr[k] = make_pair(0, val);
        for (k /= 2; k; k /= 2)
            tr[k] = max(tr[2 * k], tr[2 * k + 1]);
    }

    void update (int a, int b, ll val, int k, int l, int r) {
        if (b < l || r < a) return;
        if (a <= l && r <= b)
            return apply(k, val), void();
        pushDown(k);
        int mid = (l + r) >> 1;
        update(a, b, val, 2 * k, l, mid);
        update(a, b, val, 2 * k + 1, mid + 1, r);
        tr[k] = max(tr[2 * k], tr[2 * k + 1]);
    }

    pl query (int a, int b, int k, int l, int r) {
        if (b < l || r < a) return make_pair(0, 0);
        if (a <= l && r <= b) return tr[k];
        pushDown(k);
        int mid = (l + r) >> 1;
        return max(query(a, b, 2 * k, l, mid), query(a, b, 2 * k + 1, mid + 1, r));
    }
};

vector<tpl> adj[mn];
ll ans[mn], n;

namespace originalTree {
    int bestLeaf[mn], secLeaf[mn];
    ll downPath[mn], upPath[mn], sumDown;

    void dfs (int u, int p, ll pA, ll pH) {
        // construct costs on paths
        downPath[u] = downPath[p] + pA;
        upPath[u] = upPath[p] + pH;
        bestLeaf[u] = u;

        // run dfs down
        for (tpl item : adj[u]) {
            int v, away, home; tie(v, away, home) = item;
            if (v == p) continue;
            dfs(v, u, away, home);

            sumDown += away;
            if (downPath[bestLeaf[v]] > downPath[bestLeaf[u]])
                secLeaf[u] = bestLeaf[u], bestLeaf[u] = bestLeaf[v];
            else if (downPath[bestLeaf[v]] > downPath[secLeaf[u]]) secLeaf[u] = bestLeaf[v];
        }
    }

    ll tryNode (int u) {
        ll pathOne = downPath[bestLeaf[u]] - (bestLeaf[u] ? downPath[u] : 0);
        ll pathTwo = downPath[secLeaf[u]] - (secLeaf[u] ? downPath[u] : 0);
        return sumDown - pathOne - pathTwo - downPath[u] + upPath[u];
    }

    ll chooseNode (int u) {
        return sumDown - downPath[u] + upPath[u];
    }
}

namespace modifiedTree {
    int num[mn], sz[mn], up[mn], timeDfs;
    bool colored[mn];
    ll weight[mn];
    IT tree(mn);

    bool dfs (int u, int p, int y, ll w) {
        num[u] = ++timeDfs, sz[u] = 1, up[u] = p, weight[u] = w;
        if (u == y) colored[u] = 1;
        tree.indexing(num[u], u, 1, 1, n);

        for (tpl item : adj[u]) {
            int v, away, home; tie(v, away, home) = item;
            if (v == p) continue;
            if (dfs(v, u, y, away)) colored[u] = 1;
            sz[u] += sz[v];
        }

        if (!colored[u])
            tree.update(num[u], num[u] + sz[u] - 1, w, 1, 1, n);
        return colored[u];
    }

    pl pickCand() {
        return tree.query(1, n, 1, 1, n);
    }

    void colorNode (int u) {
        while (!colored[u]) {
            tree.update(num[u], num[u] + sz[u] - 1, -weight[u], 1, 1, n);
            colored[u] = 1, u = up[u];
        }
    }
};

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

    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b, c, d; cin >> a >> b >> c >> d;
        adj[a].emplace_back(b, c, d);
        adj[b].emplace_back(a, d, c);
    }
    originalTree::dfs(1, 1, 0, 0);

    for (int i = 1; i <= n; i++) ans[i] = LLONG_MAX;

    int optX = 0, optY = 0;
    for (int i = 1; i <= n; i++) {
        ll cur = originalTree::tryNode(i);
        if (cur < ans[2])
            ans[2] = cur, optX = originalTree::bestLeaf[i], optY = originalTree::secLeaf[i];

        ans[1] = min(ans[1], originalTree::chooseNode(i));
    }
    modifiedTree::dfs(optX, optX, optY, 0);

    for (int i = 3; i <= n; i++) {
        ll delta; int node; tie(delta, node) = modifiedTree::pickCand();
        ans[i] = ans[i - 1] - delta;
        modifiedTree::colorNode(node);
    }

    int q; cin >> q;
    while (q--) {
        int E; cin >> E;
        cout << ans[E] << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 5 ms 27848 KB Output is correct
3 Correct 5 ms 27984 KB Output is correct
4 Correct 5 ms 27792 KB Output is correct
5 Correct 5 ms 27984 KB Output is correct
6 Correct 5 ms 28152 KB Output is correct
7 Correct 4 ms 27856 KB Output is correct
8 Correct 5 ms 28152 KB Output is correct
9 Correct 5 ms 27984 KB Output is correct
10 Correct 5 ms 27984 KB Output is correct
11 Correct 5 ms 27984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 248 ms 42568 KB Output is correct
3 Correct 168 ms 59208 KB Output is correct
4 Correct 256 ms 42516 KB Output is correct
5 Correct 252 ms 42272 KB Output is correct
6 Correct 246 ms 44628 KB Output is correct
7 Correct 293 ms 42236 KB Output is correct
8 Correct 166 ms 58452 KB Output is correct
9 Correct 212 ms 42756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 249 ms 42580 KB Output is correct
3 Correct 152 ms 59208 KB Output is correct
4 Correct 243 ms 42312 KB Output is correct
5 Correct 281 ms 42320 KB Output is correct
6 Correct 263 ms 45384 KB Output is correct
7 Correct 285 ms 42736 KB Output is correct
8 Correct 234 ms 53700 KB Output is correct
9 Correct 217 ms 41972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 5 ms 27848 KB Output is correct
3 Correct 5 ms 27984 KB Output is correct
4 Correct 5 ms 27792 KB Output is correct
5 Correct 5 ms 27984 KB Output is correct
6 Correct 5 ms 28152 KB Output is correct
7 Correct 4 ms 27856 KB Output is correct
8 Correct 5 ms 28152 KB Output is correct
9 Correct 5 ms 27984 KB Output is correct
10 Correct 5 ms 27984 KB Output is correct
11 Correct 5 ms 27984 KB Output is correct
12 Correct 15 ms 23888 KB Output is correct
13 Correct 7 ms 27984 KB Output is correct
14 Correct 6 ms 28240 KB Output is correct
15 Correct 7 ms 27984 KB Output is correct
16 Correct 7 ms 28240 KB Output is correct
17 Correct 7 ms 28256 KB Output is correct
18 Correct 8 ms 28240 KB Output is correct
19 Correct 6 ms 27984 KB Output is correct
20 Correct 9 ms 28240 KB Output is correct
21 Correct 6 ms 28120 KB Output is correct
22 Correct 6 ms 27984 KB Output is correct
23 Correct 6 ms 28240 KB Output is correct
24 Correct 6 ms 28240 KB Output is correct
25 Correct 6 ms 28240 KB Output is correct
26 Correct 6 ms 28044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 248 ms 42568 KB Output is correct
3 Correct 168 ms 59208 KB Output is correct
4 Correct 256 ms 42516 KB Output is correct
5 Correct 252 ms 42272 KB Output is correct
6 Correct 246 ms 44628 KB Output is correct
7 Correct 293 ms 42236 KB Output is correct
8 Correct 166 ms 58452 KB Output is correct
9 Correct 212 ms 42756 KB Output is correct
10 Correct 5 ms 27984 KB Output is correct
11 Correct 249 ms 42580 KB Output is correct
12 Correct 152 ms 59208 KB Output is correct
13 Correct 243 ms 42312 KB Output is correct
14 Correct 281 ms 42320 KB Output is correct
15 Correct 263 ms 45384 KB Output is correct
16 Correct 285 ms 42736 KB Output is correct
17 Correct 234 ms 53700 KB Output is correct
18 Correct 217 ms 41972 KB Output is correct
19 Correct 5 ms 27984 KB Output is correct
20 Correct 292 ms 44144 KB Output is correct
21 Correct 180 ms 60944 KB Output is correct
22 Correct 312 ms 44100 KB Output is correct
23 Correct 279 ms 45220 KB Output is correct
24 Correct 325 ms 44244 KB Output is correct
25 Correct 332 ms 42580 KB Output is correct
26 Correct 269 ms 42568 KB Output is correct
27 Correct 283 ms 44292 KB Output is correct
28 Correct 262 ms 46152 KB Output is correct
29 Correct 271 ms 42312 KB Output is correct
30 Correct 281 ms 42496 KB Output is correct
31 Correct 257 ms 45852 KB Output is correct
32 Correct 226 ms 54228 KB Output is correct
33 Correct 262 ms 42840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 27984 KB Output is correct
2 Correct 5 ms 27848 KB Output is correct
3 Correct 5 ms 27984 KB Output is correct
4 Correct 5 ms 27792 KB Output is correct
5 Correct 5 ms 27984 KB Output is correct
6 Correct 5 ms 28152 KB Output is correct
7 Correct 4 ms 27856 KB Output is correct
8 Correct 5 ms 28152 KB Output is correct
9 Correct 5 ms 27984 KB Output is correct
10 Correct 5 ms 27984 KB Output is correct
11 Correct 5 ms 27984 KB Output is correct
12 Correct 5 ms 27984 KB Output is correct
13 Correct 248 ms 42568 KB Output is correct
14 Correct 168 ms 59208 KB Output is correct
15 Correct 256 ms 42516 KB Output is correct
16 Correct 252 ms 42272 KB Output is correct
17 Correct 246 ms 44628 KB Output is correct
18 Correct 293 ms 42236 KB Output is correct
19 Correct 166 ms 58452 KB Output is correct
20 Correct 212 ms 42756 KB Output is correct
21 Correct 5 ms 27984 KB Output is correct
22 Correct 249 ms 42580 KB Output is correct
23 Correct 152 ms 59208 KB Output is correct
24 Correct 243 ms 42312 KB Output is correct
25 Correct 281 ms 42320 KB Output is correct
26 Correct 263 ms 45384 KB Output is correct
27 Correct 285 ms 42736 KB Output is correct
28 Correct 234 ms 53700 KB Output is correct
29 Correct 217 ms 41972 KB Output is correct
30 Correct 15 ms 23888 KB Output is correct
31 Correct 7 ms 27984 KB Output is correct
32 Correct 6 ms 28240 KB Output is correct
33 Correct 7 ms 27984 KB Output is correct
34 Correct 7 ms 28240 KB Output is correct
35 Correct 7 ms 28256 KB Output is correct
36 Correct 8 ms 28240 KB Output is correct
37 Correct 6 ms 27984 KB Output is correct
38 Correct 9 ms 28240 KB Output is correct
39 Correct 6 ms 28120 KB Output is correct
40 Correct 6 ms 27984 KB Output is correct
41 Correct 6 ms 28240 KB Output is correct
42 Correct 6 ms 28240 KB Output is correct
43 Correct 6 ms 28240 KB Output is correct
44 Correct 6 ms 28044 KB Output is correct
45 Correct 5 ms 27984 KB Output is correct
46 Correct 292 ms 44144 KB Output is correct
47 Correct 180 ms 60944 KB Output is correct
48 Correct 312 ms 44100 KB Output is correct
49 Correct 279 ms 45220 KB Output is correct
50 Correct 325 ms 44244 KB Output is correct
51 Correct 332 ms 42580 KB Output is correct
52 Correct 269 ms 42568 KB Output is correct
53 Correct 283 ms 44292 KB Output is correct
54 Correct 262 ms 46152 KB Output is correct
55 Correct 271 ms 42312 KB Output is correct
56 Correct 281 ms 42496 KB Output is correct
57 Correct 257 ms 45852 KB Output is correct
58 Correct 226 ms 54228 KB Output is correct
59 Correct 262 ms 42840 KB Output is correct
60 Correct 5 ms 27984 KB Output is correct
61 Correct 301 ms 43984 KB Output is correct
62 Correct 200 ms 59720 KB Output is correct
63 Correct 304 ms 47176 KB Output is correct
64 Correct 307 ms 47944 KB Output is correct
65 Correct 290 ms 43592 KB Output is correct
66 Correct 271 ms 47892 KB Output is correct
67 Correct 265 ms 47128 KB Output is correct
68 Correct 248 ms 43908 KB Output is correct
69 Correct 260 ms 50504 KB Output is correct
70 Correct 259 ms 47688 KB Output is correct
71 Correct 249 ms 47584 KB Output is correct
72 Correct 246 ms 44292 KB Output is correct
73 Correct 217 ms 55112 KB Output is correct
74 Correct 260 ms 45820 KB Output is correct