QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#662011#8235. Top ClusterTomorrowWA 719ms230164kbC++172.7kb2024-10-20 20:03:262024-10-20 20:04:30

Judging History

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

  • [2024-10-20 20:04:30]
  • 评测
  • 测评结果:WA
  • 用时:719ms
  • 内存:230164kb
  • [2024-10-20 20:03:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define C const
#define OP operator
#define pb push_back
#define ft first
#define sd second
#define VD void
#define I int
#define LL long long
#define STR string
#define mi (l + (r - l) / 2)
#define BE(v) v.begin(), v.end()
#define TTT template<typename T = I>
using PII = pair<I, I>;
TTT using V = vector<T>;

#define FORX(v) for(auto& x : v)
#define FORV(v, i) for (I i = 0, _n = v.size(), x = _n ? v[0] : 0;i < _n;x = v[++i])
#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 FUNC(T, f, ...) function<T(__VA_ARGS__)> f = [&](__VA_ARGS__)->T

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 LCA
{
	I n, d; V<V<PII>> a{ {} }; V<I> id;
	LCA(V<V<I>>& e, V<V<I>>& g, V<LL>& dep, I rt = 1) :id(e.size())
	{
		FUNC(VD, dfs, I p, I fa, I dp)
		{
			id[p] = a[0].size(), a[0].pb({ dp,p });
			FORV(e[p], i)if (x != fa)dep[x] = dep[p] + g[p][i], 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 OP()(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 test()
{
	I n, q;cin >> n >> q;++n;
	V<I> p(n);
	V<V<I>> e(n), g(n);
	V<LL> dep(n);
	V<PII> pr(1);
	FOR(i, 1, n)
	{
		I w;cin >> w;
		if (w < n)p[w] = i;
	}
	FOR(i, 2, n)
	{
		I u, v, w; cin >> u >> v >> w;
		e[u].pb(v), g[u].pb(w);
		e[v].pb(u), g[v].pb(w);
	}
	LCA lca(e, g, dep);

	FUNC(VD, dm, I & u, I & v, I w, LL & d)
	{
		d = dep[u] + dep[v] - 2 * dep[lca(u, v)];
		LL du = dep[u] + dep[w] - 2 * dep[lca(u, w)];
		LL dv = dep[v] + dep[w] - 2 * dep[lca(v, w)];
		if (dv > du)swap(u, v), swap(du, dv);
		if (du > d)v = w, d = du;
	};

	FOR(i, 0, n)
	{
		if (p[i] == 0)break;
		if (i == 0) { pr.pb({ p[i],p[i] });continue; }
		pr.pb(pr.back());LL d;
		dm(pr.back().ft, pr.back().sd, p[i], d);
	}

	while (q--)
	{
		LL x, k;cin >> x >> k;
		I l = 0, r = n + 1;
		FUNC(I, check, I l)
		{
			if (l == 0)return 1;
			if(l>=pr.size())return 0;
			I u = pr[l].ft, v = pr[l].sd;
			LL d; dm(u, v, x, d);
			return d <= k;
		};
		while (l + 1 < r)(check(mi) ? l : r) = mi;
		cout << l << '\n';
	}
}

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;
	// cin >> t;
	while (t--)test();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3616kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 719ms
memory: 230164kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 8441st numbers differ - expected: '249999', found: '5556'