Submission #132772

# Submission time Handle Problem Language Result Execution time Memory
132772 2019-07-19T13:58:46 Z imyujin Designated Cities (JOI19_designated_cities) C++14
100 / 100
863 ms 42392 KB
#include <bits/stdc++.h>
using namespace std;

#define MAXN 200005
#define fi first
#define se second
#define mp make_pair

typedef long long lint;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<lint, int> pli;

vector<piii> ed[MAXN];
int par[MAXN], pare[MAXN];
bool chk[MAXN];
lint ans[MAXN], dis[MAXN];
int dfsord[MAXN], dfsr[MAXN], dfsver[MAXN], dfsn;
pli seg[4 * MAXN];
lint lazy[4 * MAXN];

lint gsum(int x) {
	lint ans = 0ll;
	for(auto a : ed[x]) if(a.se != par[x]) {
		ans += a.fi.se;
		par[a.se] = x;
		ans += gsum(a.se);
	}
	//if(x == 1) printf("gsum(1) = %lld\n", ans);
	return ans;
}

void dfs(int x, lint s) {
	ans[0] = max(ans[0], s);
	for(auto a : ed[x]) if(a.se != par[x]) 
		dfs(a.se, s + a.fi.fi - a.fi.se);
}

void gdis(int x) {
	for(auto a : ed[x]) if(a.se != par[x]) {
		par[a.se] = x;
		dis[a.se] = dis[x] + a.fi.fi;
		gdis(a.se);
	}
}

void preord(int x) {
	dfsord[x] = ++dfsn;
	dfsver[dfsord[x]] = x;
	for(auto a : ed[x]) if(a.se != par[x]) {
		par[a.se] = x;
		preord(a.se);
	}
	dfsr[x] = dfsn;
}

void mkseg(int idx, int l, int r) {
	seg[idx] = mp(0ll, l);
	if(l < r) {
		int m = (l + r) / 2;
		mkseg(idx * 2, l, m);
		mkseg(idx * 2 + 1, m + 1, r);
	}
}

void updseg(int idx, int l, int r, int x, int y, int z) {
	if(x <= l && r <= y) {
		seg[idx].fi += z;
		lazy[idx] += z;
	}
	else if(l <= y && x <= r) {
		int m = (l + r) / 2;
		updseg(idx * 2, l, m, x, y, z);
		updseg(idx * 2 + 1, m + 1, r, x, y, z);
		seg[idx] = max(seg[idx * 2], seg[idx * 2 + 1]);
		seg[idx].fi += lazy[idx];
	}
}

int main() {
	int N, Q;
	int A, B, C, D;
	int u = 1;
	lint sum = 0ll;

	scanf("%d", &N);
	for(int i = 0; i < N - 1; i++) {
		scanf("%d%d%d%d", &A, &B, &C, &D);
		ed[A].push_back(mp(mp(C, D), B));
		ed[B].push_back(mp(mp(D, C), A));
		sum += C + D;
	}

	dfs(1, gsum(1));
	gdis(1);
	for(int i = 2; i <= N; i++) if(dis[i] > dis[u]) u = i;
	par[u] = dis[u] = 0;
	preord(u);
	//printf("u = %d\n", u);

	//printf("ans[0] = %lld\n", ans[0]);

	mkseg(1, 1, N);
	for(int i = 1; i <= N; i++) if(i != u) {
		int e;
		for(e = 0; ed[i][e].se != par[i]; e++);
		pare[i] = ed[i][e].fi.se;
		updseg(1, 1, N, dfsord[i], dfsr[i], pare[i]);
	}

	par[u] = 0;
	ans[1] = gsum(u);
	chk[u] = true;
	for(int i = 2; i <= N; i++) {
		ans[i] = ans[i - 1] + seg[1].fi;
		for(int t = dfsver[seg[1].se]; !chk[t]; t = par[t]) {
			//printf("i = %d, t = %d\n", i, t);
			updseg(1, 1, N, dfsord[t], dfsr[t], -pare[t]);
			chk[t] = true;
		}
	}

	scanf("%d", &Q);
	while(Q--) {
		scanf("%d", &A);
		printf("%lld\n", sum - ans[A == 1 ? 0 : A]);
	}
	return 0;
}

Compilation message

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &A, &B, &C, &D);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:125:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &A);
   ~~~~~^~~~~~~~~~
