답안 #996268

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
996268 2024-06-10T10:29:13 Z Dzadzo Designated Cities (JOI19_designated_cities) C++17
100 / 100
403 ms 74944 KB
#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF LLONG_MAX
#define MOD 1000000007
#define MAXN 200000
using namespace std;
int t[4*MAXN+1];
int lazy[4*MAXN+1];
void push(int v){
	t[2*v]+=lazy[v];
	t[2*v+1]+=lazy[v];
	lazy[2*v]+=lazy[v];
	lazy[2*v+1]+=lazy[v];
	lazy[v]=0;
}
void up(int v,int tl,int tr,int l,int r,int delta){
	if (l>r)return;
	if (tl==l && tr==r){
		t[v]+=delta;
		lazy[v]+=delta;
	}else{
		push(v);
		int tm=(tl+tr)/2;
		up(v*2,tl,tm,l,min(r,tm),delta);
		up(v*2+1,tm+1,tr,max(l,tm+1),r,delta);
		t[v]=max(t[v*2],t[v*2+1]);
	}
}
vector<vector<pair<int,pii>>> adj(MAXN+1);
int dp[MAXN+1];
int s[MAXN+1];
int timer=0;
int ind[MAXN+1];
int n;
void DFS(int v,int p,int depth){
	s[v]=1;
	ind[v]=++timer;
	up(1,1,n,ind[v],ind[v],depth);
	for (auto X:adj[v]){
		int to=X.F;
		int x=X.S.F;
		int y=X.S.S;
		if (to!=p){
			DFS(to,v,depth+x);
			dp[v]+=dp[to]+y;
			s[v]+=s[to];
		}
	}
}
pii best={0,0};
int ans=0;
void reroot(int v,int p){
	best=max(best,{dp[v]+t[1],v});
	ans=max(ans,dp[v]);
	for (auto X:adj[v]){
		int to=X.F;
		int x=X.S.F;
		int y=X.S.S;
		if (to==p)continue;
		int dpv=dp[v],dpto=dp[to];
		dp[v]-=dp[to]+y;
		dp[to]+=dp[v]+x;
		up(1,1,n,1,n,y);
		up(1,1,n,ind[to],ind[to]+s[to]-1,-x-y);
		reroot(to,v);
		dp[v]=dpv;
		dp[to]=dpto;
		up(1,1,n,1,n,-y);
		up(1,1,n,ind[to],ind[to]+s[to]-1,x+y);
	}
}
pii val[MAXN+1];
int a[MAXN+1];
int root;
void dfs(int v,int p){
 
	val[v]={0,v};
	for (auto X:adj[v]){
		int to=X.F;
		int x=X.S.F;
		int y=X.S.S;
		if (to==p)continue;
		dfs(to,v);
		val[v]=max(val[v],{val[to].F+x,val[to].S});
	}
	for (auto X:adj[v]){
		int to=X.F;
		int x=X.S.F;
		int y=X.S.S;
		if (to==p)continue;
		if (val[to].S!=val[v].S || v==root)a[val[to].S]=val[to].F+x;
	}	
}
signed main(){	
	ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
	cin>>n;
	int cost=0;
	for (int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		int x,y;
		cin>>x>>y;
		adj[u].pb({v,{x,y}});
		adj[v].pb({u,{y,x}});
		cost+=y+x;
	}
	DFS(1,0,0);
	reroot(1,0);
	root=best.S;
	dfs(root,0);
	int q;
	cin>>q;
	vi V={INF};
	for (int i=1;i<=n;i++)V.pb(a[i]);
	sort(V.begin(),V.end());
	reverse(V.begin(),V.end());
	vi p(n+1);
	for (int i=1;i<=n;i++)p[i]=p[i-1]+V[i];
	while (q--){
		int e;
		cin>>e;
		if (e==1)cout<<cost-ans<<'\n';else{
			cout<<cost-p[e-1]-best.F+p[1]<<'\n';
		}
	}
}

