답안 #204081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
204081 2020-02-24T09:34:36 Z ics0503 Designated Cities (JOI19_designated_cities) C++17
100 / 100
865 ms 94968 KB
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
struct xy { long long x, y, z; };
vector<xy>edge[212121];
long long D[212121], F[212121], FW[212121];
void get_DF(long long w,long long bef) {
	D[w] = F[w] = 0; FW[w] = w;
	for (xy E : edge[w]) if (E.x != bef) {
		get_DF(E.x, w);
		D[w] += D[E.x] + E.y;
		if (F[w] < F[E.x] + E.z) {
			F[w] = F[E.x] + E.z;
			FW[w] = FW[E.x];
		}
	}
}
long long resX[212121], resY[212121], l[212121], r[212121];
void get_res(long long w, long long bef, long long fromRoot) {
	long long sum = 0, cnt = 0;
	vector<pair<long long, long long>>L;
	for (xy E : edge[w]) if (E.x != bef) {
		sum += D[E.x] + E.y;
		L.push_back({ -(F[E.x] + E.z), FW[E.x] });
	}
	sort(L.begin(), L.end());
	resX[w] = sum + fromRoot;
	if (L.size() > 0)resY[w] = -(L[0].first) + sum + fromRoot, l[w] = w, r[w] = L[0].second;
	if (L.size() > 1)resY[w] = -(L[0].first + L[1].first) + sum + fromRoot, l[w] = L[0].second, r[w] = L[1].second;
	for (xy E : edge[w]) if (E.x != bef) get_res(E.x, w, fromRoot + sum - (D[E.x] + E.y) + E.z);
}
//segtree
struct ND { long long l, r; long long max, maxw, pls; }tree[616161]; long long treen = 1;
void update(long long w) {
	long long l = tree[w].l, r = tree[w].r;
	if (tree[l].max >= tree[r].max) tree[w].max = tree[l].max, tree[w].maxw = tree[l].maxw;
	else tree[w].max = tree[r].max, tree[w].maxw = tree[r].maxw;
}
void spread(long long w) {
	long long l = tree[w].l, r = tree[w].r, pls = tree[w].pls;
	tree[l].max += pls; tree[l].pls += pls;
	tree[r].max += pls; tree[r].pls += pls;
	tree[w].pls = 0;
}
void plsV(long long now, long long s, long long e, long long l, long long r, long long v) {
	if (s != e)spread(now);
	if (s == l&&e == r) {
		tree[now].pls += v;
		tree[now].max += v;
		if (l == r)tree[now].maxw = l;
		return;
	}
	if (tree[now].l == 0) { tree[now].l = ++treen; tree[treen] = { 0, 0, 0, 0 }; }
	if (tree[now].r == 0) { tree[now].r = ++treen; tree[treen] = { 0, 0, 0, 0 }; }
	long long m = (s + e) / 2;
	if (l <= m)plsV(tree[now].l, s, m, l, min(r, m), v);
	if (m + 1 <= r)plsV(tree[now].r, m + 1, e, max(l, m + 1), r, v);
	update(now);
}
long long getMinV() {
	if (tree[1].max == 0)return -1;
	return tree[1].maxw;
}

long long S[212121], E[212121], R[212121], dfscnt = 0, pr[212121]; long long prV[212121], depth[212121], n;
void dfs(long long w, long long bef, long long befV, long long dep) {
	S[w] = ++dfscnt;  pr[w] = bef; prV[w] = befV; depth[w] = dep;
	R[S[w]] = w;
	for (xy E : edge[w]) if (E.x != bef) {
		dfs(E.x, w, E.z, dep + E.z);
	}
	E[w] = dfscnt;
}
long long eraseE(long long w) {
	if (prV[w] == 0)return 0;
	plsV(1, 1, n, S[w], E[w], -prV[w]);
	long long res = prV[w];
	prV[w] = 0;
	res+=eraseE(pr[w]);
	return res;
}
long long ans[212121];
int main() {
	long long  i, j, allsum = 0; scanf("%lld", &n);
	for (i = 0; i < n - 1; i++) {
		long long s, e, a, b; scanf("%lld%lld%lld%lld", &s, &e, &a, &b);
		allsum += a; allsum += b;
		edge[s].push_back({ e, b, a });
		edge[e].push_back({ s, a, b });
	}
	get_DF(1, -1);
	get_res(1, -1, 0);
	long long rootSum = 0, root = 1;
	for (i = 1; i <= n; i++) if (ans[1] < resX[i])ans[1] = resX[i];
	for (i = 1; i <= n; i++) if (rootSum < resY[i])rootSum = resY[i], root = i;
	ans[1] = allsum - ans[1];
	ans[2] = allsum - rootSum;
	dfs(root, -1, 0, 0);
	for (i = 1; i <= n; i++) {
		plsV(1, 1, n, S[i], S[i], depth[i]);
	}
	eraseE(l[root]);
	eraseE(r[root]);
	for (i = 3; i <= n; i++) {
		long long v = R[getMinV()];
		if (v == -1) { ans[i] = ans[i - 1]; continue; }
		ans[i] = ans[i-1] - eraseE(v);
	}
	long long q, x; scanf("%lld", &q);
	for (i = 0; i < q; i++) {
		scanf("%lld", &x);
		printf("%lld\n", ans[x]);
	}
	return 0;
}  

