답안 #102271

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
102271 2019-03-24T04:47:02 Z ToMo01 Designated Cities (JOI19_designated_cities) C++17
100 / 100
915 ms 67868 KB
/*input
15
14 5 12 7
14 12 6 5
14 10 14 16
9 14 16 12
13 7 4 15
1 3 8 1
6 7 15 13
15 4 4 6
9 1 12 6
13 1 7 6
13 4 5 15
2 6 11 19
8 4 12 7
13 11 14 5
3
3
6
7	
*/
#include <bits/stdc++.h>
using namespace std;

int read() {
	int number = 0, c = getchar();
	for(; !(c > 47 && c < 58); c = getchar());
	for(; (c > 47 && c < 58); c = getchar()) number = (number << 3) + (number << 1) + c - 48;
	return number;
}

const int N = 200005;

struct Edge {
	int to, f, b;
	Edge(int _to = 0, int _f = 0, int _b = 0):
		to(_to), f(_f), b(_b) {}
} edge[400000];

long long upVal[N], downVal[N], revUpVal[N], revDownVal[N], f[N][3], h[N], tour[N];
vector<int> G[N];

struct SegmentTree {
	pair<long long, int> t[N << 2];
	long long lazy[N << 2];

	SegmentTree() { 
		memset(t, 0, sizeof t);
		memset(lazy, 0, sizeof lazy);
	}

	void build(int node, int l, int r) {
		if(l == r) t[node] = make_pair(h[tour[l]], tour[l]);
		else {
			int mid = (l + r) >> 1;
			build(node << 1, l, mid);
			build(node << 1 | 1, mid + 1, r);
			t[node] = max(t[node << 1], t[node << 1 | 1]);
		}
	}

	void down(int node, int l, int r) {
		if(lazy[node] == 0) return;

		long long cur = lazy[node]; lazy[node] = 0;
		if(l != r) {
			lazy[node << 1] += cur;
			lazy[node << 1 | 1] += cur;
		}
		t[node].first += cur;
	}

	void update(int node, int l, int r, int u, int v, int val) {
		down(node, l, r);
		if(v < l || r < u) return;
		if(u <= l && r <= v) {
			lazy[node] += val;
			down(node, l, r); return;
		}
		int mid = (l + r) >> 1;
		update(node << 1, l, mid, u, v, val);
		update(node << 1 | 1, mid + 1, r, u, v, val);
		t[node] = max(t[node << 1], t[node << 1 | 1]);
	}
} seg;

void upOne(int u, int p) {
	upVal[u] = revUpVal[u] = 0;
	for(int id : G[u]) {
		int v = edge[id].to;
		if(v == p) continue;
		
		upOne(v, u);
		upVal[u] += upVal[v] + edge[id].b;
		revUpVal[u] += revUpVal[v] + edge[id].f;
	}
}

void downOne(int u, int p) {
	for(int id : G[u]) {
		int v = edge[id].to;
		if(v == p) continue;
		downVal[v] = downVal[u] + upVal[u] - upVal[v] - edge[id].b + edge[id].f;
		revDownVal[v] = revDownVal[u] + revUpVal[u] - revUpVal[v] - edge[id].f + edge[id].b;
		downOne(v, u);
	}
}

int root;
pair<long long, long long> opt[N];
long long minDis = 1e18;

void dfs(int u, int p) {
	if(G[u].size() == 1) {
		f[u][0] = f[u][1] = 0; return;
	}

	f[u][0] = 0;
	for(int id : G[u]) {
		int v = edge[id].to;
		if(v == p) continue;

		dfs(v, u);
		long long cur0 = f[u][0], cur1 = f[u][1], cur2 = f[u][2];
		f[u][0] = cur0 + f[v][0] + edge[id].f;
		f[u][1] = min(cur0 + f[v][1], cur1 + f[v][0] + edge[id].f);
		f[u][2] = min(cur1 + f[v][1], cur2 + f[v][0] + edge[id].f);
	}
	if(f[u][2] + revDownVal[u] < minDis) minDis = f[u][2] + revDownVal[u], root = u;
}

long long Ans[N];
int start[N], done[N], traversalTime, par[N], visit[N], n;

void redfs(int u) {
	if(G[u].size() == 1) opt[u] = make_pair(h[u], u);

	start[u] = ++ traversalTime;
	tour[traversalTime] = u;
	for(int id : G[u]) {
		int v = edge[id].to;
		if(v == par[u]) continue;

		Ans[2] += edge[id].f;
		h[v] = h[u] + edge[id].f;
		par[v] = u, redfs(v);

		if(u != root) opt[u] = max(opt[u], opt[v]);
			else {
				if(opt[v].first > h[opt[u].second]) opt[u].second = opt[v].second;
				if(h[opt[u].first] < h[opt[u].second]) swap(opt[u].first, opt[u].second);
			}
	}
	done[u] = traversalTime;
}

void Pick(int u) {
	for(; !visit[u]; u = par[u]) seg.update(1, 1, n, start[u], done[u], h[par[u]] - h[u]), visit[u] = 1;
}

