QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#665307#8235. Top ClusterOIer_kzc#TL 3240ms415148kbC++173.6kb2024-10-22 11:12:522024-10-22 11:12:52

Judging History

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

  • [2024-10-22 11:12:52]
  • 评测
  • 测评结果:TL
  • 用时:3240ms
  • 内存:415148kb
  • [2024-10-22 11:12:52]
  • 提交

answer

#include <stdio.h>
#include <string.h>

#include <set>
#include <vector>
#include <algorithm>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back
#define em emplace

using namespace std;

typedef long long LL;
constexpr int N = 500005, M = 1000005;

void cmin(int &x, int y) {
	if (x > y) {
		x = y;
	}
}

void cmax(int &x, int y) {
	if (x < y) {
		x = y;
	}
}

int n, m, res1, a[N];
int h[N], e[M], ne[M], idx;
LL w[M];

void add(int x, int y, LL c) {
	ne[++idx] = h[x], h[x] = idx, e[idx] = y, w[idx] = c;
}

int Rt, rt, maxv;
int fa[N], de[N];
LL dis[20][N];
int col[20][N], idc;
struct Dat {
	LL d; int c, v, w;
	Dat(LL _d, int _c, int _v) : d(_d), c(_c), v(_v), w(0x7f7f7f7f) {}
	void upd(const Dat &t) {
		if (t.c == c) {
			cmin(v, t.v);
		} else if (t.v < v) {
			w = v, v = t.v, c = t.c;
		} else {
			cmin(w, t.v);
		}
	}
	bool operator < (const Dat &t) const {
		return d < t.d;
	}
};
vector<Dat> seq[N];
bool was[N];

int getsz(int x, int fat) {
	int ret = 1;
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (y == fat || was[y]) {
			continue;
		}
		ret += getsz(y, x);
	}
	return ret;
}

int center(int x, int fat, int sz) {
	int v = 0, s = 1;
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (y == fat || was[y]) {
			continue;
		}
		int t = center(y, x, sz);
		s += t, cmax(v, t);
	}
	cmax(v, sz - s);
	if (v < maxv) {
		maxv = v, rt = x;
	}
	return s;
}

int dep;

void dfs1(int x, int fat, LL d, vector<Dat> &v) {
	col[dep][x] = idc;
	dis[dep][x] = d;
	v.eb(d, idc, a[x]);
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (y == fat || was[y]) {
			continue;
		}
		dfs1(y, x, d + w[i], v);
	}
}

int build(int x) {
	maxv = N;
	center(x, 0, getsz(x, 0));
	x = rt;
	was[x] = true;
	de[x] = dep;
	vector<Dat> &vs = seq[x];
	idc = 0;
	vs.eb(0ll, 0, a[x]);
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (was[y]) {
			continue;
		}
		++idc;
		dfs1(y, x, w[i], vs);
	}
	sort(vs.begin(), vs.end());
	int c = -1, v = 0x7f7f7f7f, w = 0x7f7f7f7f;
	for (int i = (int)vs.size() - 1; ~i; --i) {
		auto &[ignore, tc, tv, tw] = vs[i];
		if (tc == c) {
			cmin(v, tv);
		} else if (tv < v) {
			w = v, v = tv, c = tc;
		} else {
			cmin(w, tv);
		}
		vs[i].c = c, vs[i].v = v, vs[i].w = w;
	}
	
	/* LOG("rt: %d\n", x);
	for (auto [d, c, v, w] : vs) {
		LOG("%lld %d %d %d\n", d, c, v, w);
	}
	LOG("\n"); */
	dep += 1;
	for (int i = h[x]; i; i = ne[i]) {
		int y = e[i];
		if (was[y]) {
			continue;
		}
		fa[build(y)] = x;
	}
	dep -= 1;
	return x;
}

int query() {
	int ret = res1;
	int x; LL d;
	scanf("%d%lld", &x, &d);
	for (int y = x; y; y = fa[y]) {
		LL *ds = dis[de[y]];
		int *co = col[de[y]];
		LL rs = d - ds[x];
		vector<Dat> &vs = seq[y];
		int k = upper_bound(vs.begin(), vs.end(), Dat(rs, 0, 0)) - vs.begin();
		/* for (auto [d, c, v, w] : vs) {
			LOG("%lld %d %d %d\n", d, c, v, w);
		}
		LOG("rs: %lld\n", rs);
		LOG("k: %d\n", k);
		exit(0); */
		if (k >= vs.size()) {
			continue;
		}
		const auto &[ignore, c, v, w] = vs[k];
		cmin(ret, c == co[x] ? w : v);
	}
	return ret;
}

int main() {
	scanf("%d%d", &n, &m);
	set<int> st;
	for (int x = 1; x <= n; ++x) {
		scanf("%d", a + x);
		st.em(a[x]);
	}
	for (; st.count(res1); ++res1);
	for (int i = 1, x, y; i < n; ++i) {
		LL c;
		scanf("%d%d%lld", &x, &y, &c);
		add(x, y, c), add(y, x, c);
	}
	Rt = build(1);
	/* for (int x = 1; x <= n; ++x) {
		LOG("%d ", fa[x]);
	}
	LOG("\n"); */
	while (m--) {
		printf("%d\n", query());
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 31912kb

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: 2581ms
memory: 333268kb

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: 0
Accepted
time: 2685ms
memory: 346048kb

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:

0
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
2500...

result:

ok 500000 numbers

Test #4:

score: 0
Accepted
time: 3139ms
memory: 415148kb

input:

500000 500000
161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...

output:

0
250000
250000
69
10
250000
250000
0
5
250000
250000
250000
1
33
69
10
250000
250000
250000
10
0
250000
250000
250000
1
250000
250000
0
250000
10
5
0
33
10
250000
1
66
250000
1
250000
1
250000
250000
250000
16
1
1
0
5
10
250000
250000
0
250000
250000
250000
250000
250000
250000
250000
5
1
250000
1
...

result:

ok 500000 numbers

Test #5:

score: 0
Accepted
time: 3240ms
memory: 413908kb

input:

500000 500000
205699 141313 291987 999929528 111195 248994 999960789 391023 323134 92686 432395 316257 188889 219187 315056 39556 999959131 279778 30709 419259 242325 169515 137848 999905250 130641 245510 26 52987 999900052 144308 180276 162429 405263 130502 101937 108999 259339 137566 414011 304915...

output:

0
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
3
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
250000
250000
250000
250000
2
250...

result:

ok 500000 numbers

Test #6:

score: -100
Time Limit Exceeded

input:

500000 500000
142817 198136 999910358 236637 238445 325614 22961 999909937 354352 999984540 189454 221148 208300 169118 999945163 256203 999945857 276295 175580 481676 114118 119423 999962365 999904418 250723 333971 311979 384475 386372 11544 999956892 999960490 999936512 292089 42395 5450 326202 26...

output:

0
250004
0
4
6
5
250004
6
250004
250004
0
0
250004
250004
250004
250004
0
0
250004
250004
250004
0
0
0
0
0
6
0
0
0
0
6
0
250004
4
250004
250004
250004
250004
250004
0
0
6
250004
6
250004
0
0
6
250004
4
250004
0
6
250004
250004
4
250004
0
6
0
250004
0
0
0
250004
4
0
0
6
0
0
0
6
0
250004
4
250004
5
25...

result: