Submission #885756

# Submission time Handle Problem Language Result Execution time Memory
885756 2023-12-10T16:02:27 Z qthang2k11 Designated Cities (JOI19_designated_cities) C++17
100 / 100
380 ms 78420 KB
// [+]
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

struct Edge { // w: out, rw: in
	int y, w, rw;
	Edge() = default;
	Edge(int y, int w, int rw):
		y(y), w(w), rw(rw) {}
};

const int N = 2e5 + 5;

vector<Edge> adj[N];
int n;

ll tot = 0, ans1 = 0;

ll sum_out[N];

ll dfs(int x, int p) {
	for (const auto &elem: adj[x]) {
		int y = elem.y;
		if (y == p) continue;
		sum_out[x] += dfs(y, x) + elem.w;
	}
	return sum_out[x];
}

ll dfs1(int x, int p, ll up) {
	ll down = 0;
	for (const auto &elem: adj[x]) {
		int y = elem.y;
		if (y == p) continue;
		down += dfs1(y, x, up + sum_out[x] - sum_out[y] - elem.w + elem.rw) + elem.w;
	}
	ans1 = max(ans1, up + down);
	return down;
}

bool chosen[N];
ll ans[N];

int tin[N], tout[N], node[N], tcur = 0;
int par[N], rt;

int xtopar[N];

namespace e2 {
	const ll INF = 1e18;
	
	array<ll, 3> res = {0, 0, 0};
	
	pair<ll, int> mx_up[N];
	
	pair<ll, int> dfs1(int x, int p, ll up) {
		pair<ll, int> &fst = mx_up[x], scd, tmp, now;

		scd = tmp = {-INF, 0};
		fst = {0, x};
		
		for (const auto &elem: adj[x]) {
			int y = elem.y, w = elem.w, rw = elem.rw;
			if (y == p) continue;
			now = dfs1(y, x, up + sum_out[x] - sum_out[y] - w + rw);
			tmp = max(tmp, {now.first + sum_out[x] - sum_out[y] + w + rw, now.second});
			scd = max(scd, {mx_up[y].first + rw, mx_up[y].second});
			if (scd > fst) swap(scd, fst);
		}
		
		res = max(res, {up + fst.first + scd.first + sum_out[x], fst.second, scd.second});
		
		return tmp;
	}
	
	void dfs2(int x, int p) {
		tin[x] = ++tcur;
		node[tcur] = x;
		par[x] = p;
		for (const auto &elem: adj[x]) {
			int y = elem.y;
			if (y == p) continue;
			dfs2(y, x);
			xtopar[y] = elem.rw;
		}
		tout[x] = tcur;
	}
		
	void solve() {
		auto now = dfs1(1, 0, 0);
		res = max(res, {now.first, 1, now.second});
		
		ans[2] = tot - res[0];
		rt = res[1];
		
		dfs2(rt, 0);
				
		int cur = res[2];
		while (cur) {
			chosen[cur] = 1;
			cur = par[cur];
		}
	}
}

ll dis[N];

void dfs2(int x, int p, ll d) {
	if (chosen[x]) d = 0;
	dis[x] = d;
	
	for (const auto &elem: adj[x]) {
		int y = elem.y;
		if (y == p) continue;
		dfs2(y, x, d + elem.rw);
	}
}

struct Node {
	ll val; int x; ll lz;
	Node() { val = x = lz = 0; }
	Node(ll val, int x, ll lz = 0):
		val(val), x(x), lz(lz) {}
};

Node merge(const Node &x, const Node &y) {
	if (x.val > y.val) return Node(x.val, x.x);
	return Node(y.val, y.x);
}

struct SegmentTree {
	Node IT[N << 2];
	SegmentTree() = default;
	
	void build(int id, int l, int r) {
		if (l == r) return IT[id] = Node(dis[node[l]], node[l]), void();
		int mid = (l + r) / 2;
		build(id << 1, l, mid);
		build(id << 1 | 1, mid + 1, r);
		IT[id] = merge(IT[id << 1], IT[id << 1 | 1]);
	}
	
	inline void apply(int id, ll w) {
		IT[id].val += w;
		IT[id].lz += w;
	}
	
	inline void down(int id) {
		ll &lz = IT[id].lz;
		if (lz) {
			apply(id << 1, lz);
			apply(id << 1 | 1, lz);
			lz = 0;
		}
	}
	
	void update(int x, int y, ll w, int id, int l, int r) {
		if (l > y || r < x) return;
		if (x <= l && r <= y) 
			return apply(id, w);
		down(id);
		int mid = (l + r) / 2;
		update(x, y, w, id << 1, l, mid);
		update(x, y, w, id << 1 | 1, mid + 1, r);
		IT[id] = merge(IT[id << 1], IT[id << 1 | 1]);
	}
	
	inline void update(int x, int y, ll w) {
		update(x, y, w, 1, 1, n);
	}
	
	void update_val(int x, int id, int l, int r) {
		if (l == r) return IT[id] = Node(0, x), void();
		down(id);
		int mid = (l + r) / 2;
		if (x <= mid) update_val(x, id << 1, l, mid);
		else update_val(x, id << 1 | 1, mid + 1, r);
		IT[id] = merge(IT[id << 1], IT[id << 1 | 1]);
	}
	
	inline void update_val(int x) {
		update_val(x, 1, 1, n);
	}
	
	pair<ll, int> get() {
		return {IT[1].val, IT[1].x};
	}
	
	ll debug(int x, int id, int l, int r) {
		if (l == r) return IT[id].val;
		down(id);
		int mid = (l + r) / 2;
		if (x <= mid) return debug(x, id << 1, l, mid);
		else return debug(x, id << 1 | 1, mid + 1, r);
	}
	
} seg;