int main() {
	n = read();
	for(int i = 1; i < n; ++ i) {
		int u = read(), v = read(), C = read(), D = read();
		edge[i << 1] = Edge(v, C, D), edge[i << 1 | 1] = Edge(u, D, C);
		G[u].emplace_back(i << 1), G[v].emplace_back(i << 1 | 1);
	}

	if(n == 2) {
		for(int q = read(); q -- > 0;) {
			int x = read();
			if(x == 2) puts("0");
				else printf("%d\n", min(edge[2].f, edge[2].b));
		}
		return 0;
	}

	for(int i = 1; i <= n; ++ i) if(G[i].size() > 1) root = i;

	upOne(root, root), downOne(root, root);
	for(int i = 1; i <= n; ++ i) Ans[1] = min(Ans[1], -(upVal[i] + downVal[i]));
	for(int i = 1; i < n; ++ i) Ans[1] += edge[i << 1].f + edge[i << 1].b;

	memset(f, 63, sizeof f), dfs(root, root);
	redfs(root), seg.build(1, 1, n);

	Ans[2] -= h[opt[root].first] + h[opt[root].second];
	for(int i = 1; i <= n; ++ i) visit[i] = (i == root);
	Pick(opt[root].first), Pick(opt[root].second);

	for(int i = 3; i <= n; ++ i) {
		if(seg.t[1].first == 0) {
			Ans[i] = 0; break;
		}
		Ans[i] = Ans[i - 1] - seg.t[1].first, Pick(seg.t[1].second);
	}
	for(int q = read(); q -- > 0;) printf("%lld\n", Ans[read()]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 28544 KB Output is correct
2 Correct 30 ms 33280 KB Output is correct
3 Correct 31 ms 33272 KB Output is correct
4 Correct 32 ms 33280 KB Output is correct
5 Correct 30 ms 33272 KB Output is correct
6 Correct 31 ms 33272 KB Output is correct
7 Correct 31 ms 33308 KB Output is correct
8 Correct 28 ms 33296 KB Output is correct
9 Correct 35 ms 33284 KB Output is correct
10 Correct 29 ms 33272 KB Output is correct
11 Correct 37 ms 33272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28544 KB Output is correct
2 Correct 708 ms 55928 KB Output is correct
3 Correct 803 ms 63764 KB Output is correct
4 Correct 656 ms 56000 KB Output is correct
5 Correct 672 ms 56024 KB Output is correct
6 Correct 708 ms 57380 KB Output is correct
7 Correct 570 ms 56532 KB Output is correct
8 Correct 901 ms 65628 KB Output is correct
9 Correct 451 ms 57632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 28544 KB Output is correct
2 Correct 712 ms 56028 KB Output is correct
3 Correct 795 ms 63352 KB Output is correct
4 Correct 689 ms 55928 KB Output is correct
5 Correct 670 ms 56052 KB Output is correct
6 Correct 735 ms 57440 KB Output is correct
7 Correct 441 ms 57284 KB Output is correct
8 Correct 812 ms 64328 KB Output is correct
9 Correct 465 ms 57556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 28544 KB Output is correct
2 Correct 30 ms 33280 KB Output is correct
3 Correct 31 ms 33272 KB Output is correct
4 Correct 32 ms 33280 KB Output is correct
5 Correct 30 ms 33272 KB Output is correct
6 Correct 31 ms 33272 KB Output is correct
7 Correct 31 ms 33308 KB Output is correct
8 Correct 28 ms 33296 KB Output is correct
9 Correct 35 ms 33284 KB Output is correct
10 Correct 29 ms 33272 KB Output is correct
11 Correct 37 ms 33272 KB Output is correct
12 Correct 29 ms 28552 KB Output is correct
13 Correct 36 ms 33628 KB Output is correct
14 Correct 37 ms 33660 KB Output is correct
15 Correct 39 ms 33528 KB Output is correct
16 Correct 40 ms 33528 KB Output is correct
17 Correct 32 ms 33508 KB Output is correct
18 Correct 33 ms 33528 KB Output is correct
19 Correct 41 ms 33528 KB Output is correct
20 Correct 36 ms 33584 KB Output is correct
21 Correct 35 ms 33656 KB Output is correct
22 Correct 38 ms 33528 KB Output is correct
23 Correct 34 ms 33536 KB Output is correct
24 Correct 33 ms 33656 KB Output is correct
25 Correct 34 ms 33740 KB Output is correct
26 Correct 36 ms 33532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 28544 KB Output is correct
2 Correct 708 ms 55928 KB Output is correct
3 Correct 803 ms 63764 KB Output is correct
4 Correct 656 ms 56000 KB Output is correct
5 Correct 672 ms 56024 KB Output is correct
6 Correct 708 ms 57380 KB Output is correct
7 Correct 570 ms 56532 KB Output is correct
8 Correct 901 ms 65628 KB Output is correct
9 Correct 451 ms 57632 KB Output is correct
10 Correct 27 ms 28544 KB Output is correct
11 Correct 712 ms 56028 KB Output is correct
12 Correct 795 ms 63352 KB Output is correct
13 Correct 689 ms 55928 KB Output is correct
14 Correct 670 ms 56052 KB Output is correct
15 Correct 735 ms 57440 KB Output is correct
16 Correct 441 ms 57284 KB Output is correct
17 Correct 812 ms 64328 KB Output is correct
18 Correct 465 ms 57556 KB Output is correct
19 Correct 27 ms 28524 KB Output is correct
20 Correct 735 ms 56084 KB Output is correct
21 Correct 841 ms 66268 KB Output is correct
22 Correct 730 ms 58304 KB Output is correct
23 Correct 718 ms 58104 KB Output is correct
24 Correct 721 ms 58148 KB Output is correct
25 Correct 743 ms 58208 KB Output is correct
26 Correct 759 ms 58232 KB Output is correct
27 Correct 710 ms 58284 KB Output is correct
28 Correct 800 ms 59764 KB Output is correct
29 Correct 805 ms 58332 KB Output is correct
30 Correct 725 ms 58500 KB Output is correct
31 Correct 552 ms 59120 KB Output is correct
32 Correct 813 ms 65808 KB Output is correct
33 Correct 506 ms 59884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 28544 KB Output is correct
2 Correct 30 ms 33280 KB Output is correct
3 Correct 31 ms 33272 KB Output is correct
4 Correct 32 ms 33280 KB Output is correct
5 Correct 30 ms 33272 KB Output is correct
6 Correct 31 ms 33272 KB Output is correct
7 Correct 31 ms 33308 KB Output is correct
8 Correct 28 ms 33296 KB Output is correct
9 Correct 35 ms 33284 KB Output is correct
10 Correct 29 ms 33272 KB Output is correct
11 Correct 37 ms 33272 KB Output is correct
12 Correct 28 ms 28544 KB Output is correct
13 Correct 708 ms 55928 KB Output is correct
14 Correct 803 ms 63764 KB Output is correct
15 Correct 656 ms 56000 KB Output is correct
16 Correct 672 ms 56024 KB Output is correct
17 Correct 708 ms 57380 KB Output is correct
18 Correct 570 ms 56532 KB Output is correct
19 Correct 901 ms 65628 KB Output is correct
20 Correct 451 ms 57632 KB Output is correct
21 Correct 27 ms 28544 KB Output is correct
22 Correct 712 ms 56028 KB Output is correct
23 Correct 795 ms 63352 KB Output is correct
24 Correct 689 ms 55928 KB Output is correct
25 Correct 670 ms 56052 KB Output is correct
26 Correct 735 ms 57440 KB Output is correct
27 Correct 441 ms 57284 KB Output is correct
28 Correct 812 ms 64328 KB Output is correct
29 Correct 465 ms 57556 KB Output is correct
30 Correct 29 ms 28552 KB Output is correct
31 Correct 36 ms 33628 KB Output is correct
32 Correct 37 ms 33660 KB Output is correct
33 Correct 39 ms 33528 KB Output is correct
34 Correct 40 ms 33528 KB Output is correct
35 Correct 32 ms 33508 KB Output is correct
36 Correct 33 ms 33528 KB Output is correct
37 Correct 41 ms 33528 KB Output is correct
38 Correct 36 ms 33584 KB Output is correct
39 Correct 35 ms 33656 KB Output is correct
40 Correct 38 ms 33528 KB Output is correct
41 Correct 34 ms 33536 KB Output is correct
42 Correct 33 ms 33656 KB Output is correct
43 Correct 34 ms 33740 KB Output is correct
44 Correct 36 ms 33532 KB Output is correct
45 Correct 27 ms 28524 KB Output is correct
46 Correct 735 ms 56084 KB Output is correct
47 Correct 841 ms 66268 KB Output is correct
48 Correct 730 ms 58304 KB Output is correct
49 Correct 718 ms 58104 KB Output is correct
50 Correct 721 ms 58148 KB Output is correct
51 Correct 743 ms 58208 KB Output is correct
52 Correct 759 ms 58232 KB Output is correct
53 Correct 710 ms 58284 KB Output is correct
54 Correct 800 ms 59764 KB Output is correct
55 Correct 805 ms 58332 KB Output is correct
56 Correct 725 ms 58500 KB Output is correct
57 Correct 552 ms 59120 KB Output is correct
58 Correct 813 ms 65808 KB Output is correct
59 Correct 506 ms 59884 KB Output is correct
60 Correct 29 ms 28516 KB Output is correct
61 Correct 779 ms 59376 KB Output is correct
62 Correct 915 ms 67192 KB Output is correct
63 Correct 757 ms 59512 KB Output is correct
64 Correct 865 ms 59352 KB Output is correct
65 Correct 742 ms 59264 KB Output is correct
66 Correct 759 ms 59396 KB Output is correct
67 Correct 750 ms 59284 KB Output is correct
68 Correct 712 ms 59776 KB Output is correct
69 Correct 718 ms 60540 KB Output is correct
70 Correct 741 ms 59328 KB Output is correct
71 Correct 701 ms 59912 KB Output is correct
72 Correct 666 ms 60744 KB Output is correct
73 Correct 819 ms 67868 KB Output is correct
74 Correct 521 ms 62272 KB Output is correct