Submission #107798

# Submission time Handle Problem Language Result Execution time Memory
107798 2019-04-25T19:56:20 Z shayan_p Designated Cities (JOI19_designated_cities) C++14
100 / 100
761 ms 64752 KB
// Faster!

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn=2e5+10,mod=1e9+7;
const ll inf=1e17;

vector< pair<int,pii> >v[maxn];
ll ans[maxn],f[maxn],g[maxn],Tot;
pair<int,ll>pr[maxn];

int n;
ll mx[maxn];
int mxid[maxn];

pii seg[maxn];
int C,reseg[maxn];

bool in[maxn];

ll val[4*maxn],lz[4*maxn];
int who[4*maxn];

void _merge(int id){
    val[id]=max(val[2*id],val[2*id+1]);
    if(val[2*id]==val[id]) who[id]=who[2*id];
    else who[id]=who[2*id+1];
}
void shift(int l,int r,int id){
    val[id]+=lz[id];
    if(r-l>1){
	lz[2*id]+=lz[id];
	lz[2*id+1]+=lz[id];
    }
    lz[id]=0;
}
void build(int l=0,int r=n,int id=1){
    lz[id]=0;
    if(r-l==1){
	who[id]=reseg[l];
	val[id]=f[reseg[l]];
	return;
    }
    int mid=(l+r)>>1;
    build(l,mid,2*id);
    build(mid,r,2*id+1);
    _merge(id);
}
int ask(){
    shift(0,n,1);
    if(val[1]<=0) return -1;
    return who[1];
}
void put(int pos,ll x,int l=0,int r=n,int id=1){
    shift(l,r,id);
    if(r-l==1){
	val[id]=x;
	return;
    }
    int mid=(l+r)>>1;
    if(pos<mid) put(pos,x,l,mid,2*id), shift(mid,r,2*id+1);
    else put(pos,x,mid,r,2*id+1), shift(l,mid,2*id);
    _merge(id);
}
void add(int f,int s,ll x,int l=0,int r=n,int id=1){
    if(f>=s || l>=r) return;
    shift(l,r,id);
    if(s<=l || r<=f) return;
    if(f<=l && r<=s) { lz[id]=x; shift(l,r,id); return; }
    int mid=(l+r)>>1;
    add(f,s,x,l,mid,2*id);
    add(f,s,x,mid,r,2*id+1);
    _merge(id);
}

void prep(int u,int par=-1){
    mx[u]=f[u],mxid[u]=u;
    seg[u].F=C, reseg[C]=u, C++;
    for(auto p:v[u]){
	if(p.F!=par){
	    f[p.F]=f[u]+p.S.F;
	    g[p.F]=g[u]+p.S.S;
	    Tot+=p.S.F;
	    pr[p.F]={u,p.S.F};
	    
	    prep(p.F,u);
	    
	    if(mx[p.F]>mx[u]){
		mx[u]=mx[p.F];
		mxid[u]=mxid[p.F];
	    }
	}
    }
    seg[u].S=C;
}

pair<ll,pii> choose(int u,int par=-1){
    pair<ll,pii> ans={Tot-mx[u]+g[u],{u,mxid[u]}};
    pair<ll,int> pp={-1,-1};
    for(auto p:v[u]){
	if(p.F!=par){
	    ans=min(ans,choose(p.F,u));
	    if(pp.F!=-1) ans=min(ans, (pair<ll,pii>){Tot-mx[p.F]-pp.F+f[u]+g[u], {mxid[p.F],pp.S} } );
	    pp=max(pp, (pair<ll,int>){mx[p.F],mxid[p.F]} );
	}
    }
    return ans;
}

