답안 #165071

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
165071 2019-11-25T04:12:59 Z puppies_and_rainbows Designated Cities (JOI19_designated_cities) C++14
56 / 100
2000 ms 65284 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){
    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);
    }
    tout[node]=dora;
    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);
    rebuild(1, 1, n);
    dora=0, now=0;
    dfs(1, 1);
    dfsget(1, 1);
    // cout<<"wow shit this happened"<<endl;
    dora=0, now=0;
    dfs(trace2.first, trace2.first);
    rebuild(1, 1, n);
    dora=0, now=0;
    dfs(trace2.first, trace2.first);
    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';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 1319 ms 38640 KB Output is correct
3 Correct 1571 ms 65120 KB Output is correct
4 Correct 1183 ms 38648 KB Output is correct
5 Correct 1229 ms 38756 KB Output is correct
6 Correct 1335 ms 41848 KB Output is correct
7 Correct 1096 ms 39020 KB Output is correct
8 Correct 1711 ms 63796 KB Output is correct
9 Correct 963 ms 39908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 1263 ms 38724 KB Output is correct
3 Correct 1524 ms 65248 KB Output is correct
4 Correct 1217 ms 38724 KB Output is correct
5 Correct 1256 ms 38784 KB Output is correct
6 Correct 1360 ms 43180 KB Output is correct
7 Correct 1069 ms 39864 KB Output is correct
8 Correct 1585 ms 55664 KB Output is correct
9 Correct 993 ms 40236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 33 ms 5552 KB Output is correct
14 Correct 18 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 18 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 19 ms 5624 KB Output is correct
22 Correct 17 ms 5496 KB Output is correct
23 Correct 17 ms 5496 KB Output is correct
24 Correct 17 ms 5524 KB Output is correct
25 Correct 19 ms 5744 KB Output is correct
26 Correct 16 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 1319 ms 38640 KB Output is correct
3 Correct 1571 ms 65120 KB Output is correct
4 Correct 1183 ms 38648 KB Output is correct
5 Correct 1229 ms 38756 KB Output is correct
6 Correct 1335 ms 41848 KB Output is correct
7 Correct 1096 ms 39020 KB Output is correct
8 Correct 1711 ms 63796 KB Output is correct
9 Correct 963 ms 39908 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 1263 ms 38724 KB Output is correct
12 Correct 1524 ms 65248 KB Output is correct
13 Correct 1217 ms 38724 KB Output is correct
14 Correct 1256 ms 38784 KB Output is correct
15 Correct 1360 ms 43180 KB Output is correct
16 Correct 1069 ms 39864 KB Output is correct
17 Correct 1585 ms 55664 KB Output is correct
18 Correct 993 ms 40236 KB Output is correct
19 Correct 6 ms 5112 KB Output is correct
20 Correct 1294 ms 38708 KB Output is correct
21 Correct 1614 ms 65284 KB Output is correct
22 Correct 1184 ms 38648 KB Output is correct
23 Correct 1232 ms 38776 KB Output is correct
24 Correct 1272 ms 38724 KB Output is correct
25 Correct 1269 ms 38720 KB Output is correct
26 Correct 1238 ms 38784 KB Output is correct
27 Correct 1204 ms 38736 KB Output is correct
28 Correct 1532 ms 42452 KB Output is correct
29 Correct 1293 ms 38836 KB Output is correct
30 Correct 1279 ms 38932 KB Output is correct
31 Correct 1104 ms 39320 KB Output is correct
32 Correct 1683 ms 56520 KB Output is correct
33 Correct 957 ms 40104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 5112 KB Output is correct
6 Correct 7 ms 5112 KB Output is correct
7 Correct 6 ms 5112 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 7 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
12 Correct 7 ms 5112 KB Output is correct
13 Correct 1319 ms 38640 KB Output is correct
14 Correct 1571 ms 65120 KB Output is correct
15 Correct 1183 ms 38648 KB Output is correct
16 Correct 1229 ms 38756 KB Output is correct
17 Correct 1335 ms 41848 KB Output is correct
18 Correct 1096 ms 39020 KB Output is correct
19 Correct 1711 ms 63796 KB Output is correct
20 Correct 963 ms 39908 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 1263 ms 38724 KB Output is correct
23 Correct 1524 ms 65248 KB Output is correct
24 Correct 1217 ms 38724 KB Output is correct
25 Correct 1256 ms 38784 KB Output is correct
26 Correct 1360 ms 43180 KB Output is correct
27 Correct 1069 ms 39864 KB Output is correct
28 Correct 1585 ms 55664 KB Output is correct
29 Correct 993 ms 40236 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 33 ms 5552 KB Output is correct
32 Correct 18 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 18 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 19 ms 5624 KB Output is correct
40 Correct 17 ms 5496 KB Output is correct
41 Correct 17 ms 5496 KB Output is correct
42 Correct 17 ms 5524 KB Output is correct
43 Correct 19 ms 5744 KB Output is correct
44 Correct 16 ms 5496 KB Output is correct
45 Correct 6 ms 5112 KB Output is correct
46 Correct 1294 ms 38708 KB Output is correct
47 Correct 1614 ms 65284 KB Output is correct
48 Correct 1184 ms 38648 KB Output is correct
49 Correct 1232 ms 38776 KB Output is correct
50 Correct 1272 ms 38724 KB Output is correct
51 Correct 1269 ms 38720 KB Output is correct
52 Correct 1238 ms 38784 KB Output is correct
53 Correct 1204 ms 38736 KB Output is correct
54 Correct 1532 ms 42452 KB Output is correct
55 Correct 1293 ms 38836 KB Output is correct
56 Correct 1279 ms 38932 KB Output is correct
57 Correct 1104 ms 39320 KB Output is correct
58 Correct 1683 ms 56520 KB Output is correct
59 Correct 957 ms 40104 KB Output is correct
60 Correct 6 ms 5116 KB Output is correct
61 Correct 1890 ms 40236 KB Output is correct
62 Execution timed out 2023 ms 65276 KB Time limit exceeded
63 Halted 0 ms 0 KB -