Compilation message

designated_cities.cpp: In function 'void get_res(long long int, long long int, long long int)':
designated_cities.cpp:21:21: warning: unused variable 'cnt' [-Wunused-variable]
  long long sum = 0, cnt = 0;
                     ^~~
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:85:16: warning: unused variable 'j' [-Wunused-variable]
  long long  i, j, allsum = 0; scanf("%lld", &n);
                ^
designated_cities.cpp:85:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  long long  i, j, allsum = 0; scanf("%lld", &n);
                               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:87:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   long long s, e, a, b; scanf("%lld%lld%lld%lld", &s, &e, &a, &b);
                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:110:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  long long q, x; scanf("%lld", &q);
                  ~~~~~^~~~~~~~~~~~
designated_cities.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &x);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5368 KB Output is correct
3 Correct 8 ms 5368 KB Output is correct
4 Correct 8 ms 5368 KB Output is correct
5 Correct 8 ms 5368 KB Output is correct
6 Correct 8 ms 5368 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 9 ms 5368 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 8 ms 5372 KB Output is correct
11 Correct 8 ms 5368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5344 KB Output is correct
2 Correct 761 ms 56568 KB Output is correct
3 Correct 773 ms 83896 KB Output is correct
4 Correct 675 ms 56768 KB Output is correct
5 Correct 726 ms 57300 KB Output is correct
6 Correct 766 ms 60920 KB Output is correct
7 Correct 685 ms 57352 KB Output is correct
8 Correct 807 ms 84984 KB Output is correct
9 Correct 595 ms 53848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 751 ms 56544 KB Output is correct
3 Correct 737 ms 88696 KB Output is correct
4 Correct 696 ms 56572 KB Output is correct
5 Correct 727 ms 57504 KB Output is correct
6 Correct 766 ms 61480 KB Output is correct
7 Correct 617 ms 58496 KB Output is correct
8 Correct 792 ms 74232 KB Output is correct
9 Correct 613 ms 53332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5368 KB Output is correct
3 Correct 8 ms 5368 KB Output is correct
4 Correct 8 ms 5368 KB Output is correct
5 Correct 8 ms 5368 KB Output is correct
6 Correct 8 ms 5368 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 9 ms 5368 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 8 ms 5372 KB Output is correct
11 Correct 8 ms 5368 KB Output is correct
12 Correct 8 ms 5368 KB Output is correct
13 Correct 13 ms 6020 KB Output is correct
14 Correct 11 ms 6136 KB Output is correct
15 Correct 11 ms 6012 KB Output is correct
16 Correct 12 ms 6008 KB Output is correct
17 Correct 11 ms 6008 KB Output is correct
18 Correct 12 ms 6008 KB Output is correct
19 Correct 11 ms 6008 KB Output is correct
20 Correct 14 ms 6008 KB Output is correct
21 Correct 11 ms 6008 KB Output is correct
22 Correct 11 ms 6008 KB Output is correct
23 Correct 11 ms 6008 KB Output is correct
24 Correct 12 ms 6008 KB Output is correct
25 Correct 12 ms 6264 KB Output is correct
26 Correct 11 ms 6008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5344 KB Output is correct
2 Correct 761 ms 56568 KB Output is correct
3 Correct 773 ms 83896 KB Output is correct
4 Correct 675 ms 56768 KB Output is correct
5 Correct 726 ms 57300 KB Output is correct
6 Correct 766 ms 60920 KB Output is correct
7 Correct 685 ms 57352 KB Output is correct
8 Correct 807 ms 84984 KB Output is correct
9 Correct 595 ms 53848 KB Output is correct
10 Correct 8 ms 5368 KB Output is correct
11 Correct 751 ms 56544 KB Output is correct
12 Correct 737 ms 88696 KB Output is correct
13 Correct 696 ms 56572 KB Output is correct
14 Correct 727 ms 57504 KB Output is correct
15 Correct 766 ms 61480 KB Output is correct
16 Correct 617 ms 58496 KB Output is correct
17 Correct 792 ms 74232 KB Output is correct
18 Correct 613 ms 53332 KB Output is correct
19 Correct 8 ms 5368 KB Output is correct
20 Correct 754 ms 56120 KB Output is correct
21 Correct 812 ms 94968 KB Output is correct
22 Correct 683 ms 61048 KB Output is correct
23 Correct 742 ms 62840 KB Output is correct
24 Correct 742 ms 61688 KB Output is correct
25 Correct 765 ms 62916 KB Output is correct
26 Correct 747 ms 61752 KB Output is correct
27 Correct 726 ms 63152 KB Output is correct
28 Correct 742 ms 66808 KB Output is correct
29 Correct 735 ms 63096 KB Output is correct
30 Correct 720 ms 62336 KB Output is correct
31 Correct 661 ms 63268 KB Output is correct
32 Correct 807 ms 82168 KB Output is correct
33 Correct 622 ms 59860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5368 KB Output is correct
2 Correct 8 ms 5368 KB Output is correct
3 Correct 8 ms 5368 KB Output is correct
4 Correct 8 ms 5368 KB Output is correct
5 Correct 8 ms 5368 KB Output is correct
6 Correct 8 ms 5368 KB Output is correct
7 Correct 8 ms 5368 KB Output is correct
8 Correct 9 ms 5368 KB Output is correct
9 Correct 8 ms 5368 KB Output is correct
10 Correct 8 ms 5372 KB Output is correct
11 Correct 8 ms 5368 KB Output is correct
12 Correct 8 ms 5344 KB Output is correct
13 Correct 761 ms 56568 KB Output is correct
14 Correct 773 ms 83896 KB Output is correct
15 Correct 675 ms 56768 KB Output is correct
16 Correct 726 ms 57300 KB Output is correct
17 Correct 766 ms 60920 KB Output is correct
18 Correct 685 ms 57352 KB Output is correct
19 Correct 807 ms 84984 KB Output is correct
20 Correct 595 ms 53848 KB Output is correct
21 Correct 8 ms 5368 KB Output is correct
22 Correct 751 ms 56544 KB Output is correct
23 Correct 737 ms 88696 KB Output is correct
24 Correct 696 ms 56572 KB Output is correct
25 Correct 727 ms 57504 KB Output is correct
26 Correct 766 ms 61480 KB Output is correct
27 Correct 617 ms 58496 KB Output is correct
28 Correct 792 ms 74232 KB Output is correct
29 Correct 613 ms 53332 KB Output is correct
30 Correct 8 ms 5368 KB Output is correct
31 Correct 13 ms 6020 KB Output is correct
32 Correct 11 ms 6136 KB Output is correct
33 Correct 11 ms 6012 KB Output is correct
34 Correct 12 ms 6008 KB Output is correct
35 Correct 11 ms 6008 KB Output is correct
36 Correct 12 ms 6008 KB Output is correct
37 Correct 11 ms 6008 KB Output is correct
38 Correct 14 ms 6008 KB Output is correct
39 Correct 11 ms 6008 KB Output is correct
40 Correct 11 ms 6008 KB Output is correct
41 Correct 11 ms 6008 KB Output is correct
42 Correct 12 ms 6008 KB Output is correct
43 Correct 12 ms 6264 KB Output is correct
44 Correct 11 ms 6008 KB Output is correct
45 Correct 8 ms 5368 KB Output is correct
46 Correct 754 ms 56120 KB Output is correct
47 Correct 812 ms 94968 KB Output is correct
48 Correct 683 ms 61048 KB Output is correct
49 Correct 742 ms 62840 KB Output is correct
50 Correct 742 ms 61688 KB Output is correct
51 Correct 765 ms 62916 KB Output is correct
52 Correct 747 ms 61752 KB Output is correct
53 Correct 726 ms 63152 KB Output is correct
54 Correct 742 ms 66808 KB Output is correct
55 Correct 735 ms 63096 KB Output is correct
56 Correct 720 ms 62336 KB Output is correct
57 Correct 661 ms 63268 KB Output is correct
58 Correct 807 ms 82168 KB Output is correct
59 Correct 622 ms 59860 KB Output is correct
60 Correct 8 ms 5368 KB Output is correct
61 Correct 808 ms 65144 KB Output is correct
62 Correct 810 ms 91768 KB Output is correct
63 Correct 743 ms 63992 KB Output is correct
64 Correct 818 ms 65532 KB Output is correct
65 Correct 791 ms 64248 KB Output is correct
66 Correct 818 ms 65556 KB Output is correct
67 Correct 794 ms 64192 KB Output is correct
68 Correct 785 ms 65540 KB Output is correct
69 Correct 826 ms 68984 KB Output is correct
70 Correct 803 ms 65912 KB Output is correct
71 Correct 761 ms 65108 KB Output is correct
72 Correct 729 ms 66584 KB Output is correct
73 Correct 865 ms 81400 KB Output is correct
74 Correct 673 ms 64100 KB Output is correct