void del(int x) {
	assert(!chosen[x]);
	
	vector<int> arr;
	while (!chosen[x]) {
		chosen[x] = 1;
		arr.emplace_back(x);
		x = par[x];
	}
	
	reverse(arr.begin(), arr.end());
	
	ll pref = 0;
	for (int x: arr) {
		pref += xtopar[x];
		for (const auto &elem: adj[x]) {
			int y = elem.y;
			if (chosen[y]) continue;
			seg.update(tin[y], tout[y], -pref);
		}
		seg.update_val(tin[x]);
	}
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	
	cin >> n; 
	for (int i = 1; i < n; i++) {
		int x, y, w, rw;
		cin >> x >> y >> rw >> w;
		adj[x].emplace_back(y, w, rw);
		adj[y].emplace_back(x, rw, w);
		tot += w + rw;
	}
	
	ignore = dfs(1, 0);
	
	ignore = dfs1(1, 0, 0);
	ans[1] = tot - ans1;
		
	e2::solve(); // case E = 2
	
	dfs2(rt, 0, 0);
	seg.build(1, 1, n);
	
	ll val; int node;
	for (int i = 3; i <= n; i++) {
		ans[i] = ans[i - 1];		
		tie(val, node) = seg.get();
		if (val <= 0) {
			for (int j = i + 1; j <= n; j++)
				ans[j] = ans[j - 1];
			break;
		}
		ans[i] -= val;
		del(node);
	}

	int q; 	
	cin >> q;
	while (q--) {
		int x; cin >> x;
		cout << ans[x] << '\n';
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29528 KB Output is correct
2 Correct 5 ms 29532 KB Output is correct
3 Correct 5 ms 29660 KB Output is correct
4 Correct 5 ms 29752 KB Output is correct
5 Correct 5 ms 29532 KB Output is correct
6 Correct 5 ms 29620 KB Output is correct
7 Correct 5 ms 29532 KB Output is correct
8 Correct 5 ms 29532 KB Output is correct
9 Correct 5 ms 29532 KB Output is correct
10 Correct 5 ms 29532 KB Output is correct
11 Correct 5 ms 29532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29532 KB Output is correct
2 Correct 321 ms 44132 KB Output is correct
3 Correct 238 ms 67396 KB Output is correct
4 Correct 253 ms 44112 KB Output is correct
5 Correct 259 ms 43956 KB Output is correct
6 Correct 366 ms 47700 KB Output is correct
7 Correct 230 ms 43904 KB Output is correct
8 Correct 290 ms 68436 KB Output is correct
9 Correct 190 ms 44280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29532 KB Output is correct
2 Correct 359 ms 44092 KB Output is correct
3 Correct 242 ms 71708 KB Output is correct
4 Correct 291 ms 44112 KB Output is correct
5 Correct 285 ms 43788 KB Output is correct
6 Correct 318 ms 47928 KB Output is correct
7 Correct 226 ms 44720 KB Output is correct
8 Correct 278 ms 59780 KB Output is correct
9 Correct 203 ms 44256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29528 KB Output is correct
2 Correct 5 ms 29532 KB Output is correct
3 Correct 5 ms 29660 KB Output is correct
4 Correct 5 ms 29752 KB Output is correct
5 Correct 5 ms 29532 KB Output is correct
6 Correct 5 ms 29620 KB Output is correct
7 Correct 5 ms 29532 KB Output is correct
8 Correct 5 ms 29532 KB Output is correct
9 Correct 5 ms 29532 KB Output is correct
10 Correct 5 ms 29532 KB Output is correct
11 Correct 5 ms 29532 KB Output is correct
12 Correct 5 ms 29528 KB Output is correct
13 Correct 6 ms 29784 KB Output is correct
14 Correct 6 ms 30044 KB Output is correct
15 Correct 8 ms 29788 KB Output is correct
16 Correct 6 ms 29784 KB Output is correct
17 Correct 6 ms 29788 KB Output is correct
18 Correct 6 ms 29788 KB Output is correct
19 Correct 6 ms 29788 KB Output is correct
20 Correct 6 ms 29732 KB Output is correct
21 Correct 7 ms 29784 KB Output is correct
22 Correct 6 ms 29928 KB Output is correct
23 Correct 7 ms 29928 KB Output is correct
24 Correct 6 ms 29968 KB Output is correct
25 Correct 6 ms 30044 KB Output is correct
26 Correct 6 ms 29788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29532 KB Output is correct
2 Correct 321 ms 44132 KB Output is correct
3 Correct 238 ms 67396 KB Output is correct
4 Correct 253 ms 44112 KB Output is correct
5 Correct 259 ms 43956 KB Output is correct
6 Correct 366 ms 47700 KB Output is correct
7 Correct 230 ms 43904 KB Output is correct
8 Correct 290 ms 68436 KB Output is correct
9 Correct 190 ms 44280 KB Output is correct
10 Correct 5 ms 29532 KB Output is correct
11 Correct 359 ms 44092 KB Output is correct
12 Correct 242 ms 71708 KB Output is correct
13 Correct 291 ms 44112 KB Output is correct
14 Correct 285 ms 43788 KB Output is correct
15 Correct 318 ms 47928 KB Output is correct
16 Correct 226 ms 44720 KB Output is correct
17 Correct 278 ms 59780 KB Output is correct
18 Correct 203 ms 44256 KB Output is correct
19 Correct 5 ms 29532 KB Output is correct
20 Correct 302 ms 44248 KB Output is correct
21 Correct 264 ms 78420 KB Output is correct
22 Correct 310 ms 49144 KB Output is correct
23 Correct 262 ms 50580 KB Output is correct
24 Correct 268 ms 49388 KB Output is correct
25 Correct 346 ms 50600 KB Output is correct
26 Correct 291 ms 49260 KB Output is correct
27 Correct 249 ms 50320 KB Output is correct
28 Correct 316 ms 53760 KB Output is correct
29 Correct 253 ms 50608 KB Output is correct
30 Correct 315 ms 49664 KB Output is correct
31 Correct 229 ms 50356 KB Output is correct
32 Correct 293 ms 67152 KB Output is correct
33 Correct 209 ms 50952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29528 KB Output is correct
2 Correct 5 ms 29532 KB Output is correct
3 Correct 5 ms 29660 KB Output is correct
4 Correct 5 ms 29752 KB Output is correct
5 Correct 5 ms 29532 KB Output is correct
6 Correct 5 ms 29620 KB Output is correct
7 Correct 5 ms 29532 KB Output is correct
8 Correct 5 ms 29532 KB Output is correct
9 Correct 5 ms 29532 KB Output is correct
10 Correct 5 ms 29532 KB Output is correct
11 Correct 5 ms 29532 KB Output is correct
12 Correct 6 ms 29532 KB Output is correct
13 Correct 321 ms 44132 KB Output is correct
14 Correct 238 ms 67396 KB Output is correct
15 Correct 253 ms 44112 KB Output is correct
16 Correct 259 ms 43956 KB Output is correct
17 Correct 366 ms 47700 KB Output is correct
18 Correct 230 ms 43904 KB Output is correct
19 Correct 290 ms 68436 KB Output is correct
20 Correct 190 ms 44280 KB Output is correct
21 Correct 5 ms 29532 KB Output is correct
22 Correct 359 ms 44092 KB Output is correct
23 Correct 242 ms 71708 KB Output is correct
24 Correct 291 ms 44112 KB Output is correct
25 Correct 285 ms 43788 KB Output is correct
26 Correct 318 ms 47928 KB Output is correct
27 Correct 226 ms 44720 KB Output is correct
28 Correct 278 ms 59780 KB Output is correct
29 Correct 203 ms 44256 KB Output is correct
30 Correct 5 ms 29528 KB Output is correct
31 Correct 6 ms 29784 KB Output is correct
32 Correct 6 ms 30044 KB Output is correct
33 Correct 8 ms 29788 KB Output is correct
34 Correct 6 ms 29784 KB Output is correct
35 Correct 6 ms 29788 KB Output is correct
36 Correct 6 ms 29788 KB Output is correct
37 Correct 6 ms 29788 KB Output is correct
38 Correct 6 ms 29732 KB Output is correct
39 Correct 7 ms 29784 KB Output is correct
40 Correct 6 ms 29928 KB Output is correct
41 Correct 7 ms 29928 KB Output is correct
42 Correct 6 ms 29968 KB Output is correct
43 Correct 6 ms 30044 KB Output is correct
44 Correct 6 ms 29788 KB Output is correct
45 Correct 5 ms 29532 KB Output is correct
46 Correct 302 ms 44248 KB Output is correct
47 Correct 264 ms 78420 KB Output is correct
48 Correct 310 ms 49144 KB Output is correct
49 Correct 262 ms 50580 KB Output is correct
50 Correct 268 ms 49388 KB Output is correct
51 Correct 346 ms 50600 KB Output is correct
52 Correct 291 ms 49260 KB Output is correct
53 Correct 249 ms 50320 KB Output is correct
54 Correct 316 ms 53760 KB Output is correct
55 Correct 253 ms 50608 KB Output is correct
56 Correct 315 ms 49664 KB Output is correct
57 Correct 229 ms 50356 KB Output is correct
58 Correct 293 ms 67152 KB Output is correct
59 Correct 209 ms 50952 KB Output is correct
60 Correct 5 ms 29744 KB Output is correct
61 Correct 380 ms 52948 KB Output is correct
62 Correct 338 ms 75604 KB Output is correct
63 Correct 315 ms 51796 KB Output is correct
64 Correct 325 ms 53076 KB Output is correct
65 Correct 282 ms 51820 KB Output is correct
66 Correct 295 ms 52968 KB Output is correct
67 Correct 376 ms 52020 KB Output is correct
68 Correct 280 ms 53124 KB Output is correct
69 Correct 355 ms 56012 KB Output is correct
70 Correct 355 ms 53028 KB Output is correct
71 Correct 296 ms 52384 KB Output is correct
72 Correct 303 ms 53472 KB Output is correct
73 Correct 305 ms 67036 KB Output is correct
74 Correct 255 ms 55368 KB Output is correct