Submission #865257

# Submission time Handle Problem Language Result Execution time Memory
865257 2023-10-24T06:52:58 Z LittleOrange Designated Cities (JOI19_designated_cities) C++17
100 / 100
551 ms 109732 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct obj{
    ll to,c0,c1;// to, back
};
struct segtree{
    ll n;
    vector<pair<ll,ll>> dat;
    vector<ll> tag;
    segtree(ll n):n(n),dat(n<<2),tag(n<<2,0){}
    void set(ll i, ll l, ll r, ll x, pair<ll,ll> v){
        if (l == r) dat[i] = v;
        else{
            ll m = l+r>>1;
            if (x<=m) set(i<<1,l,m,x,v);
            else set(i<<1|1,m+1,r,x,v);
            dat[i] = max(dat[i<<1],dat[i<<1|1]);
        }
    }
    void set(ll x, pair<ll,ll> v){
        set(1,0,n-1,x,v);
    }
    void add(ll i, ll v){
        dat[i].first += v;
        tag[i] += v;
    }
    void push(ll i){
        add(i<<1,tag[i]);
        add(i<<1|1,tag[i]);
        tag[i] = 0;
    }
    void add(ll i, ll l, ll r, ll ql, ll qr, ll v){
        if (ql<=l&&r<=qr) add(i,v);
        else{
            push(i);
            ll m = l+r>>1;
            if (ql<=m) add(i<<1,l,m,ql,qr,v);
            if (qr>m) add(i<<1|1,m+1,r,ql,qr,v);
            dat[i] = max(dat[i<<1],dat[i<<1|1]);
        }
    }
    void add(ll ql, ll qr, ll v){
        add(1,0,n-1,ql,qr,v);
    }
    ll get(){
        return dat[1].second;
    }
};
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    ll n,q;
    cin >> n;
    vector<vector<obj>> con(n);
    ll tot = 0;
    for(ll i = 1;i<n;i++){
        ll a,b,c,d;
        cin >> a >> b >> c >> d;a--;b--;
        con[a].push_back({b,c,d});
        con[b].push_back({a,d,c});
        tot += c+d;
    }
    ll leaf_cnt = 0;
    for(auto &o : con) if (o.size()==1) leaf_cnt++;
    cin >> q;
    bool use16 = false;
    if (n<=16&&use16){
        vector<ll> ans(n+1,tot);
        //vector<ll> ch(n+1,0);
        ll cur = tot;
        vector<vector<ll>> used(n,vector<ll>(n,0));
        function<void(ll,ll)> dfs;
        dfs = [&](ll i, ll p){
            for(obj l : con[i]){
                if (l.to!=p){
                    if (!used[i][l.to]){
                        used[i][l.to] = 1;
                        cur -= l.c1;
                    }
                    dfs(l.to,i);
                }
            }
        };
        for(ll x = 1;x<(1<<n);x++){
            cur = tot;
            for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++) used[i][j] = 0;
            ll lvl = __builtin_popcountll(x);
            for(ll i = 0;i<n;i++) if(x>>i&1) dfs(i,-1);
            //if (cur<ans[lvl]) ch[lvl] = x;
            ans[lvl] = min(ans[lvl],cur);
        }
        while(q--){
            ll e;
            cin >> e;
            cout << ans[e] << "\n";
            //for(ll i = 0;i<n;i++) cout << (ch[e]>>i&1);
            //cout << "\n";
        }
        return 0;
    }
    vector<ll> dp(n,0);
    {
        function<ll(ll,ll)> dfs;
        dfs = [&](ll i, ll p){
            ll cur = 0;
            for(obj l : con[i]){
                if (l.to!=p){
                    cur += l.c1 + dfs(l.to,i);
                }
            }
            return cur;
        };
        dp[0] = dfs(0,-1);
    }
    {
        function<void(ll,ll,ll)> dfs;
        dfs = [&](ll i, ll p, ll v){
            dp[i] = v;
            for(obj l : con[i]){
                if (l.to!=p){
                    dfs(l.to,i,v+l.c0-l.c1);
                }
            }
        };
        dfs(0,-1,dp[0]);
    }
    ll ans_1 = tot;
    for(ll i : dp) ans_1 = min(tot-i,ans_1);
    ll ans_2 = tot, tg0=-1,tg1=-1;
    vector<ll> go(n,0),ch(n,0),tg(n,0);
    {
        function<void(ll,ll)> dfs;
        dfs = [&](ll i, ll p){
            tg[i] = i;
            ll de = 0;
            for(obj l : con[i]){
                if (l.to!=p){
                    dfs(l.to,i);
                    go[i] += go[l.to]+l.c0;
                    if (go[l.to]+l.c0 - ch[l.to]>de) tg[i] = tg[l.to];
                    de = max(de,go[l.to]+l.c0 - ch[l.to]);
                }
            }
            ch[i] = go[i] - de;
        };
        dfs(0,-1);
    }
    {
        function<void(ll,ll,ll)> dfs;
        dfs = [&](ll i, ll p, ll v){
            ll w = v;
            for(obj l : con[i]){
                if (l.to!=p){
                    w += go[l.to]+l.c0;
                }
            }
            ll a = 0,b = 0,t0=-1,t1=-1;
            for(obj l : con[i]){
                if (l.to!=p){
                    dfs(l.to,i,w-(go[l.to]+l.c0)+l.c1);
                    ll cur = go[l.to]+l.c0 - ch[l.to];
                    if (cur>=a){
                        t1 = t0;
                        t0 = tg[l.to];
                        b = a;
                        a = cur;
                    }else if(cur>=b){
                        t1 = tg[l.to];
                        b = cur;
                    }
                }
            }
            if (t1!=-1){
                if (w-a-b<ans_2){
                    tg0 = t0;
                    tg1 = t1;
                }
                ans_2 = min(ans_2, w-a-b);
            }else if (p==-1){
                if (w-a<ans_2){
                    tg0 = t0;
                    tg1 = i;
                }
                ans_2 = min(ans_2, w-a);
            }
        };
        dfs(0,-1, 0);
    }
    //cout << "tgs: " << tg0+1 << " " << tg1+1 << "\n";
    vector<ll> ans(n+1,-1);
    ans[1] = ans_1;
    ans[2] = ans_2;
    for(ll i = leaf_cnt;i<=n;i++) ans[i] = 0;
    bool use2000 = false;
    if (n<=2000&&use2000){
        vector<ll> pre(n,-1), tag(n,0);
        {
            function<void(ll,ll)> dfs;
            dfs = [&](ll i, ll p){
                pre[i] = p;
                for(obj l : con[i]){
                    if (l.to!=p){
                        dfs(l.to,i);
                    }
                }
            };
            dfs(tg0,-1);
        }
        {
            ll cur = tg1;
            tag[cur] = 1;
            while(cur != tg0){
                cur = pre[cur];
                tag[cur] = 1;
            }
        }
        ll it = 3;
        while(it<=leaf_cnt){
            vector<ll> h(n,0);
            ll cur = 0, value = 0, cur_ans = 0;
            function<void(ll,ll)> dfs;
            dfs = [&](ll i, ll v){
                h[i] = tag[i]?0:v;
                for(obj l : con[i]){
                    if (l.to!=pre[i]){
                        dfs(l.to,l.c0+h[i]);
                        if (!tag[l.to]) cur_ans += l.c0;
                    }
                }
                if (h[i]>value){
                    value = h[i];
                    cur = i;
                }
            };
            dfs(tg0,0);
            ans[it-1] = cur_ans;
            tag[cur] = 1;
            while(cur != tg0){
                cur = pre[cur];
                tag[cur] = 1;
            }
            it++;
        }
    }
    {
        vector<ll> h(n,0),lvl(n,0),L(n),R(n);
        ll cur_ans = 0;
        vector<ll> pre(n,-1), price(n,0);
        {
            function<void(ll,ll)> dfs;
            dfs = [&](ll i, ll p){
                pre[i] = p;
                for(obj l : con[i]){
                    if (l.to!=p){
                        dfs(l.to,i);
                        price[l.to] = l.c0;
                        cur_ans += l.c0;
                    }
                }
            };
            dfs(tg0,-1);
        }
        {
            ll it = 0;
            function<void(ll,ll,ll)> dfs;
            dfs = [&](ll i, ll v,ll lv){
                L[i] = it++;
                h[i] = v;
                lvl[i] = lv;
                for(obj l : con[i]){
                    if (l.to!=pre[i]){
                        dfs(l.to,l.c0+v,lv+1);
                    }
                }
                R[i] = it++;
            };
            dfs(tg0,0,0);
        }
        segtree t(n*2);
        for(ll i = 0;i<n;i++){
            t.set(L[i],make_pair(h[i],i));
            t.set(R[i],make_pair(h[i],i));
        }
        {
            ll cur = tg1;
            while(price[cur]>0){
                t.add(L[cur],R[cur],-price[cur]);
                cur_ans -= price[cur];
                price[cur] = 0;
                cur = pre[cur];
            }
        }
        for(ll e = 3;e<leaf_cnt;e++){
            ll cur = t.get();
            while(price[cur]>0){
                t.add(L[cur],R[cur],-price[cur]);
                cur_ans -= price[cur];
                price[cur] = 0;
                cur = pre[cur];
            }
            ans[e] = cur_ans;
        }
    }
    while(q--){
        ll e;
        cin >> e;
        cout << ans[e] << "\n";
    }
}