void solve(int u){
    f[u]=g[u]=Tot=C=0,prep(u);
    memset(in,0,sizeof in);
    in[u]=1;
    build();
    int pt=1;
    while(ask()!=-1){
	int tmp=ask();
	while(in[tmp]==0){
	    add(seg[tmp].F,seg[tmp].S,-pr[tmp].S);
	    put(seg[tmp].F,0);
	    Tot-=pr[tmp].S;
	    in[tmp]=1;
	    tmp=pr[tmp].F;
	}
	++pt,ans[pt]=min(ans[pt],Tot);
    }
    while(pt<n){
	++pt,ans[pt]=min(ans[pt],Tot);
    }
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    cin>>n;
    fill(ans,ans+n+1,inf);
    for(int i=0;i<n-1;i++){
	int a,b,c,d;cin>>a>>b>>c>>d;
	--a,--b;
	v[a].PB({b,{c,d}});
	v[b].PB({a,{d,c}});
    }
    prep(0);
    for(int i=0;i<n;i++){
	ans[1]=min(ans[1],Tot-f[i]+g[i]);
    }
    pii p=choose(0).S;
    solve(p.F);
    int q;cin>>q;
    while(q--){
	int x;cin>>x;
	cout<<ans[x]<<"\n";
    }
    return 0;
}
// Deathly mistakes:
//  * Read the problem curfully.
//  * Check maxn.
//  * Overflows.
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 6 ms 5504 KB Output is correct
6 Correct 6 ms 5376 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 6 ms 5248 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 657 ms 42616 KB Output is correct
3 Correct 634 ms 61076 KB Output is correct
4 Correct 540 ms 41340 KB Output is correct
5 Correct 574 ms 42412 KB Output is correct
6 Correct 673 ms 45412 KB Output is correct
7 Correct 506 ms 42416 KB Output is correct
8 Correct 680 ms 61920 KB Output is correct
9 Correct 703 ms 43068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5248 KB Output is correct
2 Correct 696 ms 42780 KB Output is correct
3 Correct 625 ms 64568 KB Output is correct
4 Correct 603 ms 41264 KB Output is correct
5 Correct 565 ms 42540 KB Output is correct
6 Correct 696 ms 45956 KB Output is correct
7 Correct 662 ms 42788 KB Output is correct
8 Correct 667 ms 55104 KB Output is correct
9 Correct 656 ms 42784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 6 ms 5504 KB Output is correct
6 Correct 6 ms 5376 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 6 ms 5248 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5376 KB Output is correct
12 Correct 7 ms 5376 KB Output is correct
13 Correct 8 ms 5632 KB Output is correct
14 Correct 9 ms 5760 KB Output is correct
15 Correct 10 ms 5632 KB Output is correct
16 Correct 10 ms 5632 KB Output is correct
17 Correct 10 ms 5632 KB Output is correct
18 Correct 10 ms 5624 KB Output is correct
19 Correct 10 ms 5760 KB Output is correct
20 Correct 9 ms 5632 KB Output is correct
21 Correct 9 ms 5632 KB Output is correct
22 Correct 10 ms 5632 KB Output is correct
23 Correct 9 ms 5632 KB Output is correct
24 Correct 11 ms 5760 KB Output is correct
25 Correct 11 ms 5760 KB Output is correct
26 Correct 10 ms 5632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 657 ms 42616 KB Output is correct
3 Correct 634 ms 61076 KB Output is correct
4 Correct 540 ms 41340 KB Output is correct
5 Correct 574 ms 42412 KB Output is correct
6 Correct 673 ms 45412 KB Output is correct
7 Correct 506 ms 42416 KB Output is correct
8 Correct 680 ms 61920 KB Output is correct
9 Correct 703 ms 43068 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 696 ms 42780 KB Output is correct
12 Correct 625 ms 64568 KB Output is correct
13 Correct 603 ms 41264 KB Output is correct
14 Correct 565 ms 42540 KB Output is correct
15 Correct 696 ms 45956 KB Output is correct
16 Correct 662 ms 42788 KB Output is correct
17 Correct 667 ms 55104 KB Output is correct
18 Correct 656 ms 42784 KB Output is correct
19 Correct 6 ms 5248 KB Output is correct
20 Correct 681 ms 42696 KB Output is correct
21 Correct 624 ms 64752 KB Output is correct
22 Correct 579 ms 41348 KB Output is correct
23 Correct 619 ms 42844 KB Output is correct
24 Correct 576 ms 41676 KB Output is correct
25 Correct 619 ms 42744 KB Output is correct
26 Correct 651 ms 41688 KB Output is correct
27 Correct 723 ms 42828 KB Output is correct
28 Correct 699 ms 45304 KB Output is correct
29 Correct 577 ms 42844 KB Output is correct
30 Correct 532 ms 41776 KB Output is correct
31 Correct 737 ms 42664 KB Output is correct
32 Correct 761 ms 55968 KB Output is correct
33 Correct 706 ms 43220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5376 KB Output is correct
2 Correct 6 ms 5248 KB Output is correct
3 Correct 6 ms 5376 KB Output is correct
4 Correct 6 ms 5248 KB Output is correct
5 Correct 6 ms 5504 KB Output is correct
6 Correct 6 ms 5376 KB Output is correct
7 Correct 5 ms 5248 KB Output is correct
8 Correct 6 ms 5248 KB Output is correct
9 Correct 6 ms 5376 KB Output is correct
10 Correct 7 ms 5248 KB Output is correct
11 Correct 7 ms 5376 KB Output is correct
12 Correct 7 ms 5376 KB Output is correct
13 Correct 657 ms 42616 KB Output is correct
14 Correct 634 ms 61076 KB Output is correct
15 Correct 540 ms 41340 KB Output is correct
16 Correct 574 ms 42412 KB Output is correct
17 Correct 673 ms 45412 KB Output is correct
18 Correct 506 ms 42416 KB Output is correct
19 Correct 680 ms 61920 KB Output is correct
20 Correct 703 ms 43068 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 696 ms 42780 KB Output is correct
23 Correct 625 ms 64568 KB Output is correct
24 Correct 603 ms 41264 KB Output is correct
25 Correct 565 ms 42540 KB Output is correct
26 Correct 696 ms 45956 KB Output is correct
27 Correct 662 ms 42788 KB Output is correct
28 Correct 667 ms 55104 KB Output is correct
29 Correct 656 ms 42784 KB Output is correct
30 Correct 7 ms 5376 KB Output is correct
31 Correct 8 ms 5632 KB Output is correct
32 Correct 9 ms 5760 KB Output is correct
33 Correct 10 ms 5632 KB Output is correct
34 Correct 10 ms 5632 KB Output is correct
35 Correct 10 ms 5632 KB Output is correct
36 Correct 10 ms 5624 KB Output is correct
37 Correct 10 ms 5760 KB Output is correct
38 Correct 9 ms 5632 KB Output is correct
39 Correct 9 ms 5632 KB Output is correct
40 Correct 10 ms 5632 KB Output is correct
41 Correct 9 ms 5632 KB Output is correct
42 Correct 11 ms 5760 KB Output is correct
43 Correct 11 ms 5760 KB Output is correct
44 Correct 10 ms 5632 KB Output is correct
45 Correct 6 ms 5248 KB Output is correct
46 Correct 681 ms 42696 KB Output is correct
47 Correct 624 ms 64752 KB Output is correct
48 Correct 579 ms 41348 KB Output is correct
49 Correct 619 ms 42844 KB Output is correct
50 Correct 576 ms 41676 KB Output is correct
51 Correct 619 ms 42744 KB Output is correct
52 Correct 651 ms 41688 KB Output is correct
53 Correct 723 ms 42828 KB Output is correct
54 Correct 699 ms 45304 KB Output is correct
55 Correct 577 ms 42844 KB Output is correct
56 Correct 532 ms 41776 KB Output is correct
57 Correct 737 ms 42664 KB Output is correct
58 Correct 761 ms 55968 KB Output is correct
59 Correct 706 ms 43220 KB Output is correct
60 Correct 6 ms 5376 KB Output is correct
61 Correct 702 ms 45304 KB Output is correct
62 Correct 668 ms 63068 KB Output is correct
63 Correct 638 ms 44152 KB Output is correct
64 Correct 719 ms 45304 KB Output is correct
65 Correct 685 ms 43896 KB Output is correct
66 Correct 681 ms 45396 KB Output is correct
67 Correct 690 ms 44148 KB Output is correct
68 Correct 738 ms 45236 KB Output is correct
69 Correct 681 ms 47648 KB Output is correct
70 Correct 618 ms 45176 KB Output is correct
71 Correct 691 ms 44584 KB Output is correct
72 Correct 735 ms 45868 KB Output is correct
73 Correct 734 ms 56452 KB Output is correct
74 Correct 642 ms 47172 KB Output is correct