QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729235#8235. Top Clusterveg#TL 0ms27960kbC++142.9kb2024-11-09 16:45:202024-11-09 16:45:30

Judging History

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

  • [2024-11-09 16:45:30]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:27960kb
  • [2024-11-09 16:45:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Node {
	int ls, rs, mn;
} t[10000005];
const int N = 5e5 + 5;
int siz[N], mx[N], tot, ans[N], seg_rt, a[N], vis[N], ncnt, n, q, rt;
vector<pair<int, int> > G[N];
vector<pair<ll, int> > Q[N];
void ins(int &p, ll l, ll r, ll x, int y) {
	if (p == 0) {
		p = ++ncnt;
		t[p].ls = t[p].rs = 0;
		t[p].mn = 1e9;
	}
	if (l == r) {
		t[p].mn = min(t[p].mn, y);
		return;
	}
	ll mid = l + r >> 1;
	if (x <= mid) ins(t[p].ls, l, mid, x, y);
	else ins(t[p].rs, mid + 1, r, x, y);
	t[p].mn = 1e9;
	if (t[p].ls) t[p].mn = min(t[p].mn, t[t[p].ls].mn);
	if (t[p].rs) t[p].mn = min(t[p].mn, t[t[p].rs].mn);
}
int ask(int p, ll l, ll r, ll x) {
	if (r <= x || p == 0) return 1e9;
	if (l > x) return t[p].mn;
	ll mid = l + r >> 1;
	return min(ask(t[p].ls, l, mid, x), ask(t[p].rs, mid + 1, r, x));
}
void get_rt(int u, int fa) {
	siz[u] = 1;
	mx[u] = 0;
	for (auto e : G[u]) {
		int v = e.first;
		if (v == fa || vis[v]) continue;
		get_rt(v, u);
		siz[u] += siz[v];
		if (siz[v] > mx[u]) mx[u] = siz[v];
	}
	mx[u] = max(mx[u], tot - siz[u]);
	if (rt == 0 || mx[u] < mx[rt]) rt = u;
}

void get_ans(int u, int fa, ll d) {
	for (auto q : Q[u])
		ans[q.second] = min(ans[q.second], ask(seg_rt, 0, 5e14, q.first - d));
	for (auto &e : G[u]) {
		int v = e.first;
		if (vis[v] || v == fa) continue;
		get_ans(v, u, d + e.second);
	}
}

void travel(int u, int fa, long long d) {
	ins(seg_rt, 0, 5e14, d, a[u]);
	for (auto &e : G[u]) {
		int v = e.first;
		if (v == fa || vis[v]) continue;
		travel(v, u, d + e.second);
	}
}

void dfs(int u) {
	vis[u] = 1;
	seg_rt = ncnt = 0;
	ins(seg_rt, 0, 5e14, 0, a[u]);
	for (auto e : G[u]) {
		int v = e.first;
		if (vis[v]) continue;		
		get_ans(v, u, e.second);		
		travel(v, u, e.second);
	}
	for (auto q : Q[u])
		ans[q.second] = min(ans[q.second], ask(seg_rt, 0, 5e14, q.first));
	

	seg_rt = ncnt = 0;
	ins(seg_rt, 0, 5e14, 0, a[u]);
	for (auto i = G[u].rbegin(); i != G[u].rend(); ++i) {
		auto e = *i;
		int v = e.first;
		if (vis[v]) continue;		
		get_ans(v, u, e.second);		
		travel(v, u, e.second);
	}

	for (auto e : G[u]) {
		int v = e.first;
		if (vis[v]) continue;
		rt = 0, tot = siz[v];
		get_rt(v, u);
		dfs(rt);
	}

}
int main() {
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	for (int i = 1; i < n; ++i) {
		int u, v, w;
		scanf("%d%d%d", &u, &v, &w);
		G[u].push_back(make_pair(v, w));
		G[v].push_back(make_pair(u, w));
	}
	for (int i = 1; i <= q; ++i) {
		int x; ll k;
		scanf("%d%lld", &x, &k);
		Q[x].push_back(make_pair(k, i));
		ans[i] = 1e9;
	}
	rt = 0, tot = n;
	get_rt(1, -1);
	dfs(rt);
	
	sort(a + 1, a + n + 1);
	int mex = 0;
	for (;; ++mex)
		if (a[mex + 1] != mex)
			break;
	
	for (int i = 1; i <= q; ++i)
		printf("%d\n", min(ans[i], mex));
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

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:


result: