답안 #132763

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
132763 2019-07-19T13:44:22 Z youngyojun Designated Cities (JOI19_designated_cities) C++11
100 / 100
855 ms 51704 KB
#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
typedef long long ll;

const int MAXN = 200055;

struct SEG {
	ll dmx[MAXN*3], ds[MAXN*3];
	int di[MAXN*3];

	void init(int i, int s, int e) {
		if(s == e) {
			di[i] = s;
			return;
		}
		int m = (s+e) >> 1;
		init(i<<1, s, m);
		init(i<<1|1, m+1, e);
		di[i] = s;
	}
	void upd(int i, int s, int e, int p, int q, ll r) {
		if(q < p || e < p || q < s) return;
		if(p <= s && e <= q) {
			ds[i] += r;
			dmx[i] += r;
			return;
		}
		int m = (s+e) >> 1;
		upd(i<<1, s, m, p, q, r);
		upd(i<<1|1, m+1, e, p, q, r);
		int j = dmx[i<<1] < dmx[i<<1|1] ? (i<<1|1) : (i<<1);
		dmx[i] = dmx[j] + ds[i];
		di[i] = di[j];
	}
	int get() { return di[1]; }
} seg;

vector<int> G[MAXN];
ll dl[MAXN];
int prt[MAXN], prte[MAXN], dep[MAXN], cnt[MAXN], dfo[MAXN], rfo[MAXN];
bitset<MAXN> chk;

int A[MAXN], B[MAXN], C[MAXN], D[MAXN];

ll Ans[MAXN], Sum;
int N, Q, Rt;

void goup(int i, ll &sum) {
	for(; !chk[i]; i = prt[i]) {
		sum += C[prte[i]];
		seg.upd(1, 1, N, dfo[i], dfo[i]+cnt[i]-1, -C[prte[i]]);
		chk[i] = true;
	}
}

int getCost(int p, int e) { return p == A[e] ? C[e] : D[e]; }
void predfs(int i, int &c) {
	cnt[i] = 1; dfo[i] = ++c;
	for(int e : G[i]) {
		int v = A[e]^B[e]^i;
		if(v == prt[i]) continue;
		dep[v] = dep[i] + 1;
		prt[v] = i;
		prte[v] = e;
		dl[v] = dl[i] + getCost(i, e);
		predfs(v, c);
		cnt[i] += cnt[v];
	}
}
void predfs(int i) { int c = 0; predfs(i, c); }

void findRt() {
	predfs(1);
	Rt = int(max_element(dl+1, dl+N+1) - dl);
	prt[Rt] = 0; dl[Rt] = 0;
	predfs(Rt);
	for(int i = 1; i <= N; i++) rfo[dfo[i]] = i;
	for(int i = 1; i < N; i++) if(prt[A[i]] == B[i]) {
		swap(A[i], B[i]);
		swap(C[i], D[i]);
	}
}

void getAnsTwo() {
	ll sum = 0;
	seg.init(1, 1, N);
	chk[Rt] = true;
	for(int i = 1, v; i < N; i++) {
		sum += D[i];
		v = B[i];
		seg.upd(1, 1, N, dfo[v], dfo[v]+cnt[v]-1, C[i]);
	}
	for(int t = 2, i; t <= N; t++) {
		i = rfo[seg.get()];
		goup(i, sum);
		Ans[t] = sum;
	}
}

void dfsOne(int i, ll sum) {
	if(Ans[1] < sum) Ans[1] = sum;
	for(int e : G[i]) if(A[e] != prt[i])
		dfsOne(B[e], sum - D[e] + C[e]);
}

void getAnsOne() {
	predfs(1);
	for(int i = 1; i < N; i++) if(prt[A[i]] == B[i]) {
		swap(A[i], B[i]);
		swap(C[i], D[i]);
	}
	ll sum = 0;
	for(int i = 1; i < N; i++) sum += D[i];
	dfsOne(1, sum);
}

