Submission #376205

# Submission time Handle Problem Language Result Execution time Memory
376205 2021-03-11T04:31:33 Z 8e7 Designated Cities (JOI19_designated_cities) C++14
100 / 100
717 ms 50932 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
#define ll long long
#define maxn 200005
#define pii pair<int, ll>
#define ff first
#define ss second
#define mod 1000000007
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

vector<pii> adj[maxn];

ll tup[maxn], up[maxn], best[maxn], bind[maxn], ans[maxn], wei[maxn];
int par[maxn];
bool done[maxn];

int lef[maxn], rig[maxn], arr[maxn];
int ord = 0;
void dfs(int n, int pa) {
	arr[ord] = n;
	lef[n] = ord++;
	par[n] = pa;
	for (auto v:adj[n]) {
		if (v.ff != pa) {
			wei[v.ff] = v.ss;
			dfs(v.ff, n);
			tup[n] += tup[v.ff] + up[v.ff];
			if (v.ss + best[v.ff] > best[n]) {
				best[n] = v.ss + best[v.ff];
				bind[n] = bind[v.ff] == 0 ? v.ff : bind[v.ff];
			}
		} else {
			up[n] = v.ss;
		}
	}
	rig[n] = ord;
}
void reroot(int u, int v, ll w) {
	tup[u] -= tup[v] + up[v];
	up[u] = w;
	tup[v] += tup[u] + up[u];
	up[v] = 0;
}
int type = 1;
void solve(int n, int par) {
	ans[1] = max(ans[1], tup[n]);
	for (auto v:adj[n]) {
		if (v.ff != par) {
			ll tmp = up[v.ff];
			reroot(n, v.ff, v.ss);
			solve(v.ff, n);
			reroot(v.ff, n, tmp);
		}
	}
}

ll add = 0, addind = 0;
void getweight(int n, int par, ll cur) {
	if (cur > add) {
		addind = n;
		add = cur;
	}
	for (auto v:adj[n]) {
		if (v.ff != par) {
			getweight(v.ff, n, cur + (done[v.ff] ? 0 : v.ss));
		}
	}
}

ll seg[4 * maxn], tag[4 * maxn];
void modify(int cur, int l, int r, int ql, int qr, ll val) {
	if (r <= l || ql >= r || qr <= l) return;
	if (ql <= l && qr >= r) {
		tag[cur] += val;
		return;
	}
	int mid = (l + r) / 2;
	modify(cur * 2, l, mid, ql, qr, val);
	modify(cur * 2 + 1, mid, r, ql, qr, val);
	seg[cur] = max(seg[cur * 2] + tag[cur * 2], seg[cur * 2 + 1] + tag[cur * 2 + 1]);
}
pii query(int cur, int l, int r) {
	if (r <= l) return make_pair(0, 0);
	if (r - l == 1) return make_pair(l, seg[cur] + tag[cur]);
	int mid = (l + r) / 2;
	if (seg[cur * 2] + tag[cur * 2] > seg[cur * 2 + 1] + tag[cur * 2 + 1]) {
		pii ret = query(cur * 2, l, mid);
		ret.ss += tag[cur];
		return ret;
	} else {
		pii ret = query(cur * 2 + 1, mid, r);
		ret.ss += tag[cur];
		return ret;
	}
}

