답안 #996593

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
996593 2024-06-10T21:34:30 Z NintsiChkhaidze Designated Cities (JOI19_designated_cities) C++17
100 / 100
867 ms 122436 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define pii pair <int,int>
#define left h*2,l,(l + r)/2
#define right h*2+1,(l + r)/2 + 1,r
#define int ll
using namespace std;

const int N = 2e5 + 3,inf = 1e18;
vector <pii> v[N];
int dp[N],ans[N],tot,n;
pii t[4*N];
int lz[4*N],tin[N],tout[N],timer,rev[N],A,B;
pii parent[N];
unordered_map <int,pii> mp[N];

void dfs1(int x,int par,int D){
	tin[x] = ++timer;
	rev[timer] = x;
	dp[x] = D;
	for (auto [to,w]: v[x]){
		if (to == par) continue;
		tot += w;
		parent[to] = {x,w};
		dfs1(to,x,w + D);
	}
	tout[x] = timer;
}

void build(int h,int l,int r){
	lz[h] = 0;
	if (l == r){
		t[h] = {dp[rev[l]],rev[l]};
		return;
	}
	build(left);
	build(right);
	t[h] = max(t[h*2],t[h*2+1]);
}

void push(int h){
	if (lz[h] == 0) return;
	lz[h*2] += lz[h];
	lz[h*2 + 1] += lz[h];
	t[h*2].f += lz[h];
	t[h*2 + 1].f += lz[h];
	lz[h] = 0;
}

void upd(int h,int l,int r,int L,int R,int val){
	if (r < L || R < l) return;
	if (L <= l && r <= R){
		t[h].f += val;
		lz[h] += val;
		return;
	}

	push(h);
	upd(left,L,R,val);
	upd(right,L,R,val);
	t[h] = max(t[h*2],t[h*2+1]);
}