Compilation message

designated_cities.cpp: In member function 'void segtree::set(ll, ll, ll, ll, std::pair<long long int, long long int>)':
designated_cities.cpp:15:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   15 |             ll m = l+r>>1;
      |                    ~^~
designated_cities.cpp: In member function 'void segtree::add(ll, ll, ll, ll, ll, ll)':
designated_cities.cpp:37:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |             ll m = l+r>>1;
      |                    ~^~
# Verdict Execution time Memory Grader output
1 Correct 0 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 480 ms 73288 KB Output is correct
3 Correct 546 ms 98644 KB Output is correct
4 Correct 418 ms 73008 KB Output is correct
5 Correct 435 ms 72876 KB Output is correct
6 Correct 457 ms 76552 KB Output is correct
7 Correct 382 ms 72064 KB Output is correct
8 Correct 542 ms 99336 KB Output is correct
9 Correct 316 ms 72228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 473 ms 73412 KB Output is correct
3 Correct 517 ms 102824 KB Output is correct
4 Correct 407 ms 73044 KB Output is correct
5 Correct 425 ms 72712 KB Output is correct
6 Correct 472 ms 77316 KB Output is correct
7 Correct 337 ms 72968 KB Output is correct
8 Correct 512 ms 89556 KB Output is correct
9 Correct 327 ms 71600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 3 ms 1116 KB Output is correct
14 Correct 3 ms 1372 KB Output is correct
15 Correct 3 ms 1116 KB Output is correct
16 Correct 3 ms 1112 KB Output is correct
17 Correct 3 ms 1116 KB Output is correct
18 Correct 3 ms 1116 KB Output is correct
19 Correct 2 ms 1116 KB Output is correct
20 Correct 3 ms 1116 KB Output is correct
21 Correct 3 ms 1368 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 3 ms 1116 KB Output is correct
24 Correct 2 ms 1116 KB Output is correct
25 Correct 3 ms 1368 KB Output is correct
26 Correct 2 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 480 ms 73288 KB Output is correct
3 Correct 546 ms 98644 KB Output is correct
4 Correct 418 ms 73008 KB Output is correct
5 Correct 435 ms 72876 KB Output is correct
6 Correct 457 ms 76552 KB Output is correct
7 Correct 382 ms 72064 KB Output is correct
8 Correct 542 ms 99336 KB Output is correct
9 Correct 316 ms 72228 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 473 ms 73412 KB Output is correct
12 Correct 517 ms 102824 KB Output is correct
13 Correct 407 ms 73044 KB Output is correct
14 Correct 425 ms 72712 KB Output is correct
15 Correct 472 ms 77316 KB Output is correct
16 Correct 337 ms 72968 KB Output is correct
17 Correct 512 ms 89556 KB Output is correct
18 Correct 327 ms 71600 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 452 ms 73088 KB Output is correct
21 Correct 544 ms 109732 KB Output is correct
22 Correct 443 ms 77908 KB Output is correct
23 Correct 448 ms 79692 KB Output is correct
24 Correct 458 ms 78480 KB Output is correct
25 Correct 461 ms 79704 KB Output is correct
26 Correct 432 ms 78336 KB Output is correct
27 Correct 435 ms 79296 KB Output is correct
28 Correct 461 ms 83024 KB Output is correct
29 Correct 454 ms 80208 KB Output is correct
30 Correct 404 ms 78184 KB Output is correct
31 Correct 364 ms 78148 KB Output is correct
32 Correct 514 ms 97108 KB Output is correct
33 Correct 311 ms 78580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 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 480 ms 73288 KB Output is correct
14 Correct 546 ms 98644 KB Output is correct
15 Correct 418 ms 73008 KB Output is correct
16 Correct 435 ms 72876 KB Output is correct
17 Correct 457 ms 76552 KB Output is correct
18 Correct 382 ms 72064 KB Output is correct
19 Correct 542 ms 99336 KB Output is correct
20 Correct 316 ms 72228 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 473 ms 73412 KB Output is correct
23 Correct 517 ms 102824 KB Output is correct
24 Correct 407 ms 73044 KB Output is correct
25 Correct 425 ms 72712 KB Output is correct
26 Correct 472 ms 77316 KB Output is correct
27 Correct 337 ms 72968 KB Output is correct
28 Correct 512 ms 89556 KB Output is correct
29 Correct 327 ms 71600 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 3 ms 1116 KB Output is correct
32 Correct 3 ms 1372 KB Output is correct
33 Correct 3 ms 1116 KB Output is correct
34 Correct 3 ms 1112 KB Output is correct
35 Correct 3 ms 1116 KB Output is correct
36 Correct 3 ms 1116 KB Output is correct
37 Correct 2 ms 1116 KB Output is correct
38 Correct 3 ms 1116 KB Output is correct
39 Correct 3 ms 1368 KB Output is correct
40 Correct 3 ms 1116 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 2 ms 1116 KB Output is correct
43 Correct 3 ms 1368 KB Output is correct
44 Correct 2 ms 1116 KB Output is correct
45 Correct 0 ms 344 KB Output is correct
46 Correct 452 ms 73088 KB Output is correct
47 Correct 544 ms 109732 KB Output is correct
48 Correct 443 ms 77908 KB Output is correct
49 Correct 448 ms 79692 KB Output is correct
50 Correct 458 ms 78480 KB Output is correct
51 Correct 461 ms 79704 KB Output is correct
52 Correct 432 ms 78336 KB Output is correct
53 Correct 435 ms 79296 KB Output is correct
54 Correct 461 ms 83024 KB Output is correct
55 Correct 454 ms 80208 KB Output is correct
56 Correct 404 ms 78184 KB Output is correct
57 Correct 364 ms 78148 KB Output is correct
58 Correct 514 ms 97108 KB Output is correct
59 Correct 311 ms 78580 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 491 ms 79592 KB Output is correct
62 Correct 542 ms 105044 KB Output is correct
63 Correct 453 ms 78164 KB Output is correct
64 Correct 470 ms 79652 KB Output is correct
65 Correct 468 ms 78676 KB Output is correct
66 Correct 474 ms 80100 KB Output is correct
67 Correct 478 ms 78736 KB Output is correct
68 Correct 459 ms 79372 KB Output is correct
69 Correct 482 ms 83068 KB Output is correct
70 Correct 473 ms 80724 KB Output is correct
71 Correct 432 ms 78084 KB Output is correct
72 Correct 404 ms 78416 KB Output is correct
73 Correct 551 ms 94804 KB Output is correct
74 Correct 338 ms 77788 KB Output is correct