답안 #1086119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086119 2024-09-09T15:28:34 Z ymm Designated Cities (JOI19_designated_cities) C++17
100 / 100
374 ms 65324 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 200'010;
vector<pii> A[N];
int par[N];
ll vup[N], vdown[N], sup[N], sdown[N];
int height[N];
int st[N], ft[N];
int stp[N];
int n;

void dfs(int v, int p, ll vp, int &t, int h)
{
	vup[v] = vdown[v] = sup[v] = sdown[v] = 0;
	height[v] = h;
	par[v] = p;
	for (auto [u, w] : A[v])
		if (u == p)
			vup[v] = w;
	vdown[v] = vp;
	sup[v] = vup[v];
	sdown[v] = vdown[v];
	if (p != -1) {
		sup[v] += sup[p];
		sdown[v] += sdown[p];
	}
	st[v] = t++;
	stp[st[v]] = v;
	for (auto [u, w] : A[v]) {
		if (u == p)
			continue;
		dfs(u, v, w, t, h+1);
	}
	ft[v] = t;
}

struct Seg {
	struct Node {
		ll val;
		ll mx;
	};
	vector<Node> t;
	int sz;

	Seg(int n) {
		t.resize(n*4);
		sz = n;
	}

	void add(int l, int r, ll x, int i, int b, int e) {
		if (l <= b && e <= r) {
			t[i].val += x;
			t[i].mx += x;
			return;
		}
		if (r <= b || e <= l)
			return;
		add(l, r, x, 2*i+1, b, (b+e)/2);
		add(l, r, x, 2*i+2, (b+e)/2, e);
		t[i].mx = max(t[2*i+1].mx, t[2*i+2].mx) + t[i].val;
	}
	void add(int l, int r, ll x) { if (l < r) add(l, r, x, 0, 0, sz); }

	int getmax(int i, int b, int e) {
		if (e-b == 1)
			return b;
		if (t[2*i+1].mx + t[i].val == t[i].mx)
			return getmax(2*i+1, b, (b+e)/2);
		else
			return getmax(2*i+2, (b+e)/2, e);
	}
	pll getmax() { return {getmax(0, 0, sz), t[0].mx}; }

	ll maxval(int l, int r, int i, int b, int e) {
		if (l <= b && e <= r)
			return t[i].mx;
		if (r <= b || e <= l)
			return LONG_LONG_MIN;
		return max(maxval(l, r, 2*i+1, b, (b+e)/2),
		           maxval(l, r, 2*i+2, (b+e)/2, e)) + t[i].val;
	}
	ll maxval(int l, int r) { return (l < r? maxval(l, r, 0, 0, sz): -1e18); }
};

ll greedy_val[N];
int barkh[N];
bool vis[N];
vector<int> greedy_ord;

void first_greedy()
{
	Seg seg(n);
	Loop (i,0,n)
		seg.add(st[i], ft[i], vdown[i]);
	Loop (i,0,n) {
		auto [stv, w] = seg.getmax();
		int v = stp[stv];
		int vs = v;
		greedy_ord.push_back(v);
		greedy_val[v] = w;
		seg.add(st[v], st[v] + 1, -1);
		while (v != -1 && !vis[v]) {
			seg.add(st[v], ft[v], -vdown[v]);
			vis[v] = 1;
			v = par[v];
		}
		barkh[vs] = v;
	}
}

ll ans[N];

void solve1()
{
	ans[1] = 0;
	Loop (i,0,n)
		ans[1] = max(ans[1], sdown[i] - sup[i]);
}