void reroot(int x,int par,int total){

	ans[1] = min(ans[1],total);
	int y = t[1].s,val = t[1].f;
	if (total - val < ans[2]) {
		ans[2] = total - val;
		A = x,B = y;
	}

	for (auto [to,w]: v[x]){
		if (to == par) continue;
		int w2 = mp[x][to].f;
		if (mp[x][to].f == w) w2 = mp[x][to].s;

		upd(1,1,n,tin[to],tout[to],-w);
		upd(1,1,n,1,tin[to] - 1,+w2);
		upd(1,1,n,tout[to] + 1,n,+w2);
		total += w2 - w;

		reroot(to,x,total);

		upd(1,1,n,tin[to],tout[to],+w);
		upd(1,1,n,1,tin[to] - 1,-w2);
		upd(1,1,n,tout[to] + 1,n,-w2);
		total -= w2 - w;
	}
}
signed main (){
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
	
	cin>>n;

	for (int i = 1; i < n; i++){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		v[a].pb({b,c});
		v[b].pb({a,d});
		mp[a][b] = mp[b][a] = {c,d};
	}

	for (int i = 1; i <= n; i++)
		ans[i] = inf;

	timer=tot=0;
	dfs1(1,1,0);
	build(1,1,n);
	reroot(1,1,tot);

	for (int i = 1; i <= n; i++){
		if (i != A && i != B) continue;
		tot = timer = 0;
		parent[i] = {0,0};
		dfs1(i,i,0);
		build(1,1,n);
		ans[1] = min(ans[1],tot);

		for (int j = 2; j <= n; j++){
			int val = t[1].f,x = t[1].s;
			tot -= val;
			ans[j] = min(ans[j],tot);
			
			while (val > 0){
				upd(1,1,n,tin[x],tout[x],-parent[x].s);
				val -= parent[x].s;
				x = parent[x].f;
			}
		}
	}

	int q;
	cin>>q;
	while (q--){
		int x;
		cin>>x;
		cout<<ans[x]<<"\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31320 KB Output is correct
2 Correct 4 ms 31324 KB Output is correct
3 Correct 4 ms 31324 KB Output is correct
4 Correct 5 ms 31324 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31320 KB Output is correct
7 Correct 4 ms 31324 KB Output is correct
8 Correct 5 ms 31320 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 4 ms 31324 KB Output is correct
11 Correct 5 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31324 KB Output is correct
2 Correct 745 ms 94672 KB Output is correct
3 Correct 746 ms 118756 KB Output is correct
4 Correct 753 ms 97504 KB Output is correct
5 Correct 729 ms 98340 KB Output is correct
6 Correct 743 ms 100948 KB Output is correct
7 Correct 706 ms 98864 KB Output is correct
8 Correct 867 ms 119268 KB Output is correct
9 Correct 627 ms 100072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31324 KB Output is correct
2 Correct 739 ms 94740 KB Output is correct
3 Correct 778 ms 122196 KB Output is correct
4 Correct 737 ms 97620 KB Output is correct
5 Correct 800 ms 98596 KB Output is correct
6 Correct 816 ms 101456 KB Output is correct
7 Correct 674 ms 99808 KB Output is correct
8 Correct 858 ms 111440 KB Output is correct
9 Correct 640 ms 100524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31320 KB Output is correct
2 Correct 4 ms 31324 KB Output is correct
3 Correct 4 ms 31324 KB Output is correct
4 Correct 5 ms 31324 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31320 KB Output is correct
7 Correct 4 ms 31324 KB Output is correct
8 Correct 5 ms 31320 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 4 ms 31324 KB Output is correct
11 Correct 5 ms 31324 KB Output is correct
12 Correct 4 ms 31320 KB Output is correct
13 Correct 9 ms 31836 KB Output is correct
14 Correct 9 ms 32092 KB Output is correct
15 Correct 9 ms 32008 KB Output is correct
16 Correct 8 ms 31836 KB Output is correct
17 Correct 8 ms 31836 KB Output is correct
18 Correct 9 ms 31836 KB Output is correct
19 Correct 9 ms 31832 KB Output is correct
20 Correct 10 ms 31836 KB Output is correct
21 Correct 9 ms 31836 KB Output is correct
22 Correct 9 ms 31836 KB Output is correct
23 Correct 11 ms 31836 KB Output is correct
24 Correct 8 ms 31948 KB Output is correct
25 Correct 9 ms 32088 KB Output is correct
26 Correct 8 ms 31836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 31324 KB Output is correct
2 Correct 745 ms 94672 KB Output is correct
3 Correct 746 ms 118756 KB Output is correct
4 Correct 753 ms 97504 KB Output is correct
5 Correct 729 ms 98340 KB Output is correct
6 Correct 743 ms 100948 KB Output is correct
7 Correct 706 ms 98864 KB Output is correct
8 Correct 867 ms 119268 KB Output is correct
9 Correct 627 ms 100072 KB Output is correct
10 Correct 4 ms 31324 KB Output is correct
11 Correct 739 ms 94740 KB Output is correct
12 Correct 778 ms 122196 KB Output is correct
13 Correct 737 ms 97620 KB Output is correct
14 Correct 800 ms 98596 KB Output is correct
15 Correct 816 ms 101456 KB Output is correct
16 Correct 674 ms 99808 KB Output is correct
17 Correct 858 ms 111440 KB Output is correct
18 Correct 640 ms 100524 KB Output is correct
19 Correct 5 ms 31320 KB Output is correct
20 Correct 747 ms 97976 KB Output is correct
21 Correct 808 ms 122436 KB Output is correct
22 Correct 780 ms 97616 KB Output is correct
23 Correct 722 ms 98328 KB Output is correct
24 Correct 748 ms 97784 KB Output is correct
25 Correct 730 ms 98384 KB Output is correct
26 Correct 777 ms 97708 KB Output is correct
27 Correct 747 ms 98336 KB Output is correct
28 Correct 792 ms 100752 KB Output is correct
29 Correct 755 ms 98640 KB Output is correct
30 Correct 716 ms 97872 KB Output is correct
31 Correct 717 ms 98988 KB Output is correct
32 Correct 802 ms 112212 KB Output is correct
33 Correct 685 ms 100064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 31320 KB Output is correct
2 Correct 4 ms 31324 KB Output is correct
3 Correct 4 ms 31324 KB Output is correct
4 Correct 5 ms 31324 KB Output is correct
5 Correct 5 ms 31324 KB Output is correct
6 Correct 5 ms 31320 KB Output is correct
7 Correct 4 ms 31324 KB Output is correct
8 Correct 5 ms 31320 KB Output is correct
9 Correct 5 ms 31324 KB Output is correct
10 Correct 4 ms 31324 KB Output is correct
11 Correct 5 ms 31324 KB Output is correct
12 Correct 4 ms 31324 KB Output is correct
13 Correct 745 ms 94672 KB Output is correct
14 Correct 746 ms 118756 KB Output is correct
15 Correct 753 ms 97504 KB Output is correct
16 Correct 729 ms 98340 KB Output is correct
17 Correct 743 ms 100948 KB Output is correct
18 Correct 706 ms 98864 KB Output is correct
19 Correct 867 ms 119268 KB Output is correct
20 Correct 627 ms 100072 KB Output is correct
21 Correct 4 ms 31324 KB Output is correct
22 Correct 739 ms 94740 KB Output is correct
23 Correct 778 ms 122196 KB Output is correct
24 Correct 737 ms 97620 KB Output is correct
25 Correct 800 ms 98596 KB Output is correct
26 Correct 816 ms 101456 KB Output is correct
27 Correct 674 ms 99808 KB Output is correct
28 Correct 858 ms 111440 KB Output is correct
29 Correct 640 ms 100524 KB Output is correct
30 Correct 4 ms 31320 KB Output is correct
31 Correct 9 ms 31836 KB Output is correct
32 Correct 9 ms 32092 KB Output is correct
33 Correct 9 ms 32008 KB Output is correct
34 Correct 8 ms 31836 KB Output is correct
35 Correct 8 ms 31836 KB Output is correct
36 Correct 9 ms 31836 KB Output is correct
37 Correct 9 ms 31832 KB Output is correct
38 Correct 10 ms 31836 KB Output is correct
39 Correct 9 ms 31836 KB Output is correct
40 Correct 9 ms 31836 KB Output is correct
41 Correct 11 ms 31836 KB Output is correct
42 Correct 8 ms 31948 KB Output is correct
43 Correct 9 ms 32088 KB Output is correct
44 Correct 8 ms 31836 KB Output is correct
45 Correct 5 ms 31320 KB Output is correct
46 Correct 747 ms 97976 KB Output is correct
47 Correct 808 ms 122436 KB Output is correct
48 Correct 780 ms 97616 KB Output is correct
49 Correct 722 ms 98328 KB Output is correct
50 Correct 748 ms 97784 KB Output is correct
51 Correct 730 ms 98384 KB Output is correct
52 Correct 777 ms 97708 KB Output is correct
53 Correct 747 ms 98336 KB Output is correct
54 Correct 792 ms 100752 KB Output is correct
55 Correct 755 ms 98640 KB Output is correct
56 Correct 716 ms 97872 KB Output is correct
57 Correct 717 ms 98988 KB Output is correct
58 Correct 802 ms 112212 KB Output is correct
59 Correct 685 ms 100064 KB Output is correct
60 Correct 5 ms 31324 KB Output is correct
61 Correct 807 ms 100020 KB Output is correct
62 Correct 748 ms 120144 KB Output is correct
63 Correct 756 ms 99408 KB Output is correct
64 Correct 749 ms 100432 KB Output is correct
65 Correct 754 ms 99636 KB Output is correct
66 Correct 767 ms 100436 KB Output is correct
67 Correct 786 ms 99792 KB Output is correct
68 Correct 764 ms 100516 KB Output is correct
69 Correct 761 ms 102756 KB Output is correct
70 Correct 747 ms 100688 KB Output is correct
71 Correct 750 ms 99784 KB Output is correct
72 Correct 770 ms 101424 KB Output is correct
73 Correct 826 ms 112208 KB Output is correct
74 Correct 653 ms 103648 KB Output is correct