# 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 5040 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 702 ms 33768 KB Output is correct
3 Correct 658 ms 41336 KB Output is correct
4 Correct 652 ms 33808 KB Output is correct
5 Correct 635 ms 33740 KB Output is correct
6 Correct 745 ms 34908 KB Output is correct
7 Correct 550 ms 33436 KB Output is correct
8 Correct 730 ms 41112 KB Output is correct
9 Correct 615 ms 33400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 666 ms 34040 KB Output is correct
3 Correct 651 ms 41464 KB Output is correct
4 Correct 659 ms 33924 KB Output is correct
5 Correct 662 ms 34008 KB Output is correct
6 Correct 720 ms 35592 KB Output is correct
7 Correct 524 ms 33568 KB Output is correct
8 Correct 751 ms 39552 KB Output is correct
9 Correct 510 ms 33356 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 5040 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 9 ms 5368 KB Output is correct
14 Correct 9 ms 5496 KB Output is correct
15 Correct 10 ms 5368 KB Output is correct
16 Correct 10 ms 5368 KB Output is correct
17 Correct 10 ms 5372 KB Output is correct
18 Correct 10 ms 5368 KB Output is correct
19 Correct 9 ms 5368 KB Output is correct
20 Correct 10 ms 5368 KB Output is correct
21 Correct 10 ms 5368 KB Output is correct
22 Correct 13 ms 5368 KB Output is correct
23 Correct 14 ms 5368 KB Output is correct
24 Correct 10 ms 5496 KB Output is correct
25 Correct 10 ms 5496 KB Output is correct
26 Correct 9 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 702 ms 33768 KB Output is correct
3 Correct 658 ms 41336 KB Output is correct
4 Correct 652 ms 33808 KB Output is correct
5 Correct 635 ms 33740 KB Output is correct
6 Correct 745 ms 34908 KB Output is correct
7 Correct 550 ms 33436 KB Output is correct
8 Correct 730 ms 41112 KB Output is correct
9 Correct 615 ms 33400 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 666 ms 34040 KB Output is correct
12 Correct 651 ms 41464 KB Output is correct
13 Correct 659 ms 33924 KB Output is correct
14 Correct 662 ms 34008 KB Output is correct
15 Correct 720 ms 35592 KB Output is correct
16 Correct 524 ms 33568 KB Output is correct
17 Correct 751 ms 39552 KB Output is correct
18 Correct 510 ms 33356 KB Output is correct
19 Correct 6 ms 5112 KB Output is correct
20 Correct 670 ms 33912 KB Output is correct
21 Correct 660 ms 41496 KB Output is correct
22 Correct 646 ms 33772 KB Output is correct
23 Correct 682 ms 34240 KB Output is correct
24 Correct 675 ms 34040 KB Output is correct
25 Correct 682 ms 34168 KB Output is correct
26 Correct 683 ms 33836 KB Output is correct
27 Correct 651 ms 33948 KB Output is correct
28 Correct 700 ms 35448 KB Output is correct
29 Correct 701 ms 34040 KB Output is correct
30 Correct 640 ms 33840 KB Output is correct
31 Correct 572 ms 33940 KB Output is correct
32 Correct 715 ms 39924 KB Output is correct
33 Correct 506 ms 33672 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 5040 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5116 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
12 Correct 6 ms 5112 KB Output is correct
13 Correct 702 ms 33768 KB Output is correct
14 Correct 658 ms 41336 KB Output is correct
15 Correct 652 ms 33808 KB Output is correct
16 Correct 635 ms 33740 KB Output is correct
17 Correct 745 ms 34908 KB Output is correct
18 Correct 550 ms 33436 KB Output is correct
19 Correct 730 ms 41112 KB Output is correct
20 Correct 615 ms 33400 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 666 ms 34040 KB Output is correct
23 Correct 651 ms 41464 KB Output is correct
24 Correct 659 ms 33924 KB Output is correct
25 Correct 662 ms 34008 KB Output is correct
26 Correct 720 ms 35592 KB Output is correct
27 Correct 524 ms 33568 KB Output is correct
28 Correct 751 ms 39552 KB Output is correct
29 Correct 510 ms 33356 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 9 ms 5368 KB Output is correct
32 Correct 9 ms 5496 KB Output is correct
33 Correct 10 ms 5368 KB Output is correct
34 Correct 10 ms 5368 KB Output is correct
35 Correct 10 ms 5372 KB Output is correct
36 Correct 10 ms 5368 KB Output is correct
37 Correct 9 ms 5368 KB Output is correct
38 Correct 10 ms 5368 KB Output is correct
39 Correct 10 ms 5368 KB Output is correct
40 Correct 13 ms 5368 KB Output is correct
41 Correct 14 ms 5368 KB Output is correct
42 Correct 10 ms 5496 KB Output is correct
43 Correct 10 ms 5496 KB Output is correct
44 Correct 9 ms 5496 KB Output is correct
45 Correct 6 ms 5112 KB Output is correct
46 Correct 670 ms 33912 KB Output is correct
47 Correct 660 ms 41496 KB Output is correct
48 Correct 646 ms 33772 KB Output is correct
49 Correct 682 ms 34240 KB Output is correct
50 Correct 675 ms 34040 KB Output is correct
51 Correct 682 ms 34168 KB Output is correct
52 Correct 683 ms 33836 KB Output is correct
53 Correct 651 ms 33948 KB Output is correct
54 Correct 700 ms 35448 KB Output is correct
55 Correct 701 ms 34040 KB Output is correct
56 Correct 640 ms 33840 KB Output is correct
57 Correct 572 ms 33940 KB Output is correct
58 Correct 715 ms 39924 KB Output is correct
59 Correct 506 ms 33672 KB Output is correct
60 Correct 6 ms 5112 KB Output is correct
61 Correct 863 ms 36088 KB Output is correct
62 Correct 686 ms 42392 KB Output is correct
63 Correct 699 ms 35576 KB Output is correct
64 Correct 730 ms 36164 KB Output is correct
65 Correct 688 ms 35576 KB Output is correct
66 Correct 791 ms 36268 KB Output is correct
67 Correct 707 ms 35568 KB Output is correct
68 Correct 695 ms 36048 KB Output is correct
69 Correct 709 ms 37464 KB Output is correct
70 Correct 823 ms 35672 KB Output is correct
71 Correct 676 ms 36168 KB Output is correct
72 Correct 620 ms 36524 KB Output is correct
73 Correct 799 ms 41464 KB Output is correct
74 Correct 553 ms 37032 KB Output is correct