void solve()
{
	int v = greedy_ord[0];
	vector<ll> vallca;
	vector<ll> supv;
	{
		Seg seg(n);
		Loop (i,0,n)
			seg.add(st[i], ft[i], vdown[i]);
		int l = st[v], r = ft[v];
		supv.push_back(sup[v]);
		for (int u = par[v]; u != -1; u = par[u]) {
			ll val = max(seg.maxval(st[u], l), seg.maxval(r, ft[u]));
			val -= sdown[u] + sup[u];
			vallca.push_back(val);
			supv.push_back(sup[u]);
			l = st[u];
			r = ft[u];
		}
		reverse(vallca.begin(), vallca.end());
		reverse(supv.begin(), supv.end());
		Loop (i,0,vallca.size()-1)
			vallca[i+1] = max(vallca[i], vallca[i+1]);
	}
	assert(greedy_val[v] == sdown[v]);
	assert(supv[vallca.size()] == sup[v]);
	ll sum = greedy_val[v];
	Loop (i,1,n) {
		ans[i] = max(ans[i], sum - supv[vallca.size()]);
		if (vallca.size())
			ans[i+1] = max(ans[i+1], sum + vallca.back());
		int u = greedy_ord[i];
		while ((ll)vallca.size() > height[barkh[u]])
			vallca.pop_back();
		sum += greedy_val[u];
	}
	ans[n] = sum;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n;
	Loop (i,1,n) {
		int v, u, c, d;
		cin >> v >> u >> c >> d;
		--v; --u;
		A[v].emplace_back(u, c);
		A[u].emplace_back(v, d);
	}
	int dummy = 0;
	dfs(0, -1, 0, dummy, 0);
	first_greedy();
	solve1();
	solve();
	int q;
	cin >> q;
	ll sumd = 0;
	Loop (i,0,n)
		sumd += vdown[i];
	while (q--) {
		int e;
		cin >> e;
		cout << sumd - ans[e] << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5168 KB Output is correct
5 Correct 2 ms 5172 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5176 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 299 ms 46344 KB Output is correct
3 Correct 332 ms 62672 KB Output is correct
4 Correct 294 ms 45036 KB Output is correct
5 Correct 285 ms 46424 KB Output is correct
6 Correct 301 ms 48848 KB Output is correct
7 Correct 281 ms 46452 KB Output is correct
8 Correct 371 ms 63188 KB Output is correct
9 Correct 284 ms 48000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 294 ms 46320 KB Output is correct
3 Correct 302 ms 64940 KB Output is correct
4 Correct 278 ms 44916 KB Output is correct
5 Correct 284 ms 46248 KB Output is correct
6 Correct 302 ms 49108 KB Output is correct
7 Correct 282 ms 47568 KB Output is correct
8 Correct 374 ms 57056 KB Output is correct
9 Correct 288 ms 47692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5168 KB Output is correct
5 Correct 2 ms 5172 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5176 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 5 ms 5656 KB Output is correct
14 Correct 4 ms 5724 KB Output is correct
15 Correct 4 ms 5468 KB Output is correct
16 Correct 4 ms 5468 KB Output is correct
17 Correct 4 ms 5468 KB Output is correct
18 Correct 4 ms 5504 KB Output is correct
19 Correct 4 ms 5440 KB Output is correct
20 Correct 4 ms 5436 KB Output is correct
21 Correct 5 ms 5440 KB Output is correct
22 Correct 4 ms 5464 KB Output is correct
23 Correct 4 ms 5468 KB Output is correct
24 Correct 4 ms 5564 KB Output is correct
25 Correct 5 ms 5724 KB Output is correct
26 Correct 5 ms 5456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 299 ms 46344 KB Output is correct
3 Correct 332 ms 62672 KB Output is correct
4 Correct 294 ms 45036 KB Output is correct
5 Correct 285 ms 46424 KB Output is correct
6 Correct 301 ms 48848 KB Output is correct
7 Correct 281 ms 46452 KB Output is correct
8 Correct 371 ms 63188 KB Output is correct
9 Correct 284 ms 48000 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 294 ms 46320 KB Output is correct
12 Correct 302 ms 64940 KB Output is correct
13 Correct 278 ms 44916 KB Output is correct
14 Correct 284 ms 46248 KB Output is correct
15 Correct 302 ms 49108 KB Output is correct
16 Correct 282 ms 47568 KB Output is correct
17 Correct 374 ms 57056 KB Output is correct
18 Correct 288 ms 47692 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 311 ms 46264 KB Output is correct
21 Correct 357 ms 65324 KB Output is correct
22 Correct 300 ms 44972 KB Output is correct
23 Correct 284 ms 46296 KB Output is correct
24 Correct 291 ms 45116 KB Output is correct
25 Correct 277 ms 46292 KB Output is correct
26 Correct 275 ms 45268 KB Output is correct
27 Correct 275 ms 46296 KB Output is correct
28 Correct 290 ms 48604 KB Output is correct
29 Correct 285 ms 46552 KB Output is correct
30 Correct 286 ms 45516 KB Output is correct
31 Correct 272 ms 46416 KB Output is correct
32 Correct 336 ms 57816 KB Output is correct
33 Correct 257 ms 48080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5168 KB Output is correct
5 Correct 2 ms 5172 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5176 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 299 ms 46344 KB Output is correct
14 Correct 332 ms 62672 KB Output is correct
15 Correct 294 ms 45036 KB Output is correct
16 Correct 285 ms 46424 KB Output is correct
17 Correct 301 ms 48848 KB Output is correct
18 Correct 281 ms 46452 KB Output is correct
19 Correct 371 ms 63188 KB Output is correct
20 Correct 284 ms 48000 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 294 ms 46320 KB Output is correct
23 Correct 302 ms 64940 KB Output is correct
24 Correct 278 ms 44916 KB Output is correct
25 Correct 284 ms 46248 KB Output is correct
26 Correct 302 ms 49108 KB Output is correct
27 Correct 282 ms 47568 KB Output is correct
28 Correct 374 ms 57056 KB Output is correct
29 Correct 288 ms 47692 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
31 Correct 5 ms 5656 KB Output is correct
32 Correct 4 ms 5724 KB Output is correct
33 Correct 4 ms 5468 KB Output is correct
34 Correct 4 ms 5468 KB Output is correct
35 Correct 4 ms 5468 KB Output is correct
36 Correct 4 ms 5504 KB Output is correct
37 Correct 4 ms 5440 KB Output is correct
38 Correct 4 ms 5436 KB Output is correct
39 Correct 5 ms 5440 KB Output is correct
40 Correct 4 ms 5464 KB Output is correct
41 Correct 4 ms 5468 KB Output is correct
42 Correct 4 ms 5564 KB Output is correct
43 Correct 5 ms 5724 KB Output is correct
44 Correct 5 ms 5456 KB Output is correct
45 Correct 2 ms 5212 KB Output is correct
46 Correct 311 ms 46264 KB Output is correct
47 Correct 357 ms 65324 KB Output is correct
48 Correct 300 ms 44972 KB Output is correct
49 Correct 284 ms 46296 KB Output is correct
50 Correct 291 ms 45116 KB Output is correct
51 Correct 277 ms 46292 KB Output is correct
52 Correct 275 ms 45268 KB Output is correct
53 Correct 275 ms 46296 KB Output is correct
54 Correct 290 ms 48604 KB Output is correct
55 Correct 285 ms 46552 KB Output is correct
56 Correct 286 ms 45516 KB Output is correct
57 Correct 272 ms 46416 KB Output is correct
58 Correct 336 ms 57816 KB Output is correct
59 Correct 257 ms 48080 KB Output is correct
60 Correct 2 ms 5212 KB Output is correct
61 Correct 295 ms 49092 KB Output is correct
62 Correct 305 ms 64468 KB Output is correct
63 Correct 287 ms 47792 KB Output is correct
64 Correct 303 ms 49108 KB Output is correct
65 Correct 300 ms 47572 KB Output is correct
66 Correct 294 ms 49104 KB Output is correct
67 Correct 294 ms 47548 KB Output is correct
68 Correct 289 ms 49200 KB Output is correct
69 Correct 296 ms 51164 KB Output is correct
70 Correct 303 ms 48888 KB Output is correct
71 Correct 293 ms 48216 KB Output is correct
72 Correct 292 ms 49704 KB Output is correct
73 Correct 330 ms 58592 KB Output is correct
74 Correct 284 ms 52176 KB Output is correct