QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#585888#7103. Red Black TreeTomorrowTL 1ms3776kbC++203.5kb2024-09-23 22:42:082024-09-23 22:42:09

Judging History

你现在查看的是最新测评结果

  • [2024-09-23 22:42:09]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3776kb
  • [2024-09-23 22:42:08]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define C const
#define U unsigned
#define SC static
#define CE constexpr
#define TL template
#define TN typename
#define OP operator
#define pb push_back
#define ft first
#define sd second
#define mi (l + (r - l) / 2)
#define TTT TL<TN T = I>
#define TTI TL<TN It>
#define BE(v) v.begin(), v.end()
#define VD void
#define BL bool
#define CH char
#define I int
#define LL long long
#define II __int128
#define D double
using PII = pair<I, I>;
TTT using V = vector<T>;

#define FORX(v) for(auto& x : v)
#define FOR(i, b, e)  for(auto i = b, _e = (decltype(i))e;i < _e; ++i)
#define FORR(i, b, e) for(auto i = b, _e = (decltype(i))e;i > _e; --i)
#define FORV(v, i) for (I i = 0, _n = v.size(), x = _n ? v[0] : 0;i < _n;x = v[++i])
#define FUNC(T, f, ...) function<T(__VA_ARGS__)> f = [&](__VA_ARGS__)->T

#define DR(T, ...) T __VA_ARGS__; R(__VA_ARGS__);
TTT VD R(T& x) { cin >> x; }
TTT T R() { DR(T, x); return x; }
TL<TN T, TN... A> VD R(T& x, A&... a) { R(x), R(a...); }
CE CH LF = '\n', SP = ' ';
TL<CH s = LF> VD W() { cout << s; }
TL<CH s = LF, TN T> VD W(C T& x) { cout << x << s; }
TL<CH s = LF, TN T, TN... A> VD W(C T& x, C A&... a) { W<SP>(x), W<s>(a...); }

TTT CE T inf() { return numeric_limits<T>::max() / 2; }
TTT VD setmin(T& a, C T& b) { if (b < a)a = b; }
TTT VD setmax(T& a, C T& b) { if (a < b)a = b; }

struct VT//VirtualTree
{
	I n, d, rt; V<V<PII>> a{ {} }; V<I> id;
	VT(C V<V<I>>& e, I rt = 1) :id(e.size()), rt(rt)
	{
		FUNC(VD, dfs, I p, I fa, I dp)
		{
			id[p] = a[0].size(), a[0].pb({ dp,p });
			FORX(e[p])if (x != fa)dfs(x, p, dp + 1), a[0].pb({ dp,p });
		};
		dfs(rt, -1, 0);
		n = a[0].size(); d = ilogb(n); a.resize(d + 1, V<PII>(n));
		FOR(k, 0, d)FOR(i, 0, n - (1 << k))a[k + 1][i] = min(a[k][i], a[k][i + (1 << k)]);
	}
	I lca(I x, I y)
	{
		if (id[x] > id[y])swap(x, y);
		x = id[x], y = id[y] + 1; I k = ilogb(y - x);
		return min(a[k][x], a[k][y - (1 << k)]).sd;
	}
	VD OP()(V<I> a, V<V<I>>& e)
	{
		sort(BE(a), [&](I x, I y) {return id[x] < id[y];});
		V<I> s(a.size() + 1); s[0] = rt; I m = 0; e[rt].clear();
		FORX(a)
		{
			if (x == rt)continue;
			I l = lca(x, s[m]);
			if (l != s[m])
			{
				while (id[l] < id[s[m - 1]])e[s[m - 1]].pb(s[m]), --m;
				if (id[l] > id[s[m - 1]])e[l].assign({ s[m] }), s[m] = l;
				else e[l].pb(s[m--]);
			}
			e[x].clear(); s[++m] = x;
		}
		while (m--)e[s[m]].pb(s[m + 1]);
	}
};

VD test()
{
	DR(I, n, m, q); ++n;
	V<I> isr(n), is(n); V<LL> dp(n);
	V<V<I>> te(n), e(n); V<V<LL>> tg(n);
	while (m--)isr[R<I>()] = 1;
	FOR(i, 2, n)
	{
		DR(I, u, v, w);
		te[u].pb(v), tg[u].pb(w);
		te[v].pb(u), tg[v].pb(w);
	}
	FUNC(VD, dfs, I p, I fa) { FORV(te[p], i)if (x != fa)dp[x] = dp[p] + tg[p][i], dfs(x, p); };
	dfs(1, 0);
	VT vt(te);
	FUNC(BL, check, LL lim)
	{
		// if (lim == -1)return false;
		I cnt = 0;
		FUNC(LL, dfs, I p, I fa)
		{
			LL mxd = dp[p];
			FORX(e[p])if (x != fa)setmax(mxd, dfs(x, p));
			if (isr[p])return 0;
			if (mxd - dp[fa] > lim) { ++cnt;return 0; }
			return mxd;
		};
		dfs(1, 0);
		return cnt <= 1;
	};
	while (q--)
	{
		V<I> a; DR(I, k);while (k--) { DR(I, x);a.pb(x); }
		vt(a, e);
		LL l = -1, r = inf<LL>();
		while (l + 1 < r)(!check(mi) ? l : r) = mi;
		W(r);
	}
}

I main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	if (fopen("test.in", "r"))
	{
		freopen("test.in", "r", stdin);
		freopen("test.out", "w", stdout);
	}
	I t = 1;
	R(t);
	while (t--)test();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3776kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: -100
Time Limit Exceeded

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
345857037
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result: