Submission #165075

# Submission time Handle Problem Language Result Execution time Memory
165075 2019-11-25T04:16:52 Z puppies_and_rainbows Designated Cities (JOI19_designated_cities) C++14
100 / 100
1942 ms 63996 KB
#include <bits/stdc++.h>
#define int long long
#pragma GCC optimize("O3")
using namespace std;

int sum=0, m, n;
int u[200005], v[200005], a[200005],b[200005];
int tin[200005], tout[200005], pos[200005];
vector<int> adj[200005];
int ans[200005];
bool used[200005];
pair<int, int> trace2;

int f[200005];
int lazy[800005];
pair<int, int> it[800005];

int now=0, dora;

void down(int id){
    it[id*2].first+=lazy[id];
    it[id*2+1].first+=lazy[id];
    lazy[id*2]+=lazy[id];
    lazy[id*2+1]+=lazy[id];
    lazy[id]=0;
}
void update(int id, int l, int r, int u, int v, int val){
    if(l>v||r<u) return;
    if(l>=u&&r<=v)
    {
        it[id].first+=val;
        lazy[id]+=val;
        return;
    }
    down(id);
    int mid=(l+r)/2;
    update(id*2, l, mid, u, v, val);
    update(id*2+1, mid+1, r, u, v, val);
    it[id]=max(it[id*2], it[id*2+1]);
}
pair<int, int> get(int id, int l, int r, int u, int v){
    if(l>v||r<u) return{-1, -1};
    if(l>=u&&r<=v)
    {
        return it[id];
    }
    down(id);
    int mid=(l+r)/2;
    return max(get(id*2, l, mid, u, v), get(id*2+1, mid+1, r, u, v));
}
pair<int, int> unpack(int node, int i){
    int uv=a[i], vu=b[i];
    if(node!=u[i])
    {
        swap(uv, vu);
    }
    return {uv, vu};
}
void dfs(int node, int fa, bool ok){
    f[node]=fa;
    dora++;
    tin[node]=dora;
    pos[dora]=node;
    for(auto i:adj[node])
    {
        int other=u[i]+v[i]-node, uv, vu;
        if(other==fa) continue;
        tie(uv, vu)=unpack(node, i);
        dfs(other, node, ok);
    }
    tout[node]=dora;
    if(ok==1)
    for(auto i:adj[node])
    {
        int other=u[i]+v[i]-node, uv, vu;
        if(other==fa) continue;
        tie(uv, vu)=unpack(node, i);
        update(1, 1, n, tin[other], tout[other], uv);
        now+=vu;
    }
}
void dfsget(int node, int fa){
    int w, oth;
    ans[1]=max(ans[1], now);
    tie(w, oth)=it[1];
    if(ans[2]<w+now)
    {
    	// cout<<w<<" "<<now<<endl;
        ans[2]=w+now;
        trace2={node, oth};
    }
    for(auto i:adj[node])
    {
        int other=u[i]+v[i]-node, uv, vu;
        if(other==fa) continue;
        tie(uv, vu)=unpack(node, i);
        now+=uv-vu;
        update(1, 1, n, 1, n, vu);
        update(1, 1, n, tin[other], tout[other], -vu-uv);
        dfsget(other, node);
        now-=uv-vu;
        update(1, 1, n, 1, n, -vu);
        update(1, 1, n, tin[other], tout[other], +vu+uv);
    }
}
void rebuild(int id, int l, int r){
    if(l==r)
    {
    	it[id]={0, pos[l]};
    	lazy[id]=0;
    }
    else
    {
        int mid=(l+r)/2;
        rebuild(id*2, l, mid);
        rebuild(id*2+1, mid+1, r);
        it[id]=min(it[id*2], it[id*2+1]);
        lazy[id]=0;
    }
}

