Submission #1095533

# Submission time Handle Problem Language Result Execution time Memory
1095533 2024-10-02T13:57:53 Z vladilius Designated Cities (JOI19_designated_cities) C++17
30 / 100
911 ms 33620 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
const ll inf = numeric_limits<ll> :: max();
 
struct ST{
    vector<pair<ll, int>> t;
    vector<ll> a, p;
    int n;
    ST(int ns){
        n = ns;
        a.resize(n + 1);
        t.resize(4 * n);
        p.resize(4 * n);
    }
    void upd(int p, ll x){
        a[p] = x;
    }
    void build(int v, int tl, int tr){
        if (tl == tr){
            t[v] = {a[tl], tl};
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        build(vv, tl, tm);
        build(vv + 1, tm + 1, tr);
        t[v] = max(t[vv], t[vv + 1]);
    }
    void build(){
        fill(p.begin(), p.end(), 0);
        build(1, 1, n);
    }
    pair<ll, int> get(){
        return t[1];
    }
    void push(int& v){
        if (!p[v]) return;
        int vv = 2 * v;
        t[vv].ff += p[v]; p[vv] += p[v];
        t[vv + 1].ff += p[v]; p[vv + 1] += p[v];
        p[v] = 0;
    }
    void add(int v, int tl, int tr, int& l, int& r, int& x){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            t[v].ff += x;
            p[v] += x;
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        push(v);
        add(vv, tl, tm, l, r, x);
        add(vv + 1, tm + 1, tr, l, r, x);
        t[v] = max(t[vv], t[vv + 1]);
    }
    void add(int l, int r, int x){
        add(1, 1, n, l, r, x);
    }
};
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n; cin>>n;
    vector<arr3> g[n + 1];
    ll tot = 0;
    for (int i = 1; i < n; i++){
        int a, b, c, d; cin>>a>>b>>c>>d;
        tot += c; tot += d;
        g[a].pb({b, c, d});
        g[b].pb({a, d, c});
    }
    
    int q; cin>>q;
    vector<int> qq(q + 1);
    for (int i = 1; i <= q; i++){
        cin>>qq[i];
    }
    
    if (q == 1 && qq[1] == 1){
        ll S = 0;
        function<void(int, int)> dfs = [&](int v, int pr){
            for (auto [i, w1, w2]: g[v]){
                if (i == pr) continue;
                S += w2;
                dfs(i, v);
            }
        };
        dfs(1, 0);
        ll out = inf;
        function<void(int, int)> solve = [&](int v, int pr){
            out = min(out, tot - S);
            for (auto [i, w1, w2]: g[v]){
                if (i == pr) continue;
                S += (w1 - w2);
                solve(i, v);
                S -= (w1 - w2);
            }
        };
        solve(1, 0);
        cout<<out<<"\n";
        return 0;
    }
    if (q == 1 && qq[1] == 2){
        return 0;
    }
        
    vector<int> tin(n + 1), tout(n + 1), inv(n + 1), p(n + 1), dd(n + 1);
    vector<ll> d(n + 1);
    int timer = 0; ll S = 0;
    function<void(int, int)> dfs = [&](int v, int pr){
        p[v] = pr;
        tin[v] = ++timer;
        for (auto [i, w1, w2]: g[v]){
            if (i == pr) continue;
            S += w2;
            dd[i] = w1;
            d[i] = d[v] + w1;
            dfs(i, v);
        }
        tout[v] = timer;
    };
    
    vector<ll> out(n + 1, inf);
    vector<bool> used(n + 1);
    ST T(n);
    for (int i = 1; i <= n; i++){
        S = d[i] = dd[i] = timer = 0;
        dfs(i, 0);
        out[1] = min(out[1], tot - S);
        for (int j = 1; j <= n; j++){
            T.upd(tin[j], d[j]);
            inv[tin[j]] = j;
        }
        T.build();
        fill(used.begin(), used.end(), 0);
        used[i] = 1;
        for (int j = 2; j <= n; j++){
            auto [x, y] = T.get();
            if (!x) continue;
            int v = inv[y];
            while (!used[v]){
                T.add(tin[v], tout[v], -dd[v]);
                used[v] = 1;
                v = p[v];
            }
            S += x;
            out[j] = min(out[j], tot - S);
        }
    }
    for (int i = 1; i <= n; i++) out[i] = min(out[i], out[i - 1]);
    
    for (int i = 1; i <= q; i++){
        cout<<out[qq[i]]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 100 ms 19732 KB Output is correct
3 Correct 111 ms 32848 KB Output is correct
4 Correct 96 ms 18320 KB Output is correct
5 Correct 101 ms 19424 KB Output is correct
6 Correct 97 ms 21784 KB Output is correct
7 Correct 88 ms 19508 KB Output is correct
8 Correct 107 ms 33620 KB Output is correct
9 Correct 71 ms 20220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 897 ms 916 KB Output is correct
14 Correct 686 ms 1108 KB Output is correct
15 Correct 836 ms 856 KB Output is correct
16 Correct 907 ms 732 KB Output is correct
17 Correct 911 ms 724 KB Output is correct
18 Correct 874 ms 856 KB Output is correct
19 Correct 886 ms 856 KB Output is correct
20 Correct 833 ms 860 KB Output is correct
21 Correct 895 ms 856 KB Output is correct
22 Correct 887 ms 912 KB Output is correct
23 Correct 870 ms 860 KB Output is correct
24 Correct 713 ms 912 KB Output is correct
25 Correct 846 ms 856 KB Output is correct
26 Correct 673 ms 912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 100 ms 19732 KB Output is correct
3 Correct 111 ms 32848 KB Output is correct
4 Correct 96 ms 18320 KB Output is correct
5 Correct 101 ms 19424 KB Output is correct
6 Correct 97 ms 21784 KB Output is correct
7 Correct 88 ms 19508 KB Output is correct
8 Correct 107 ms 33620 KB Output is correct
9 Correct 71 ms 20220 KB Output is correct
10 Incorrect 0 ms 344 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 100 ms 19732 KB Output is correct
14 Correct 111 ms 32848 KB Output is correct
15 Correct 96 ms 18320 KB Output is correct
16 Correct 101 ms 19424 KB Output is correct
17 Correct 97 ms 21784 KB Output is correct
18 Correct 88 ms 19508 KB Output is correct
19 Correct 107 ms 33620 KB Output is correct
20 Correct 71 ms 20220 KB Output is correct
21 Incorrect 0 ms 344 KB Output isn't correct
22 Halted 0 ms 0 KB -