QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#338465#8235. Top ClusterFISHER_WA 738ms205684kbC++201.9kb2024-02-25 22:21:402024-02-25 22:21:41

Judging History

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

  • [2024-02-25 22:21:41]
  • 评测
  • 测评结果:WA
  • 用时:738ms
  • 内存:205684kb
  • [2024-02-25 22:21:40]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define PB push_back
#define EB emplace_back
using namespace std;
typedef long long i64;
const int maxn = 500000;
int w[maxn + 5];
struct edge { int v, w; };
vector<edge> g[maxn + 5];
i64 dep[maxn + 5];
pair<i64, int> mn[maxn + 5][20];
int lg2[maxn + 5];
int dfn[maxn + 5], stamp;
void dfs(int u, int fa) {
	mn[dfn[u] = ++stamp][0] = {dep[u], fa};
	for (const auto &[v, w] : g[u])
		if (v != fa) {
			dep[v] = dep[u] + w;
			dfs(v, u);
		}
}
inline int lca(int u, int v) {
	if (u == v) return u;
	u = dfn[u], v = dfn[v];
	if (u > v) swap(u, v);
	int t = lg2[v - u++];
	return min(mn[u][t], mn[v - (1 << t) + 1][t]).se;
}
inline int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
int id[maxn + 5];
int p0[maxn + 5], p1[maxn + 5];
int main() {
	int n, q;
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &w[i]);
		if (w[i] < n) id[w[i]] = i;
	}
	for (int i = 1; i < n; i++) {
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		g[u].PB({v, w}), g[v].PB({u, w});
	}
	dfs(1, 0);
	for (int i = n; i; i--)
		for (int j = 1; i + (1 << j) - 1 <= n; j++) mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
	for (int i = 2; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
	int mx;
	for (int i = 0; i <= n; i++) {
		if (!id[i]) { mx = i; break; }
		if (!i) p0[i] = p1[i] = id[i];
		else {
			i64 lst = dis(p0[i - 1], p1[i - 1]), n0 = dis(p0[i - 1], id[i]), n1 = dis(p1[i - 1], id[i]);
			if (lst >= n0 && lst >= n1) p0[i] = p0[i - 1], p1[i] = p1[i - 1];
			else if (n0 >= lst && n0 >= n1) p0[i] = p0[i - 1], p1[i] = id[i];
			else p0[i] = id[i], p1[i] = p1[i - 1];
		}
	}
	for (int i = 1; i <= q; i++) {
		int u; i64 k;
		scanf("%d%lld", &u, &k);
		int l = 0, r = mx;
		while (l < r) {
			int mid = (l + r + 1) / 2;
			if (dis(u, p0[mid - 1]) > k || dis(u, p1[mid - 1]) > k) r = mid - 1;
			else l = mid;
		}
		printf("%d\n", l);
	}
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 719ms
memory: 205684kb

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:

ok 500000 numbers

Test #3:

score: -100
Wrong Answer
time: 738ms
memory: 205656kb

input:

500000 500000
416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...

output:

59
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250...

result:

wrong answer 1st numbers differ - expected: '0', found: '59'