Submission #1109701

# Submission time Handle Problem Language Result Execution time Memory
1109701 2024-11-07T10:48:22 Z _callmelucian Designated Cities (JOI19_designated_cities) C++14
100 / 100
278 ms 65096 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;
const auto start = chrono::high_resolution_clock::now();
const double TL = 1.8;

double getTime() {
    auto cur = chrono::high_resolution_clock::now();
    chrono::duration<double> elapsed = cur - start;
    return elapsed.count();
}

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(k << 1, lazy[k]), apply(k << 1 | 1, lazy[k]), lazy[k] = 0;
    }

    void build (int k, int l, int r, vector<pl> &a) {
        if (l == r)
            return tr[k] = a[l], void();
        int mid = (l + r) >> 1;
        build(k << 1, l, mid, a);
        build(k << 1 | 1, mid + 1, r, a);
        tr[k] = max(tr[k << 1], tr[k << 1 | 1]);
    }

    void update (int prf, ll val, int k, int l, int r) {
        for (; l < r;) {
            if (prf == r) break;
            pushDown(k);
            int mid = (l + r) >> 1;
            if (prf <= mid) k <<= 1, r = mid;
            else {
                apply(k << 1, val);
                k <<= 1, k |= 1, l = mid + 1;
            }
        }
        apply(k, val);
        for (k >>= 1; k; k >>= 1)
            tr[k] = max(tr[k << 1], tr[k << 1 | 1]);
    }

    pl getBest() { return tr[1]; }
};

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, int pA, int 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], toP[mn], timeDfs;
    bool colored[mn];
    ll weight[mn];
    IT tree(mn);

    bool dfs (int u, int p, int y, int w) {
        num[u] = ++timeDfs, sz[u] = 1, up[u] = p, toP[u] = w;
        if (u == y) colored[u] = 1;

        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];
        }
        return colored[u];
    }

    void buildTree() {
        vector<pl> a(n + 1);
        for (int i = 1; i <= n; i++)
            a[num[i]] = make_pair(0, i);
        for (int i = 1; i <= n; i++) {
            int u = a[i].second;
            if (!colored[u])
                a[i].first = weight[u] = weight[up[u]] + toP[u];
        }
        tree.build(1, 1, n, a);
    }

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

            if (getTime() > TL) {
                cout << "Still don't know why did I got TLE\n";
                exit(0);
            }
        }
    }
};

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);
    modifiedTree::buildTree();

    bool leafLeft = 1;
    for (int i = 3; i <= n; i++) {
        if (leafLeft) {
            ll delta; int node; tie(delta, node) = modifiedTree::tree.getBest();
            ans[i] = ans[i - 1] - delta;

            if (!delta) leafLeft = 0;
            modifiedTree::colorNode(node);
        }
        else ans[i] = ans[i - 1];
    }

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28752 KB Output is correct
2 Correct 6 ms 28752 KB Output is correct
3 Correct 5 ms 28752 KB Output is correct
4 Correct 6 ms 28632 KB Output is correct
5 Correct 6 ms 28752 KB Output is correct
6 Correct 5 ms 28752 KB Output is correct
7 Correct 6 ms 28752 KB Output is correct
8 Correct 6 ms 28752 KB Output is correct
9 Correct 6 ms 28752 KB Output is correct
10 Correct 5 ms 28752 KB Output is correct
11 Correct 6 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 28752 KB Output is correct
2 Correct 227 ms 46256 KB Output is correct
3 Correct 137 ms 58440 KB Output is correct
4 Correct 213 ms 46108 KB Output is correct
5 Correct 203 ms 46088 KB Output is correct
6 Correct 208 ms 48284 KB Output is correct
7 Correct 218 ms 46120 KB Output is correct
8 Correct 252 ms 59464 KB Output is correct
9 Correct 229 ms 46188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 28752 KB Output is correct
2 Correct 257 ms 46408 KB Output is correct
3 Correct 139 ms 58444 KB Output is correct
4 Correct 214 ms 46240 KB Output is correct
5 Correct 201 ms 46080 KB Output is correct
6 Correct 208 ms 48884 KB Output is correct
7 Correct 191 ms 46332 KB Output is correct
8 Correct 162 ms 55624 KB Output is correct
9 Correct 188 ms 46080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28752 KB Output is correct
2 Correct 6 ms 28752 KB Output is correct
3 Correct 5 ms 28752 KB Output is correct
4 Correct 6 ms 28632 KB Output is correct
5 Correct 6 ms 28752 KB Output is correct
6 Correct 5 ms 28752 KB Output is correct
7 Correct 6 ms 28752 KB Output is correct
8 Correct 6 ms 28752 KB Output is correct
9 Correct 6 ms 28752 KB Output is correct
10 Correct 5 ms 28752 KB Output is correct
11 Correct 6 ms 28752 KB Output is correct
12 Correct 5 ms 28752 KB Output is correct
13 Correct 6 ms 28940 KB Output is correct
14 Correct 6 ms 29020 KB Output is correct
15 Correct 6 ms 29008 KB Output is correct
16 Correct 6 ms 29008 KB Output is correct
17 Correct 7 ms 28752 KB Output is correct
18 Correct 7 ms 28752 KB Output is correct
19 Correct 7 ms 28752 KB Output is correct
20 Correct 7 ms 29008 KB Output is correct
21 Correct 7 ms 28908 KB Output is correct
22 Correct 7 ms 28752 KB Output is correct
23 Correct 8 ms 28752 KB Output is correct
24 Correct 7 ms 29008 KB Output is correct
25 Correct 7 ms 29008 KB Output is correct
26 Correct 7 ms 29008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 28752 KB Output is correct
2 Correct 227 ms 46256 KB Output is correct
3 Correct 137 ms 58440 KB Output is correct
4 Correct 213 ms 46108 KB Output is correct
5 Correct 203 ms 46088 KB Output is correct
6 Correct 208 ms 48284 KB Output is correct
7 Correct 218 ms 46120 KB Output is correct
8 Correct 252 ms 59464 KB Output is correct
9 Correct 229 ms 46188 KB Output is correct
10 Correct 7 ms 28752 KB Output is correct
11 Correct 257 ms 46408 KB Output is correct
12 Correct 139 ms 58444 KB Output is correct
13 Correct 214 ms 46240 KB Output is correct
14 Correct 201 ms 46080 KB Output is correct
15 Correct 208 ms 48884 KB Output is correct
16 Correct 191 ms 46332 KB Output is correct
17 Correct 162 ms 55624 KB Output is correct
18 Correct 188 ms 46080 KB Output is correct
19 Correct 6 ms 28752 KB Output is correct
20 Correct 235 ms 46408 KB Output is correct
21 Correct 133 ms 58440 KB Output is correct
22 Correct 205 ms 46268 KB Output is correct
23 Correct 202 ms 46420 KB Output is correct
24 Correct 199 ms 46180 KB Output is correct
25 Correct 242 ms 46308 KB Output is correct
26 Correct 222 ms 46152 KB Output is correct
27 Correct 239 ms 46212 KB Output is correct
28 Correct 238 ms 48548 KB Output is correct
29 Correct 214 ms 46408 KB Output is correct
30 Correct 210 ms 51676 KB Output is correct
31 Correct 223 ms 52532 KB Output is correct
32 Correct 185 ms 62372 KB Output is correct
33 Correct 220 ms 52616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28752 KB Output is correct
2 Correct 6 ms 28752 KB Output is correct
3 Correct 5 ms 28752 KB Output is correct
4 Correct 6 ms 28632 KB Output is correct
5 Correct 6 ms 28752 KB Output is correct
6 Correct 5 ms 28752 KB Output is correct
7 Correct 6 ms 28752 KB Output is correct
8 Correct 6 ms 28752 KB Output is correct
9 Correct 6 ms 28752 KB Output is correct
10 Correct 5 ms 28752 KB Output is correct
11 Correct 6 ms 28752 KB Output is correct
12 Correct 5 ms 28752 KB Output is correct
13 Correct 227 ms 46256 KB Output is correct
14 Correct 137 ms 58440 KB Output is correct
15 Correct 213 ms 46108 KB Output is correct
16 Correct 203 ms 46088 KB Output is correct
17 Correct 208 ms 48284 KB Output is correct
18 Correct 218 ms 46120 KB Output is correct
19 Correct 252 ms 59464 KB Output is correct
20 Correct 229 ms 46188 KB Output is correct
21 Correct 7 ms 28752 KB Output is correct
22 Correct 257 ms 46408 KB Output is correct
23 Correct 139 ms 58444 KB Output is correct
24 Correct 214 ms 46240 KB Output is correct
25 Correct 201 ms 46080 KB Output is correct
26 Correct 208 ms 48884 KB Output is correct
27 Correct 191 ms 46332 KB Output is correct
28 Correct 162 ms 55624 KB Output is correct
29 Correct 188 ms 46080 KB Output is correct
30 Correct 5 ms 28752 KB Output is correct
31 Correct 6 ms 28940 KB Output is correct
32 Correct 6 ms 29020 KB Output is correct
33 Correct 6 ms 29008 KB Output is correct
34 Correct 6 ms 29008 KB Output is correct
35 Correct 7 ms 28752 KB Output is correct
36 Correct 7 ms 28752 KB Output is correct
37 Correct 7 ms 28752 KB Output is correct
38 Correct 7 ms 29008 KB Output is correct
39 Correct 7 ms 28908 KB Output is correct
40 Correct 7 ms 28752 KB Output is correct
41 Correct 8 ms 28752 KB Output is correct
42 Correct 7 ms 29008 KB Output is correct
43 Correct 7 ms 29008 KB Output is correct
44 Correct 7 ms 29008 KB Output is correct
45 Correct 6 ms 28752 KB Output is correct
46 Correct 235 ms 46408 KB Output is correct
47 Correct 133 ms 58440 KB Output is correct
48 Correct 205 ms 46268 KB Output is correct
49 Correct 202 ms 46420 KB Output is correct
50 Correct 199 ms 46180 KB Output is correct
51 Correct 242 ms 46308 KB Output is correct
52 Correct 222 ms 46152 KB Output is correct
53 Correct 239 ms 46212 KB Output is correct
54 Correct 238 ms 48548 KB Output is correct
55 Correct 214 ms 46408 KB Output is correct
56 Correct 210 ms 51676 KB Output is correct
57 Correct 223 ms 52532 KB Output is correct
58 Correct 185 ms 62372 KB Output is correct
59 Correct 220 ms 52616 KB Output is correct
60 Correct 5 ms 28752 KB Output is correct
61 Correct 257 ms 52992 KB Output is correct
62 Correct 220 ms 65096 KB Output is correct
63 Correct 234 ms 51528 KB Output is correct
64 Correct 270 ms 52808 KB Output is correct
65 Correct 243 ms 51688 KB Output is correct
66 Correct 255 ms 52940 KB Output is correct
67 Correct 225 ms 51528 KB Output is correct
68 Correct 250 ms 52612 KB Output is correct
69 Correct 220 ms 54856 KB Output is correct
70 Correct 251 ms 52808 KB Output is correct
71 Correct 256 ms 51868 KB Output is correct
72 Correct 278 ms 52928 KB Output is correct
73 Correct 222 ms 62536 KB Output is correct
74 Correct 235 ms 53944 KB Output is correct