int main() {
	io
	int n;
	cin >> n;
	ll tot = 0;
	for (int i = 0;i < n - 1;i++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		tot += c + d;
		adj[a].push_back(make_pair(b, c));
		adj[b].push_back(make_pair(a, d));
	}
	int q;
	cin >> q;
	dfs(1, 0);
	solve(1, 0);
	int p1 = bind[1];
	for (int j = 1;j <= n;j++) tup[j] = 0, up[j] = 0, best[j] = 0, bind[j] = 0, wei[j] = 0;

	ord = 0;
	dfs(p1, 0);
	ans[2] = tup[p1] + best[p1];
	int p2 = bind[p1], cur = p2;
	//p1 and p2 are diameter
	//cout << p1 <<" " << p2 << endl;
	//for (int i = 0;i < n;i++) cout << arr[i] << " " << wei[arr[i]] << endl;
	//cout << endl;
	for (int i = 1;i <= n;i++) {
		//cout << lef[i] << " " << rig[i] << " " << wei[i] << endl;
		modify(1, 0, n, lef[i], rig[i], wei[i]);
	}
	while (cur != p1) {
		done[cur] = 1;
		//cout << lef[cur] << " " << rig[cur] << " " << -wei[cur] << endl;
		modify(1, 0, n, lef[cur], rig[cur], -wei[cur]);
		cur = par[cur];
	}
	for (int i = 3;i <= n;i++) {
		pii leaf = query(1, 0, n);
		leaf.ff = arr[leaf.ff];
		//getweight(p1, 0, 0);
		if (leaf.ss == 0) {
			ans[i] = ans[i - 1];
		} else {
			ans[i] = ans[i - 1] + leaf.ss;
			cur = leaf.ff;
			while (!done[cur]) {
				done[cur] = 1;
				//cout << lef[cur] << " " << rig[cur] << " " << -wei[cur] << endl;
				modify(1, 0, n, lef[cur], rig[cur], -wei[cur]);
				cur = par[cur];
			}
		}
	}
	for (int i = 0;i < q;i++) {
		int t;
		cin >> t;
		cout << tot - ans[t] << "\n";
	}
}
/*
4
1 2 1 2
1 3 3 4
1 4 5 6
1
2

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

 */
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 5 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 633 ms 34364 KB Output is correct
3 Correct 555 ms 50144 KB Output is correct
4 Correct 569 ms 34104 KB Output is correct
5 Correct 563 ms 34144 KB Output is correct
6 Correct 619 ms 36320 KB Output is correct
7 Correct 541 ms 33500 KB Output is correct
8 Correct 686 ms 49388 KB Output is correct
9 Correct 515 ms 32352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 625 ms 34076 KB Output is correct
3 Correct 601 ms 50156 KB Output is correct
4 Correct 568 ms 34284 KB Output is correct
5 Correct 606 ms 34144 KB Output is correct
6 Correct 604 ms 36864 KB Output is correct
7 Correct 540 ms 32964 KB Output is correct
8 Correct 717 ms 44420 KB Output is correct
9 Correct 526 ms 32668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 5 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 7 ms 5356 KB Output is correct
14 Correct 7 ms 5612 KB Output is correct
15 Correct 6 ms 5356 KB Output is correct
16 Correct 8 ms 5356 KB Output is correct
17 Correct 6 ms 5356 KB Output is correct
18 Correct 7 ms 5356 KB Output is correct
19 Correct 6 ms 5356 KB Output is correct
20 Correct 8 ms 5484 KB Output is correct
21 Correct 8 ms 5484 KB Output is correct
22 Correct 7 ms 5356 KB Output is correct
23 Correct 6 ms 5356 KB Output is correct
24 Correct 7 ms 5356 KB Output is correct
25 Correct 7 ms 5484 KB Output is correct
26 Correct 6 ms 5356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 633 ms 34364 KB Output is correct
3 Correct 555 ms 50144 KB Output is correct
4 Correct 569 ms 34104 KB Output is correct
5 Correct 563 ms 34144 KB Output is correct
6 Correct 619 ms 36320 KB Output is correct
7 Correct 541 ms 33500 KB Output is correct
8 Correct 686 ms 49388 KB Output is correct
9 Correct 515 ms 32352 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 625 ms 34076 KB Output is correct
12 Correct 601 ms 50156 KB Output is correct
13 Correct 568 ms 34284 KB Output is correct
14 Correct 606 ms 34144 KB Output is correct
15 Correct 604 ms 36864 KB Output is correct
16 Correct 540 ms 32964 KB Output is correct
17 Correct 717 ms 44420 KB Output is correct
18 Correct 526 ms 32668 KB Output is correct
19 Correct 4 ms 5228 KB Output is correct
20 Correct 622 ms 34156 KB Output is correct
21 Correct 569 ms 50796 KB Output is correct
22 Correct 611 ms 35308 KB Output is correct
23 Correct 591 ms 35308 KB Output is correct
24 Correct 589 ms 35436 KB Output is correct
25 Correct 627 ms 35312 KB Output is correct
26 Correct 598 ms 35308 KB Output is correct
27 Correct 590 ms 35044 KB Output is correct
28 Correct 598 ms 37356 KB Output is correct
29 Correct 614 ms 35692 KB Output is correct
30 Correct 579 ms 34656 KB Output is correct
31 Correct 529 ms 33784 KB Output is correct
32 Correct 666 ms 45548 KB Output is correct
33 Correct 485 ms 32980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 5 ms 5100 KB Output is correct
8 Correct 4 ms 5100 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 633 ms 34364 KB Output is correct
14 Correct 555 ms 50144 KB Output is correct
15 Correct 569 ms 34104 KB Output is correct
16 Correct 563 ms 34144 KB Output is correct
17 Correct 619 ms 36320 KB Output is correct
18 Correct 541 ms 33500 KB Output is correct
19 Correct 686 ms 49388 KB Output is correct
20 Correct 515 ms 32352 KB Output is correct
21 Correct 4 ms 5100 KB Output is correct
22 Correct 625 ms 34076 KB Output is correct
23 Correct 601 ms 50156 KB Output is correct
24 Correct 568 ms 34284 KB Output is correct
25 Correct 606 ms 34144 KB Output is correct
26 Correct 604 ms 36864 KB Output is correct
27 Correct 540 ms 32964 KB Output is correct
28 Correct 717 ms 44420 KB Output is correct
29 Correct 526 ms 32668 KB Output is correct
30 Correct 4 ms 5100 KB Output is correct
31 Correct 7 ms 5356 KB Output is correct
32 Correct 7 ms 5612 KB Output is correct
33 Correct 6 ms 5356 KB Output is correct
34 Correct 8 ms 5356 KB Output is correct
35 Correct 6 ms 5356 KB Output is correct
36 Correct 7 ms 5356 KB Output is correct
37 Correct 6 ms 5356 KB Output is correct
38 Correct 8 ms 5484 KB Output is correct
39 Correct 8 ms 5484 KB Output is correct
40 Correct 7 ms 5356 KB Output is correct
41 Correct 6 ms 5356 KB Output is correct
42 Correct 7 ms 5356 KB Output is correct
43 Correct 7 ms 5484 KB Output is correct
44 Correct 6 ms 5356 KB Output is correct
45 Correct 4 ms 5228 KB Output is correct
46 Correct 622 ms 34156 KB Output is correct
47 Correct 569 ms 50796 KB Output is correct
48 Correct 611 ms 35308 KB Output is correct
49 Correct 591 ms 35308 KB Output is correct
50 Correct 589 ms 35436 KB Output is correct
51 Correct 627 ms 35312 KB Output is correct
52 Correct 598 ms 35308 KB Output is correct
53 Correct 590 ms 35044 KB Output is correct
54 Correct 598 ms 37356 KB Output is correct
55 Correct 614 ms 35692 KB Output is correct
56 Correct 579 ms 34656 KB Output is correct
57 Correct 529 ms 33784 KB Output is correct
58 Correct 666 ms 45548 KB Output is correct
59 Correct 485 ms 32980 KB Output is correct
60 Correct 4 ms 5100 KB Output is correct
61 Correct 643 ms 36316 KB Output is correct
62 Correct 706 ms 50932 KB Output is correct
63 Correct 617 ms 36204 KB Output is correct
64 Correct 670 ms 36340 KB Output is correct
65 Correct 653 ms 36076 KB Output is correct
66 Correct 700 ms 36360 KB Output is correct
67 Correct 680 ms 36228 KB Output is correct
68 Correct 630 ms 36196 KB Output is correct
69 Correct 679 ms 38616 KB Output is correct
70 Correct 658 ms 36588 KB Output is correct
71 Correct 589 ms 35936 KB Output is correct
72 Correct 600 ms 35932 KB Output is correct
73 Correct 690 ms 46424 KB Output is correct
74 Correct 528 ms 35796 KB Output is correct