signed main()
{
	ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1; i<n; i++)
    {
        cin>>u[i]>>v[i]>>a[i]>>b[i];
        sum+=a[i]+b[i];
        adj[u[i]].push_back(i);
        adj[v[i]].push_back(i);
    }
    dora=0, now=0;
    dfs(1, 1, 0);
    rebuild(1, 1, n);
    dora=0, now=0;
    dfs(1, 1, 1);
    dfsget(1, 1);
    // cout<<"wow shit this happened"<<endl;
    dora=0, now=0;
    dfs(trace2.first, trace2.first, 0);
    rebuild(1, 1, n);
    dora=0, now=0;
    dfs(trace2.first, trace2.first, 1);
    int lfcnt=0;
    for(int i=1; i<=n; i++)
    {
        if(adj[i].size()==1)
        {
            lfcnt++;
        }
    }
    used[trace2.first]=true;
    // for(int i=1; i<=4; i++)
    // {
    // 	cout<<f[i]<<endl;
    // }
    // return 0;
    for(int i=2; i<=lfcnt; i++)
    {
    	ans[i]=ans[i-1];
    	if(i==2) ans[i]=now;
        int w, oth;
        tie(w, oth)=it[1];
        while(!used[oth])
        {
        	// cout<<oth<<endl;
            for(auto j:adj[oth])
            {
                if(u[j]+v[j]-oth==f[oth])
                {
                    int uv, vu;
                    tie(uv, vu)=unpack(oth, j);
                    update(1, 1, n, tin[oth], tout[oth], -vu);
                    ans[i]+=vu;
                    used[oth]=true;
                    break;
                }
            }
            oth=f[oth];
        }
    }
    // cout<<"how the fuck did my code even run to this point"<<endl;
    int q;
    cin>>q;
    for(int i=1; i<=q; i++)
    {
        int ax;
        cin>>ax;
        if(ax>lfcnt) ax=lfcnt;
        cout<<sum-ans[ax]<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 5 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5116 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 1069 ms 38636 KB Output is correct
3 Correct 1343 ms 61816 KB Output is correct
4 Correct 1033 ms 38392 KB Output is correct
5 Correct 1006 ms 38516 KB Output is correct
6 Correct 1157 ms 41540 KB Output is correct
7 Correct 972 ms 38760 KB Output is correct
8 Correct 1393 ms 60792 KB Output is correct
9 Correct 803 ms 39908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5172 KB Output is correct
2 Correct 1044 ms 38532 KB Output is correct
3 Correct 1250 ms 63608 KB Output is correct
4 Correct 1030 ms 38564 KB Output is correct
5 Correct 1022 ms 38740 KB Output is correct
6 Correct 1123 ms 42464 KB Output is correct
7 Correct 863 ms 39624 KB Output is correct
8 Correct 1268 ms 53424 KB Output is correct
9 Correct 807 ms 39908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 5 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5116 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 17 ms 5496 KB Output is correct
14 Correct 17 ms 5752 KB Output is correct
15 Correct 17 ms 5496 KB Output is correct
16 Correct 17 ms 5496 KB Output is correct
17 Correct 17 ms 5496 KB Output is correct
18 Correct 17 ms 5496 KB Output is correct
19 Correct 17 ms 5496 KB Output is correct
20 Correct 18 ms 5496 KB Output is correct
21 Correct 17 ms 5496 KB Output is correct
22 Correct 18 ms 5624 KB Output is correct
23 Correct 16 ms 5576 KB Output is correct
24 Correct 16 ms 5496 KB Output is correct
25 Correct 17 ms 5752 KB Output is correct
26 Correct 16 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 1069 ms 38636 KB Output is correct
3 Correct 1343 ms 61816 KB Output is correct
4 Correct 1033 ms 38392 KB Output is correct
5 Correct 1006 ms 38516 KB Output is correct
6 Correct 1157 ms 41540 KB Output is correct
7 Correct 972 ms 38760 KB Output is correct
8 Correct 1393 ms 60792 KB Output is correct
9 Correct 803 ms 39908 KB Output is correct
10 Correct 6 ms 5172 KB Output is correct
11 Correct 1044 ms 38532 KB Output is correct
12 Correct 1250 ms 63608 KB Output is correct
13 Correct 1030 ms 38564 KB Output is correct
14 Correct 1022 ms 38740 KB Output is correct
15 Correct 1123 ms 42464 KB Output is correct
16 Correct 863 ms 39624 KB Output is correct
17 Correct 1268 ms 53424 KB Output is correct
18 Correct 807 ms 39908 KB Output is correct
19 Correct 6 ms 5112 KB Output is correct
20 Correct 1067 ms 38628 KB Output is correct
21 Correct 1343 ms 63996 KB Output is correct
22 Correct 1052 ms 38672 KB Output is correct
23 Correct 1051 ms 38484 KB Output is correct
24 Correct 1202 ms 38608 KB Output is correct
25 Correct 1063 ms 38612 KB Output is correct
26 Correct 1293 ms 38588 KB Output is correct
27 Correct 1032 ms 38604 KB Output is correct
28 Correct 1141 ms 41772 KB Output is correct
29 Correct 1070 ms 38608 KB Output is correct
30 Correct 1033 ms 38856 KB Output is correct
31 Correct 910 ms 39064 KB Output is correct
32 Correct 1277 ms 54304 KB Output is correct
33 Correct 827 ms 39908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 5 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5116 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 1069 ms 38636 KB Output is correct
14 Correct 1343 ms 61816 KB Output is correct
15 Correct 1033 ms 38392 KB Output is correct
16 Correct 1006 ms 38516 KB Output is correct
17 Correct 1157 ms 41540 KB Output is correct
18 Correct 972 ms 38760 KB Output is correct
19 Correct 1393 ms 60792 KB Output is correct
20 Correct 803 ms 39908 KB Output is correct
21 Correct 6 ms 5172 KB Output is correct
22 Correct 1044 ms 38532 KB Output is correct
23 Correct 1250 ms 63608 KB Output is correct
24 Correct 1030 ms 38564 KB Output is correct
25 Correct 1022 ms 38740 KB Output is correct
26 Correct 1123 ms 42464 KB Output is correct
27 Correct 863 ms 39624 KB Output is correct
28 Correct 1268 ms 53424 KB Output is correct
29 Correct 807 ms 39908 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 17 ms 5496 KB Output is correct
32 Correct 17 ms 5752 KB Output is correct
33 Correct 17 ms 5496 KB Output is correct
34 Correct 17 ms 5496 KB Output is correct
35 Correct 17 ms 5496 KB Output is correct
36 Correct 17 ms 5496 KB Output is correct
37 Correct 17 ms 5496 KB Output is correct
38 Correct 18 ms 5496 KB Output is correct
39 Correct 17 ms 5496 KB Output is correct
40 Correct 18 ms 5624 KB Output is correct
41 Correct 16 ms 5576 KB Output is correct
42 Correct 16 ms 5496 KB Output is correct
43 Correct 17 ms 5752 KB Output is correct
44 Correct 16 ms 5496 KB Output is correct
45 Correct 6 ms 5112 KB Output is correct
46 Correct 1067 ms 38628 KB Output is correct
47 Correct 1343 ms 63996 KB Output is correct
48 Correct 1052 ms 38672 KB Output is correct
49 Correct 1051 ms 38484 KB Output is correct
50 Correct 1202 ms 38608 KB Output is correct
51 Correct 1063 ms 38612 KB Output is correct
52 Correct 1293 ms 38588 KB Output is correct
53 Correct 1032 ms 38604 KB Output is correct
54 Correct 1141 ms 41772 KB Output is correct
55 Correct 1070 ms 38608 KB Output is correct
56 Correct 1033 ms 38856 KB Output is correct
57 Correct 910 ms 39064 KB Output is correct
58 Correct 1277 ms 54304 KB Output is correct
59 Correct 827 ms 39908 KB Output is correct
60 Correct 6 ms 5116 KB Output is correct
61 Correct 1674 ms 39940 KB Output is correct
62 Correct 1885 ms 62400 KB Output is correct
63 Correct 1819 ms 45920 KB Output is correct
64 Correct 1691 ms 47352 KB Output is correct
65 Correct 1644 ms 45952 KB Output is correct
66 Correct 1592 ms 47224 KB Output is correct
67 Correct 1754 ms 45944 KB Output is correct
68 Correct 1614 ms 47348 KB Output is correct
69 Correct 1732 ms 50404 KB Output is correct
70 Correct 1663 ms 47176 KB Output is correct
71 Correct 1643 ms 46720 KB Output is correct
72 Correct 1523 ms 48584 KB Output is correct
73 Correct 1942 ms 62588 KB Output is correct
74 Correct 1438 ms 50184 KB Output is correct