Compilation message

designated_cities.cpp: In function 'void dfs(long long int, long long int)':
designated_cities.cpp:93:7: warning: unused variable 'y' [-Wunused-variable]
   93 |   int y=X.S.S;
      |       ^
designated_cities.cpp:101:7: warning: unused variable 'y' [-Wunused-variable]
  101 |   int y=X.S.S;
      |       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13144 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 3 ms 13144 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 298 ms 45568 KB Output is correct
3 Correct 356 ms 70340 KB Output is correct
4 Correct 250 ms 45008 KB Output is correct
5 Correct 281 ms 46196 KB Output is correct
6 Correct 271 ms 48588 KB Output is correct
7 Correct 292 ms 44768 KB Output is correct
8 Correct 365 ms 69324 KB Output is correct
9 Correct 226 ms 44280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13148 KB Output is correct
2 Correct 249 ms 45308 KB Output is correct
3 Correct 311 ms 70344 KB Output is correct
4 Correct 262 ms 45000 KB Output is correct
5 Correct 267 ms 46080 KB Output is correct
6 Correct 289 ms 49536 KB Output is correct
7 Correct 197 ms 44792 KB Output is correct
8 Correct 355 ms 61636 KB Output is correct
9 Correct 199 ms 44280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13144 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 3 ms 13144 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 3 ms 13148 KB Output is correct
13 Correct 4 ms 13640 KB Output is correct
14 Correct 5 ms 13764 KB Output is correct
15 Correct 4 ms 13660 KB Output is correct
16 Correct 4 ms 13660 KB Output is correct
17 Correct 4 ms 13660 KB Output is correct
18 Correct 4 ms 13660 KB Output is correct
19 Correct 4 ms 13656 KB Output is correct
20 Correct 4 ms 13660 KB Output is correct
21 Correct 5 ms 13652 KB Output is correct
22 Correct 6 ms 13660 KB Output is correct
23 Correct 4 ms 13912 KB Output is correct
24 Correct 4 ms 13656 KB Output is correct
25 Correct 5 ms 13660 KB Output is correct
26 Correct 5 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 298 ms 45568 KB Output is correct
3 Correct 356 ms 70340 KB Output is correct
4 Correct 250 ms 45008 KB Output is correct
5 Correct 281 ms 46196 KB Output is correct
6 Correct 271 ms 48588 KB Output is correct
7 Correct 292 ms 44768 KB Output is correct
8 Correct 365 ms 69324 KB Output is correct
9 Correct 226 ms 44280 KB Output is correct
10 Correct 3 ms 13148 KB Output is correct
11 Correct 249 ms 45308 KB Output is correct
12 Correct 311 ms 70344 KB Output is correct
13 Correct 262 ms 45000 KB Output is correct
14 Correct 267 ms 46080 KB Output is correct
15 Correct 289 ms 49536 KB Output is correct
16 Correct 197 ms 44792 KB Output is correct
17 Correct 355 ms 61636 KB Output is correct
18 Correct 199 ms 44280 KB Output is correct
19 Correct 2 ms 13148 KB Output is correct
20 Correct 261 ms 45396 KB Output is correct
21 Correct 371 ms 73412 KB Output is correct
22 Correct 240 ms 47128 KB Output is correct
23 Correct 317 ms 48844 KB Output is correct
24 Correct 240 ms 47524 KB Output is correct
25 Correct 252 ms 48840 KB Output is correct
26 Correct 294 ms 47528 KB Output is correct
27 Correct 272 ms 48768 KB Output is correct
28 Correct 294 ms 52168 KB Output is correct
29 Correct 266 ms 49204 KB Output is correct
30 Correct 235 ms 48132 KB Output is correct
31 Correct 245 ms 47464 KB Output is correct
32 Correct 328 ms 65216 KB Output is correct
33 Correct 197 ms 48364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 13148 KB Output is correct
4 Correct 2 ms 13148 KB Output is correct
5 Correct 3 ms 13144 KB Output is correct
6 Correct 2 ms 13148 KB Output is correct
7 Correct 2 ms 13148 KB Output is correct
8 Correct 3 ms 13148 KB Output is correct
9 Correct 2 ms 13148 KB Output is correct
10 Correct 3 ms 13144 KB Output is correct
11 Correct 3 ms 13148 KB Output is correct
12 Correct 3 ms 13144 KB Output is correct
13 Correct 298 ms 45568 KB Output is correct
14 Correct 356 ms 70340 KB Output is correct
15 Correct 250 ms 45008 KB Output is correct
16 Correct 281 ms 46196 KB Output is correct
17 Correct 271 ms 48588 KB Output is correct
18 Correct 292 ms 44768 KB Output is correct
19 Correct 365 ms 69324 KB Output is correct
20 Correct 226 ms 44280 KB Output is correct
21 Correct 3 ms 13148 KB Output is correct
22 Correct 249 ms 45308 KB Output is correct
23 Correct 311 ms 70344 KB Output is correct
24 Correct 262 ms 45000 KB Output is correct
25 Correct 267 ms 46080 KB Output is correct
26 Correct 289 ms 49536 KB Output is correct
27 Correct 197 ms 44792 KB Output is correct
28 Correct 355 ms 61636 KB Output is correct
29 Correct 199 ms 44280 KB Output is correct
30 Correct 3 ms 13148 KB Output is correct
31 Correct 4 ms 13640 KB Output is correct
32 Correct 5 ms 13764 KB Output is correct
33 Correct 4 ms 13660 KB Output is correct
34 Correct 4 ms 13660 KB Output is correct
35 Correct 4 ms 13660 KB Output is correct
36 Correct 4 ms 13660 KB Output is correct
37 Correct 4 ms 13656 KB Output is correct
38 Correct 4 ms 13660 KB Output is correct
39 Correct 5 ms 13652 KB Output is correct
40 Correct 6 ms 13660 KB Output is correct
41 Correct 4 ms 13912 KB Output is correct
42 Correct 4 ms 13656 KB Output is correct
43 Correct 5 ms 13660 KB Output is correct
44 Correct 5 ms 13660 KB Output is correct
45 Correct 2 ms 13148 KB Output is correct
46 Correct 261 ms 45396 KB Output is correct
47 Correct 371 ms 73412 KB Output is correct
48 Correct 240 ms 47128 KB Output is correct
49 Correct 317 ms 48844 KB Output is correct
50 Correct 240 ms 47524 KB Output is correct
51 Correct 252 ms 48840 KB Output is correct
52 Correct 294 ms 47528 KB Output is correct
53 Correct 272 ms 48768 KB Output is correct
54 Correct 294 ms 52168 KB Output is correct
55 Correct 266 ms 49204 KB Output is correct
56 Correct 235 ms 48132 KB Output is correct
57 Correct 245 ms 47464 KB Output is correct
58 Correct 328 ms 65216 KB Output is correct
59 Correct 197 ms 48364 KB Output is correct
60 Correct 2 ms 13148 KB Output is correct
61 Correct 267 ms 51244 KB Output is correct
62 Correct 403 ms 74944 KB Output is correct
63 Correct 268 ms 49856 KB Output is correct
64 Correct 285 ms 51544 KB Output is correct
65 Correct 264 ms 49988 KB Output is correct
66 Correct 278 ms 51580 KB Output is correct
67 Correct 278 ms 49948 KB Output is correct
68 Correct 283 ms 51640 KB Output is correct
69 Correct 305 ms 54684 KB Output is correct
70 Correct 289 ms 51932 KB Output is correct
71 Correct 284 ms 50824 KB Output is correct
72 Correct 253 ms 50996 KB Output is correct
73 Correct 386 ms 67524 KB Output is correct
74 Correct 208 ms 51704 KB Output is correct