Submission #526904

# Submission time Handle Problem Language Result Execution time Memory
526904 2022-02-16T15:43:14 Z wildturtle Designated Cities (JOI19_designated_cities) C++17
100 / 100
1085 ms 72108 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define sc second
#define pb push_back
using namespace std;
ll a,b,c,d,i,e,f,g,n,m,k,l;
ll t,maxroot,fotlebi,wvrt,sum,idx;
pair < pair <ll,ll> , pair <ll,ll> > A[200005];
ll in[200005],out[200005],P[200005],D[200005],down[200005],inv[200005];
ll lz[800005];
pair <ll,ll> tree[800005],pr;
ll ans[200005],fix[200005];
vector < pair < ll, pair <ll,ll> > > v[200005];
void build(ll node,ll le,ll ri) {
	lz[node]=0;
	if(le==ri) {
		tree[node].f=0;
		tree[node].sc=le;
		return;
	}
	build(2*node,le,(le+ri)/2);
	build(2*node+1,(le+ri)/2+1,ri);
	if(tree[2*node].f>tree[2*node+1].f) tree[node]=tree[2*node];
	else tree[node]=tree[2*node+1];
}
void update(ll node,ll le,ll ri,ll start,ll end,ll val) {
	if(lz[node]) {
		tree[node].f+=lz[node];
		if(le!=ri) {
			lz[2*node]+=lz[node];
			lz[2*node+1]+=lz[node];
		}
		lz[node]=0;
	}
	if(ri<start || le>end) return;
	if(start<=le && ri<=end) {
		tree[node].f+=val;
		if(le!=ri) {
			lz[2*node]+=val;
			lz[2*node+1]+=val;
		}	
		return;
	}
	update(2*node,le,(le+ri)/2,start,end,val);
	update(2*node+1,(le+ri)/2+1,ri,start,end,val);
	if(tree[2*node].f>tree[2*node+1].f) tree[node]=tree[2*node];
	else tree[node]=tree[2*node+1];
}
pair <ll,ll> getmax(ll node,ll le,ll ri,ll start,ll end) {
	if(lz[node]) {
		tree[node].f+=lz[node];
		if(le!=ri) {
			lz[2*node]+=lz[node];
			lz[2*node+1]+=lz[node];
		}
		lz[node]=0;
	}
	if(ri<start || le>end) return {-1,-1};
	if(start<=le && ri<=end) { return tree[node]; }
	pair <ll,ll> pr1=getmax(2*node,le,(le+ri)/2,start,end);
	pair <ll,ll> pr2=getmax(2*node+1,(le+ri)/2+1,ri,start,end);
	if(pr1.f>pr2.f) return pr1;
	else return pr2;
}
void dfs(ll x,ll y,ll dist) {
	//cout<<x<<" "<<y<<" "<<dist<<endl;
	t++;
	in[x]=t;
	D[x]=dist;
	P[x]=y;
	inv[t]=x;
	down[x]=D[x]-D[y];
	if(x!=maxroot && v[x].size()==1 && maxroot!=0) fotlebi++;
	update(1,1,n,in[x],in[x],D[x]);
	//cout<<x<<" "<<in[x]<<" "<<D[x]<<endl;
	for(ll i=0;i<v[x].size();i++) {
		if(v[x][i].f==y) continue;
		dfs(v[x][i].f,x,dist+v[x][i].sc.f);
		wvrt+=v[x][i].sc.sc;
	}
	out[x]=t;
}
void dfs1(ll x,ll y) {
	ans[1]=max(ans[1],wvrt);
	pr=getmax(1,1,n,1,n);
	//cout<<x<<"_"<<wvrt<<"_"<<pr.f<<endl;
	if(ans[2]<wvrt+pr.f) { ans[2]=wvrt+pr.f; maxroot=x; }
	for(ll i=0;i<v[x].size();i++) {
		if(v[x][i].f==y) continue;
		ll yx=v[x][i].sc.f; ll xy=v[x][i].sc.sc;
		wvrt=wvrt+yx-xy;
		update(1,1,n,in[v[x][i].f],out[v[x][i].f],-yx);
		update(1,1,n,1,in[v[x][i].f]-1,xy);
		update(1,1,n,out[v[x][i].f]+1,n,xy);		
		dfs1(v[x][i].f,x);
		wvrt=wvrt-yx+xy;
		update(1,1,n,in[v[x][i].f],out[v[x][i].f],+yx);
		update(1,1,n,1,in[v[x][i].f]-1,-xy);
		update(1,1,n,out[v[x][i].f]+1,n,-xy);
	}
}
int main() {
	std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	for(ll i=1;i<n;i++) {
		cin>>A[i].f.f>>A[i].f.sc>>A[i].sc.f>>A[i].sc.sc;
		v[A[i].f.f].pb({A[i].f.sc,{A[i].sc.f,A[i].sc.sc}});
		v[A[i].f.sc].pb({A[i].f.f,{A[i].sc.sc,A[i].sc.f}});
		sum+=A[i].sc.f+A[i].sc.sc;
	}
	build(1,1,n);
	
	dfs(1,0,0);
	//cout<<"*"; return 0;
	dfs1(1,0);
	build(1,1,n);
	t=0;
	//cout<<maxroot<<endl;
	dfs(maxroot,0,0);
	for(ll i=2;i<=fotlebi+1;i++) {
		pr=getmax(1,1,n,1,n);
		if(i!=2) ans[i]=ans[i-1]+pr.f;
		//cout<<ans[i]<<" ";
		idx=inv[pr.sc];
		//cout<<pr.f<<" "<<pr.sc<<" "<<idx<<endl;
		while(idx!=maxroot && fix[idx]==0) {
			fix[idx]=1;
			update(1,1,n,in[idx],out[idx],-down[idx]);
			idx=P[idx];
		}
	}
	cin>>t;
	while(t--) {
		cin>>f;
		if(f>fotlebi) cout<<0<<endl;
		else cout<<sum-ans[f]<<endl;
	}
}
/*
4
1 2 1 2
1 3 3 4
1 4 5 6
2
1
2
*/

Compilation message

designated_cities.cpp: In function 'void dfs(long long int, long long int, long long int)':
designated_cities.cpp:77:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |  for(ll i=0;i<v[x].size();i++) {
      |             ~^~~~~~~~~~~~
designated_cities.cpp: In function 'void dfs1(long long int, long long int)':
designated_cities.cpp:89:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |  for(ll i=0;i<v[x].size();i++) {
      |             ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 2 ms 5068 KB Output is correct
3 Correct 2 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 2 ms 5068 KB Output is correct
7 Correct 2 ms 5016 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 829 ms 54640 KB Output is correct
3 Correct 770 ms 69408 KB Output is correct
4 Correct 736 ms 53208 KB Output is correct
5 Correct 784 ms 54652 KB Output is correct
6 Correct 772 ms 57072 KB Output is correct
7 Correct 739 ms 53752 KB Output is correct
8 Correct 852 ms 69892 KB Output is correct
9 Correct 669 ms 53660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5020 KB Output is correct
2 Correct 760 ms 54584 KB Output is correct
3 Correct 681 ms 71908 KB Output is correct
4 Correct 708 ms 53240 KB Output is correct
5 Correct 741 ms 54644 KB Output is correct
6 Correct 785 ms 57300 KB Output is correct
7 Correct 669 ms 53440 KB Output is correct
8 Correct 830 ms 64692 KB Output is correct
9 Correct 671 ms 53216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 2 ms 5068 KB Output is correct
3 Correct 2 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 2 ms 5068 KB Output is correct
7 Correct 2 ms 5016 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5020 KB Output is correct
13 Correct 9 ms 5580 KB Output is correct
14 Correct 9 ms 5596 KB Output is correct
15 Correct 9 ms 5536 KB Output is correct
16 Correct 10 ms 5444 KB Output is correct
17 Correct 10 ms 5572 KB Output is correct
18 Correct 9 ms 5540 KB Output is correct
19 Correct 9 ms 5452 KB Output is correct
20 Correct 10 ms 5452 KB Output is correct
21 Correct 10 ms 5548 KB Output is correct
22 Correct 11 ms 5532 KB Output is correct
23 Correct 11 ms 5452 KB Output is correct
24 Correct 9 ms 5580 KB Output is correct
25 Correct 10 ms 5708 KB Output is correct
26 Correct 9 ms 5544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 829 ms 54640 KB Output is correct
3 Correct 770 ms 69408 KB Output is correct
4 Correct 736 ms 53208 KB Output is correct
5 Correct 784 ms 54652 KB Output is correct
6 Correct 772 ms 57072 KB Output is correct
7 Correct 739 ms 53752 KB Output is correct
8 Correct 852 ms 69892 KB Output is correct
9 Correct 669 ms 53660 KB Output is correct
10 Correct 3 ms 5020 KB Output is correct
11 Correct 760 ms 54584 KB Output is correct
12 Correct 681 ms 71908 KB Output is correct
13 Correct 708 ms 53240 KB Output is correct
14 Correct 741 ms 54644 KB Output is correct
15 Correct 785 ms 57300 KB Output is correct
16 Correct 669 ms 53440 KB Output is correct
17 Correct 830 ms 64692 KB Output is correct
18 Correct 671 ms 53216 KB Output is correct
19 Correct 2 ms 5068 KB Output is correct
20 Correct 767 ms 54664 KB Output is correct
21 Correct 821 ms 72108 KB Output is correct
22 Correct 741 ms 53276 KB Output is correct
23 Correct 800 ms 54940 KB Output is correct
24 Correct 748 ms 53700 KB Output is correct
25 Correct 777 ms 54952 KB Output is correct
26 Correct 764 ms 53920 KB Output is correct
27 Correct 753 ms 54484 KB Output is correct
28 Correct 789 ms 56804 KB Output is correct
29 Correct 761 ms 55120 KB Output is correct
30 Correct 738 ms 53484 KB Output is correct
31 Correct 731 ms 53664 KB Output is correct
32 Correct 840 ms 65360 KB Output is correct
33 Correct 677 ms 53616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5068 KB Output is correct
2 Correct 2 ms 5068 KB Output is correct
3 Correct 2 ms 5068 KB Output is correct
4 Correct 3 ms 5068 KB Output is correct
5 Correct 3 ms 5068 KB Output is correct
6 Correct 2 ms 5068 KB Output is correct
7 Correct 2 ms 5016 KB Output is correct
8 Correct 3 ms 5068 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5068 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 829 ms 54640 KB Output is correct
14 Correct 770 ms 69408 KB Output is correct
15 Correct 736 ms 53208 KB Output is correct
16 Correct 784 ms 54652 KB Output is correct
17 Correct 772 ms 57072 KB Output is correct
18 Correct 739 ms 53752 KB Output is correct
19 Correct 852 ms 69892 KB Output is correct
20 Correct 669 ms 53660 KB Output is correct
21 Correct 3 ms 5020 KB Output is correct
22 Correct 760 ms 54584 KB Output is correct
23 Correct 681 ms 71908 KB Output is correct
24 Correct 708 ms 53240 KB Output is correct
25 Correct 741 ms 54644 KB Output is correct
26 Correct 785 ms 57300 KB Output is correct
27 Correct 669 ms 53440 KB Output is correct
28 Correct 830 ms 64692 KB Output is correct
29 Correct 671 ms 53216 KB Output is correct
30 Correct 3 ms 5020 KB Output is correct
31 Correct 9 ms 5580 KB Output is correct
32 Correct 9 ms 5596 KB Output is correct
33 Correct 9 ms 5536 KB Output is correct
34 Correct 10 ms 5444 KB Output is correct
35 Correct 10 ms 5572 KB Output is correct
36 Correct 9 ms 5540 KB Output is correct
37 Correct 9 ms 5452 KB Output is correct
38 Correct 10 ms 5452 KB Output is correct
39 Correct 10 ms 5548 KB Output is correct
40 Correct 11 ms 5532 KB Output is correct
41 Correct 11 ms 5452 KB Output is correct
42 Correct 9 ms 5580 KB Output is correct
43 Correct 10 ms 5708 KB Output is correct
44 Correct 9 ms 5544 KB Output is correct
45 Correct 2 ms 5068 KB Output is correct
46 Correct 767 ms 54664 KB Output is correct
47 Correct 821 ms 72108 KB Output is correct
48 Correct 741 ms 53276 KB Output is correct
49 Correct 800 ms 54940 KB Output is correct
50 Correct 748 ms 53700 KB Output is correct
51 Correct 777 ms 54952 KB Output is correct
52 Correct 764 ms 53920 KB Output is correct
53 Correct 753 ms 54484 KB Output is correct
54 Correct 789 ms 56804 KB Output is correct
55 Correct 761 ms 55120 KB Output is correct
56 Correct 738 ms 53484 KB Output is correct
57 Correct 731 ms 53664 KB Output is correct
58 Correct 840 ms 65360 KB Output is correct
59 Correct 677 ms 53616 KB Output is correct
60 Correct 3 ms 5068 KB Output is correct
61 Correct 1020 ms 57232 KB Output is correct
62 Correct 1008 ms 71304 KB Output is correct
63 Correct 1002 ms 56004 KB Output is correct
64 Correct 1002 ms 57648 KB Output is correct
65 Correct 1048 ms 56084 KB Output is correct
66 Correct 1004 ms 57728 KB Output is correct
67 Correct 1000 ms 56180 KB Output is correct
68 Correct 1014 ms 57364 KB Output is correct
69 Correct 1037 ms 59412 KB Output is correct
70 Correct 1048 ms 57852 KB Output is correct
71 Correct 995 ms 56324 KB Output is correct
72 Correct 966 ms 57064 KB Output is correct
73 Correct 1085 ms 66072 KB Output is correct
74 Correct 914 ms 57700 KB Output is correct