int main() {
	ios::sync_with_stdio(false);

	cin >> N;
	for(int i = 1; i < N; i++) {
		cin >> A[i] >> B[i] >> C[i] >> D[i];
		G[A[i]].eb(i);
		G[B[i]].eb(i);
		Sum += C[i] + D[i];
	}

	getAnsOne();
	findRt();
	getAnsTwo();

	cin >> Q;
	for(int a; Q--;) {
		cin >> a;
		printf("%lld\n", Sum - Ans[a]);
	}
	return 0;
}
# 결과 실행 시간 메모리 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 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 6 ms 5112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 671 ms 32860 KB Output is correct
3 Correct 744 ms 45204 KB Output is correct
4 Correct 653 ms 32888 KB Output is correct
5 Correct 656 ms 32980 KB Output is correct
6 Correct 712 ms 34148 KB Output is correct
7 Correct 592 ms 32880 KB Output is correct
8 Correct 784 ms 44408 KB Output is correct
9 Correct 440 ms 32360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 692 ms 38984 KB Output is correct
3 Correct 726 ms 51472 KB Output is correct
4 Correct 640 ms 37884 KB Output is correct
5 Correct 714 ms 39156 KB Output is correct
6 Correct 699 ms 40984 KB Output is correct
7 Correct 515 ms 39120 KB Output is correct
8 Correct 748 ms 46684 KB Output is correct
9 Correct 461 ms 38636 KB Output is correct
# 결과 실행 시간 메모리 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 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 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 10 ms 5500 KB Output is correct
14 Correct 9 ms 5624 KB Output is correct
15 Correct 9 ms 5496 KB Output is correct
16 Correct 10 ms 5496 KB Output is correct
17 Correct 9 ms 5496 KB Output is correct
18 Correct 9 ms 5496 KB Output is correct
19 Correct 9 ms 5496 KB Output is correct
20 Correct 9 ms 5496 KB Output is correct
21 Correct 10 ms 5496 KB Output is correct
22 Correct 9 ms 5496 KB Output is correct
23 Correct 10 ms 5496 KB Output is correct
24 Correct 9 ms 5496 KB Output is correct
25 Correct 10 ms 5496 KB Output is correct
26 Correct 9 ms 5496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 671 ms 32860 KB Output is correct
3 Correct 744 ms 45204 KB Output is correct
4 Correct 653 ms 32888 KB Output is correct
5 Correct 656 ms 32980 KB Output is correct
6 Correct 712 ms 34148 KB Output is correct
7 Correct 592 ms 32880 KB Output is correct
8 Correct 784 ms 44408 KB Output is correct
9 Correct 440 ms 32360 KB Output is correct
10 Correct 6 ms 5112 KB Output is correct
11 Correct 692 ms 38984 KB Output is correct
12 Correct 726 ms 51472 KB Output is correct
13 Correct 640 ms 37884 KB Output is correct
14 Correct 714 ms 39156 KB Output is correct
15 Correct 699 ms 40984 KB Output is correct
16 Correct 515 ms 39120 KB Output is correct
17 Correct 748 ms 46684 KB Output is correct
18 Correct 461 ms 38636 KB Output is correct
19 Correct 6 ms 5160 KB Output is correct
20 Correct 707 ms 38880 KB Output is correct
21 Correct 736 ms 51464 KB Output is correct
22 Correct 630 ms 37676 KB Output is correct
23 Correct 685 ms 38860 KB Output is correct
24 Correct 660 ms 37992 KB Output is correct
25 Correct 837 ms 38896 KB Output is correct
26 Correct 667 ms 38008 KB Output is correct
27 Correct 657 ms 38904 KB Output is correct
28 Correct 758 ms 40568 KB Output is correct
29 Correct 718 ms 39240 KB Output is correct
30 Correct 646 ms 38260 KB Output is correct
31 Correct 620 ms 39060 KB Output is correct
32 Correct 855 ms 46860 KB Output is correct
33 Correct 463 ms 38488 KB Output is correct
# 결과 실행 시간 메모리 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 5112 KB Output is correct
8 Correct 6 ms 5112 KB Output is correct
9 Correct 6 ms 5112 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 671 ms 32860 KB Output is correct
14 Correct 744 ms 45204 KB Output is correct
15 Correct 653 ms 32888 KB Output is correct
16 Correct 656 ms 32980 KB Output is correct
17 Correct 712 ms 34148 KB Output is correct
18 Correct 592 ms 32880 KB Output is correct
19 Correct 784 ms 44408 KB Output is correct
20 Correct 440 ms 32360 KB Output is correct
21 Correct 6 ms 5112 KB Output is correct
22 Correct 692 ms 38984 KB Output is correct
23 Correct 726 ms 51472 KB Output is correct
24 Correct 640 ms 37884 KB Output is correct
25 Correct 714 ms 39156 KB Output is correct
26 Correct 699 ms 40984 KB Output is correct
27 Correct 515 ms 39120 KB Output is correct
28 Correct 748 ms 46684 KB Output is correct
29 Correct 461 ms 38636 KB Output is correct
30 Correct 6 ms 5112 KB Output is correct
31 Correct 10 ms 5500 KB Output is correct
32 Correct 9 ms 5624 KB Output is correct
33 Correct 9 ms 5496 KB Output is correct
34 Correct 10 ms 5496 KB Output is correct
35 Correct 9 ms 5496 KB Output is correct
36 Correct 9 ms 5496 KB Output is correct
37 Correct 9 ms 5496 KB Output is correct
38 Correct 9 ms 5496 KB Output is correct
39 Correct 10 ms 5496 KB Output is correct
40 Correct 9 ms 5496 KB Output is correct
41 Correct 10 ms 5496 KB Output is correct
42 Correct 9 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 5160 KB Output is correct
46 Correct 707 ms 38880 KB Output is correct
47 Correct 736 ms 51464 KB Output is correct
48 Correct 630 ms 37676 KB Output is correct
49 Correct 685 ms 38860 KB Output is correct
50 Correct 660 ms 37992 KB Output is correct
51 Correct 837 ms 38896 KB Output is correct
52 Correct 667 ms 38008 KB Output is correct
53 Correct 657 ms 38904 KB Output is correct
54 Correct 758 ms 40568 KB Output is correct
55 Correct 718 ms 39240 KB Output is correct
56 Correct 646 ms 38260 KB Output is correct
57 Correct 620 ms 39060 KB Output is correct
58 Correct 855 ms 46860 KB Output is correct
59 Correct 463 ms 38488 KB Output is correct
60 Correct 6 ms 5112 KB Output is correct
61 Correct 732 ms 40092 KB Output is correct
62 Correct 774 ms 51704 KB Output is correct
63 Correct 755 ms 40312 KB Output is correct
64 Correct 741 ms 40156 KB Output is correct
65 Correct 735 ms 39928 KB Output is correct
66 Correct 751 ms 40260 KB Output is correct
67 Correct 745 ms 39928 KB Output is correct
68 Correct 737 ms 40336 KB Output is correct
69 Correct 748 ms 41676 KB Output is correct
70 Correct 770 ms 40068 KB Output is correct
71 Correct 689 ms 40052 KB Output is correct
72 Correct 632 ms 40884 KB Output is correct
73 Correct 830 ms 47532 KB Output is correct
74 Correct 537 ms 41160 KB Output is correct