QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344467#8235. Top Clustermendicillin2#TL 3888ms146320kbC++172.9kb2024-03-04 16:55:122024-03-04 16:55:13

Judging History

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

  • [2024-03-04 16:55:13]
  • 评测
  • 测评结果:TL
  • 用时:3888ms
  • 内存:146320kb
  • [2024-03-04 16:55:12]
  • 提交

answer

#include<bits/stdc++.h>

#define int long long
#define F(i, j, k) for (int i = j; i <= k; i ++)
#define DF(i, j, k) for (int i = j; i >= k; i --)

using namespace std;

inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}

inline long long readlong() {
	long long x=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}

const int N = 5e5 + 100;

struct wzy {
	int val, num;
} a[N];

inline bool cmp(wzy x, wzy y) {
	return x.val < y.val;
}

int cnt, head[N], nxt[N << 1], to[N << 1], val[N << 1];

inline void addedge(int x, int y, int w) {
	nxt[++cnt] = head[x];
	to[cnt] = y;
	val[cnt] = w;
	head[x] = cnt;
}

int n, fa[N][21], depth[N], lg[N];

long long di[N];

inline void dfs(int x) {
	for (int i = head[x]; i; i = nxt[i]) {
		if (to[i] == fa[x][0]) continue;
		fa[to[i]][0] = x;
		depth[to[i]] = depth[x] + 1;
		di[to[i]] = di[x] + val[i];
		dfs(to[i]);
	}
}

inline void prepare() {
	depth[1] = 1;
	dfs(1);
	F(i, 1, 20)
		F(j, 1, n)
			fa[j][i] = fa[fa[j][i - 1]][i - 1];
	lg[1] = 0;
	F(i, 2, n) lg[i] = lg[i >> 1] + 1;
}

inline int lca(int x, int y) {
	if (depth[y] > depth[x]) swap(x, y);
	DF(i, lg[depth[x] - depth[y]], 0)
		if (depth[fa[x][i]] >= depth[y]) x = fa[x][i];
	if (x == y) return x;
	DF(i, lg[depth[x]], 0)
		if (fa[x][i] != fa[y][i]) { x = fa[x][i]; y = fa[y][i]; }
	return fa[x][0];
}

inline long long dis(int x, int y) {
	return di[x] + di[y] - 2LL * di[lca(x, y)];
}

int X[N], Y[N];

signed main()
{
	n = read();
	int Q = read();

	F(i, 1, n) {
		a[i].val = read();
		a[i].num = i;
	}
	sort(a + 1, a + n + 1, cmp);
	int ans = n, mx = n;
	F(i, 1, n)
		if (a[i].val != i - 1) {
			ans = i - 1;
			mx = i - 1;
			break;
		}
	F(i, 1, n - 1) {
		int x = read(), y = read(), w = read();
		addedge(x, y, w);
		addedge(y, x, w);
	}
	prepare();
	long long cur = 0;
	F(i, 1, mx) {
		if (i == 1) { X[i] = Y[i] = a[i].num; continue; }
		X[i] = X[i - 1];
		Y[i] = Y[i - 1];
		long long dis1 = dis(a[i].num, X[i - 1]), dis2 = dis(a[i].num, Y[i - 1]);
		if (dis1 > cur) {
			cur = dis1;
			X[i] = X[i - 1];
			Y[i] = a[i].num;
		}
		if (dis2 > cur) {
			cur = dis2;
			X[i] = a[i].num;
			Y[i] = Y[i - 1];
		}
	}
	while (Q --) {
		int x = read();
		long long k = readlong();
		int l = 1, r = mx, an = ans;
		while (l <= r) {
			int mid = (l + r) >> 1;
			//cout << Q << ": " << mid << ' ' << x << ' ' << X[mid] << ' ' << Y[mid] << ' ' << lca(x, X[mid]) << endl;
			if (dis(x, X[mid]) <= k && dis(x, Y[mid]) <= k) l = mid + 1;
			else { an = mid - 1; r = mid - 1; }
		}
		cout << an << '\n';
	}
 	return 0;
}
/*
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

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1148ms
memory: 136396kb

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: 1199ms
memory: 136464kb

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: 3661ms
memory: 143376kb

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: 3810ms
memory: 143416kb

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: 0
Accepted
time: 3888ms
memory: 146320kb

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:

ok 500000 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

500000 500000
273731 92547 391851 223257 245734 999907678 125748 999999710 260816 269057 173753 337309 208492 162862 160710 371998 296145 999901286 269727 36806 193730 999966618 219876 999919231 999950308 999989538 403483 27 199626 83765 58199 38349 999985326 362514 103279 96338 292486 353093 235704...

output:

0
250007
0
250007
250007
250007
250007
250007
250007
0
250007
0
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
2
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
0
250007
250007
250007
250007
250007
250007
250007
250007
250007
250007
2...

result: