Submission #126476

# Submission time Handle Problem Language Result Execution time Memory
126476 2019-07-07T20:07:35 Z TadijaSebez Designated Cities (JOI19_designated_cities) C++11
100 / 100
725 ms 50724 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N=200050;
vector<pair<int,int>> E[N];
ll sum[N],all_up;
int up[N],down[N];
ll dep[N];
int lid[N],rid[N],tid,nod[N],par[N],my[N],rt,n;
ll mxd[N],sol[N];
void DFS(int u, int p)
{
	for(auto e:E[u])
	{
		int v=e.first,w=e.second;
		if(v==p) up[u]=w,all_up+=w;
	}
	if(p) sum[u]=sum[p]+down[u]-up[u];
	mxd[u]=0;
	for(auto e:E[u])
	{
		int v=e.first,w=e.second;
		if(v!=p)
		{
			down[v]=w;
			dep[v]=dep[u]+w;
			DFS(v,u);
			mxd[u]=max(mxd[u],mxd[v]+w);
		}
	}
}
void Solve(int u, int p, ll hi)
{
	ll fir=0,sec=0;
	int t=-1;
	sol[u]=max(hi,mxd[u])+sum[u];
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first,w=e.second;
		if(fir<mxd[v]+w) sec=fir,fir=mxd[v]+w,t=v;
		else if(sec<mxd[v]+w) sec=mxd[v]+w;
	}
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first,w=e.second;
		ll best=hi;
		if(t==v) best=max(best,sec);
		else best=max(best,fir);
		Solve(v,u,best+up[v]);
	}
}
void DFS2(int u, int p)
{
	lid[u]=++tid;
	nod[tid]=u;
	par[u]=p;
	my[u]=u;
	for(auto e:E[u]) if(e.first!=p)
	{
		int v=e.first;
		dep[v]=dep[u]+e.second;
		DFS2(v,u);
		if(dep[my[u]]<dep[my[v]]) my[u]=my[v];
	}
	rid[u]=tid;
}
const int M=2*N;
int ls[M],rs[M],tsz,root,id[M];
ll mx[M],lzy[M];
void Build(int &c, int ss, int se)
{
	c=++tsz;
	if(ss==se){ mx[c]=dep[nod[ss]];id[c]=nod[ss];return;}
	int mid=ss+se>>1;
	Build(ls[c],ss,mid);
	Build(rs[c],mid+1,se);
	mx[c]=max(mx[ls[c]],mx[rs[c]]);
	if(mx[c]==mx[ls[c]]) id[c]=id[ls[c]];
	else id[c]=id[rs[c]];
}
void Add(int c, int ss, int se, int qs, int qe, ll x)
{
	if(qs>qe || qs>se || ss>qe) return;
	if(qs<=ss && qe>=se){ mx[c]+=x;lzy[c]+=x;return;}
	int mid=ss+se>>1;
	Add(ls[c],ss,mid,qs,qe,x);
	Add(rs[c],mid+1,se,qs,qe,x);
	mx[c]=max(mx[ls[c]],mx[rs[c]]);
	if(mx[c]==mx[ls[c]]) id[c]=id[ls[c]];
	else id[c]=id[rs[c]];
	mx[c]+=lzy[c];
}
bool was[N];
ll ans[N],cur;
void Add(int x)
{
	for(;!was[x];x=par[x])
	{
		was[x]=1;
		Add(root,1,n,lid[x],rid[x],-dep[x]+dep[par[x]]);
		cur+=dep[x]-dep[par[x]];
	}
}
int main()
{
	int q,u,v,c,d;
	scanf("%i",&n);
	ll z=0;
	for(int i=1;i<n;i++)
	{
		scanf("%i %i %i %i",&u,&v,&c,&d);
		E[u].pb({v,c});
		E[v].pb({u,d});
		z+=c;
		z+=d;
	}
	DFS(1,0);
	rt=1;
	for(int i=1;i<=n;i++)
	{
		sum[i]+=all_up;
		if(sum[i]>sum[rt]) rt=i;
	}
	ans[1]=sum[rt];
	Solve(1,0,0);
	for(int i=1;i<=n;i++) if(sol[i]>sol[rt]) rt=i;
	for(int i=1;i<=n;i++) dep[i]=0;
	DFS2(rt,0);
	Build(root,1,n);
	cur=sum[rt];
	was[rt]=1;
	for(int i=2;i<=n;i++)
	{
		if(id[root]!=rt) Add(id[root]);
		ans[i]=cur;
	}
	scanf("%i",&q);
	while(q--)
	{
		int x;
		scanf("%i",&x);
		printf("%lld\n",z-ans[x]);
	}
	return 0;
}

Compilation message

designated_cities.cpp: In function 'void Solve(int, int, long long int)':
designated_cities.cpp:46:17: warning: unused variable 'w' [-Wunused-variable]
   int v=e.first,w=e.second;
                 ^
designated_cities.cpp: In function 'void Build(int&, int, int)':
designated_cities.cpp:75:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
designated_cities.cpp: In function 'void Add(int, int, int, int, int, long long int)':
designated_cities.cpp:86:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=ss+se>>1;
          ~~^~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:108:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&n);
  ~~~~~^~~~~~~~~
designated_cities.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i %i %i",&u,&v,&c,&d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i",&q);
  ~~~~~^~~~~~~~~
designated_cities.cpp:142:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i",&x);
   ~~~~~^~~~~~~~~
# 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 6 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 5240 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 7 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 453 ms 36892 KB Output is correct
3 Correct 458 ms 48608 KB Output is correct
4 Correct 463 ms 36856 KB Output is correct
5 Correct 554 ms 36928 KB Output is correct
6 Correct 532 ms 38648 KB Output is correct
7 Correct 423 ms 36880 KB Output is correct
8 Correct 643 ms 49008 KB Output is correct
9 Correct 473 ms 37476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 478 ms 36780 KB Output is correct
3 Correct 436 ms 50672 KB Output is correct
4 Correct 414 ms 36856 KB Output is correct
5 Correct 447 ms 36900 KB Output is correct
6 Correct 518 ms 38908 KB Output is correct
7 Correct 472 ms 37164 KB Output is correct
8 Correct 462 ms 44664 KB Output is correct
9 Correct 395 ms 37376 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 6 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 5240 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 6 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 9 ms 5496 KB Output is correct
14 Correct 10 ms 5496 KB Output is correct
15 Correct 9 ms 5496 KB Output is correct
16 Correct 9 ms 5500 KB Output is correct
17 Correct 9 ms 5496 KB Output is correct
18 Correct 9 ms 5496 KB Output is correct
19 Correct 8 ms 5496 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 11 ms 5496 KB Output is correct
22 Correct 9 ms 5496 KB Output is correct
23 Correct 9 ms 5496 KB Output is correct
24 Correct 9 ms 5496 KB Output is correct
25 Correct 9 ms 5624 KB Output is correct
26 Correct 10 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 453 ms 36892 KB Output is correct
3 Correct 458 ms 48608 KB Output is correct
4 Correct 463 ms 36856 KB Output is correct
5 Correct 554 ms 36928 KB Output is correct
6 Correct 532 ms 38648 KB Output is correct
7 Correct 423 ms 36880 KB Output is correct
8 Correct 643 ms 49008 KB Output is correct
9 Correct 473 ms 37476 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 478 ms 36780 KB Output is correct
12 Correct 436 ms 50672 KB Output is correct
13 Correct 414 ms 36856 KB Output is correct
14 Correct 447 ms 36900 KB Output is correct
15 Correct 518 ms 38908 KB Output is correct
16 Correct 472 ms 37164 KB Output is correct
17 Correct 462 ms 44664 KB Output is correct
18 Correct 395 ms 37376 KB Output is correct
19 Correct 6 ms 5112 KB Output is correct
20 Correct 482 ms 36732 KB Output is correct
21 Correct 439 ms 50724 KB Output is correct
22 Correct 429 ms 36856 KB Output is correct
23 Correct 666 ms 36948 KB Output is correct
24 Correct 445 ms 36856 KB Output is correct
25 Correct 487 ms 37112 KB Output is correct
26 Correct 558 ms 36860 KB Output is correct
27 Correct 497 ms 36824 KB Output is correct
28 Correct 545 ms 38520 KB Output is correct
29 Correct 481 ms 36832 KB Output is correct
30 Correct 476 ms 36976 KB Output is correct
31 Correct 419 ms 37088 KB Output is correct
32 Correct 504 ms 45304 KB Output is correct
33 Correct 521 ms 37432 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 6 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 5240 KB Output is correct
8 Correct 7 ms 5112 KB Output is correct
9 Correct 7 ms 5112 KB Output is correct
10 Correct 6 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 453 ms 36892 KB Output is correct
14 Correct 458 ms 48608 KB Output is correct
15 Correct 463 ms 36856 KB Output is correct
16 Correct 554 ms 36928 KB Output is correct
17 Correct 532 ms 38648 KB Output is correct
18 Correct 423 ms 36880 KB Output is correct
19 Correct 643 ms 49008 KB Output is correct
20 Correct 473 ms 37476 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 478 ms 36780 KB Output is correct
23 Correct 436 ms 50672 KB Output is correct
24 Correct 414 ms 36856 KB Output is correct
25 Correct 447 ms 36900 KB Output is correct
26 Correct 518 ms 38908 KB Output is correct
27 Correct 472 ms 37164 KB Output is correct
28 Correct 462 ms 44664 KB Output is correct
29 Correct 395 ms 37376 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 9 ms 5496 KB Output is correct
32 Correct 10 ms 5496 KB Output is correct
33 Correct 9 ms 5496 KB Output is correct
34 Correct 9 ms 5500 KB Output is correct
35 Correct 9 ms 5496 KB Output is correct
36 Correct 9 ms 5496 KB Output is correct
37 Correct 8 ms 5496 KB Output is correct
38 Correct 9 ms 5496 KB Output is correct
39 Correct 11 ms 5496 KB Output is correct
40 Correct 9 ms 5496 KB Output is correct
41 Correct 9 ms 5496 KB Output is correct
42 Correct 9 ms 5496 KB Output is correct
43 Correct 9 ms 5624 KB Output is correct
44 Correct 10 ms 5496 KB Output is correct
45 Correct 6 ms 5112 KB Output is correct
46 Correct 482 ms 36732 KB Output is correct
47 Correct 439 ms 50724 KB Output is correct
48 Correct 429 ms 36856 KB Output is correct
49 Correct 666 ms 36948 KB Output is correct
50 Correct 445 ms 36856 KB Output is correct
51 Correct 487 ms 37112 KB Output is correct
52 Correct 558 ms 36860 KB Output is correct
53 Correct 497 ms 36824 KB Output is correct
54 Correct 545 ms 38520 KB Output is correct
55 Correct 481 ms 36832 KB Output is correct
56 Correct 476 ms 36976 KB Output is correct
57 Correct 419 ms 37088 KB Output is correct
58 Correct 504 ms 45304 KB Output is correct
59 Correct 521 ms 37432 KB Output is correct
60 Correct 7 ms 5112 KB Output is correct
61 Correct 633 ms 38236 KB Output is correct
62 Correct 483 ms 49144 KB Output is correct
63 Correct 464 ms 38456 KB Output is correct
64 Correct 540 ms 38480 KB Output is correct
65 Correct 514 ms 38008 KB Output is correct
66 Correct 725 ms 38308 KB Output is correct
67 Correct 547 ms 38080 KB Output is correct
68 Correct 515 ms 38388 KB Output is correct
69 Correct 552 ms 39844 KB Output is correct
70 Correct 533 ms 38128 KB Output is correct
71 Correct 478 ms 38564 KB Output is correct
72 Correct 511 ms 38940 KB Output is correct
73 Correct 592 ms 45768 KB Output is correct
74 Correct 504 ms 40264 KB Output is correct