QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#616777#5363. ZYB 的游览计划SkyMaths25 1053ms8128kbC++143.5kb2024-10-06 11:28:522024-10-06 11:28:53

Judging History

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

  • [2024-10-06 11:28:53]
  • 评测
  • 测评结果:25
  • 用时:1053ms
  • 内存:8128kb
  • [2024-10-06 11:28:52]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,l,r) for (int i(l), i##end(r); i <= i##end; ++i)
#define per(i,r,l) for (int i(r), i##end(l); i >= i##end; --i)
#define LL long long
#define uLL unsigned long long
#define PII pair <int, int>
#define db double
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
using namespace std;

template <typename T>
inline void read(T &x) {
	x = 0; bool f = 0; char ch = getchar();
	while (ch < '0' || ch > '9') f ^= ch == '-', ch = getchar();
	while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
	if (f) x = -x;
}
template <typename Tx, typename ...Ty>
inline void read(Tx &x, Ty &...y) {
	read(x);
	read(y...);
}

template <typename T>
inline void Write(T x) {
	if (x > 9) Write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T>
inline void write(T x, char ch = '\n') {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	Write(x);
	putchar(ch);
}

template <typename T> inline void cmin(T &x, T y) { if (x > y) x = y;}
template <typename T> inline void cmax(T &x, T y) { if (x < y) x = y;}
bool Mbe;

/* - - - all above template - - - */

const int N = 3e4 + 9;
const int inf = 0x3f3f3f3f;

int n, m;
int a[N], f[N], g[N];
vector <int> G[N];
set <int> s;

int dfc;
int sz[N], son[N], dfn[N], id[N], top[N], fa[N], dep[N];
void dfs1(int x, int last) {
	sz[x] = 1; fa[x] = last; dep[x] = dep[last] + 1;
	for (int v : G[x]) if (v != last) {
		dfs1(v, x); sz[x] += sz[v];
		if (sz[son[x]] < sz[v]) son[x] = v;
	}
}
void dfs2(int x, int last, int tp) {
	dfn[x] = ++dfc; id[dfc] = x; top[x] = tp;
	if (son[x]) dfs2(son[x], x, tp);
	for (int v : G[x]) if (v != last && v != son[x]) {
		dfs2(v, x, v);
	}
}
int LCA(int x, int y) {
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	return dep[x] < dep[y] ? x : y;
}
int DIS(int x, int y) {
	return dep[x] + dep[y] - 2 * dep[LCA(x, y)];
}

int iL(1), iR(0), sum;
void add(int x) {
	auto it = s.insert(dfn[x]).first;
	assert(it != s.begin() && prev(it) != s.end());
	sum += DIS(id[*prev(it)], x) + DIS(x, id[*next(it)]) - DIS(id[*prev(it)], id[*next(it)]);
}
void del(int x) {
	auto it = s.erase(s.find(dfn[x]));
	assert(it != s.end() && it != s.begin());
	sum -= (DIS(id[*prev(it)], x) + DIS(id[*it], x) - DIS(id[*prev(it)], id[*it]));
}
int calc(int l, int r) {
	while (iL > l) add(a[--iL]);
	while (iR < r) add(a[++iR]);

	while (iL < l) del(a[iL++]);
	while (iR > r) del(a[iR--]);

	return sum;
}

void solve(int l, int r, int pl, int pr) {
	if (l > r || pl > pr) return ;
	int o = (l + r) >> 1, op = pl;
	rep (i, pl, min(pr, o)) {
		int t = g[i - 1] + calc(i, o);
		if (t > f[o]) f[o] = t, op = i;
	}
	solve(l, o - 1, pl, min(o - 1, op));
	solve(o + 1, r, op, pr);
}

void skymaths() {
	read(n, m);
	rep (i, 1, n) read(a[i]);
	rep (i, 1, n - 1) {
		int u, v; read(u, v);
		G[u].eb(v);
		G[v].eb(u);
	}

	dfs1(1, 0);
	dfs2(1, 0, 1);

	id[0] = id[n + 1] = 1;
	s.insert(0); s.insert(n + 1);

	rep (i, 1, n) f[i] = -inf;

	rep (i, 1, m) {
		memcpy(g, f, sizeof(f[0]) * (n + 1));
		rep (j, 0, n) f[j] = -inf;
		solve(1, n, 1, n);
	}

	printf("%d\n", f[n]);
}

bool Med; double start_time;
signed main() {
	start_time = clock();
	cerr << (&Mbe - &Med) / 1048576.0 << "MB\n";
    // freopen("a.in", "r", stdin);

	int T = 1;
 	// read(T);
    while (T--) {
        skymaths();
    }

	cerr << (clock() - start_time) / CLOCKS_PER_SEC << "s\n";
    return 0;
}

詳細信息

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 5
Accepted
time: 0ms
memory: 4744kb

input:

5 2
2 4 3 5 1
1 2
2 3
3 4
4 5

output:

14

result:

ok single line: '14'

Test #2:

score: 0
Runtime Error

input:

90752 2
88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...

output:


result:


Subtask #2:

score: 10
Accepted

Test #10:

score: 10
Accepted
time: 320ms
memory: 4624kb

input:

760 217
632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...

output:

35938

result:

ok single line: '35938'

Test #11:

score: 10
Accepted
time: 42ms
memory: 4780kb

input:

919 16
836 94 652 285 192 830 643 430 855 825 844 852 750 773 835 743 310 59 115 589 187 831 277 35 577 683 348 611 459 590 196 531 609 575 754 729 542 502 19 890 695 433 445 678 221 901 140 525 441 689 510 423 709 568 894 802 552 474 878 653 51 866 916 123 2 167 190 302 226 769 658 818 351 757 400 ...

output:

5682

result:

ok single line: '5682'

Test #12:

score: 10
Accepted
time: 154ms
memory: 4872kb

input:

727 116
382 218 586 528 66 508 58 216 234 251 629 222 403 356 596 577 92 221 167 84 417 226 595 388 127 341 656 715 180 71 67 429 25 308 194 494 144 196 675 556 201 289 711 727 594 304 188 330 581 517 471 441 462 254 654 203 159 472 273 280 442 603 677 650 317 389 545 265 475 499 362 15 686 239 205 ...

output:

40024

result:

ok single line: '40024'

Test #13:

score: 10
Accepted
time: 166ms
memory: 4848kb

input:

961 67
180 513 138 39 787 472 499 847 561 275 602 804 331 191 492 75 718 287 647 724 323 224 436 442 389 605 250 679 59 19 746 177 692 461 115 786 289 267 424 58 952 541 422 397 401 484 273 445 660 925 806 789 744 553 314 435 253 209 530 462 594 795 745 644 281 427 637 141 808 76 378 351 348 464 35 ...

output:

13302

result:

ok single line: '13302'

Test #14:

score: 10
Accepted
time: 444ms
memory: 4864kb

input:

979 185
384 598 917 477 487 141 460 529 299 53 462 614 238 772 535 933 915 588 789 816 710 660 866 158 83 139 69 473 965 908 434 733 582 132 318 722 880 18 478 426 154 262 809 216 576 781 375 170 736 130 765 753 122 456 526 373 734 222 425 693 595 877 818 482 114 68 263 308 891 390 845 320 447 922 3...

output:

11084

result:

ok single line: '11084'

Test #15:

score: 10
Accepted
time: 246ms
memory: 4736kb

input:

515 312
449 338 205 182 444 131 496 370 300 32 343 143 475 133 43 172 385 266 283 227 228 458 470 469 196 399 136 329 146 218 367 83 30 304 99 158 26 268 435 1 206 494 324 7 140 506 155 113 112 162 219 109 393 70 490 166 359 231 251 233 277 373 222 246 501 229 429 169 12 473 316 313 264 122 168 398 ...

output:

47230

result:

ok single line: '47230'

Test #16:

score: 10
Accepted
time: 207ms
memory: 4708kb

input:

771 119
83 275 752 576 319 581 324 287 496 137 114 73 541 100 418 488 120 701 234 539 554 677 194 91 561 369 171 230 301 632 389 211 655 157 208 586 606 377 169 724 364 714 538 743 749 407 599 193 311 233 65 550 640 513 135 615 1 421 354 108 726 633 290 682 41 270 22 302 115 670 614 678 246 605 509 ...

output:

12334

result:

ok single line: '12334'

Test #17:

score: 10
Accepted
time: 322ms
memory: 4876kb

input:

971 164
241 934 565 457 914 803 892 300 927 270 769 177 930 250 701 287 623 510 915 164 483 385 29 438 98 431 145 715 855 337 408 303 506 758 795 700 396 495 290 531 235 719 570 174 346 339 898 607 295 808 10 496 561 875 931 155 479 94 275 317 368 361 669 788 366 175 801 709 908 364 616 804 965 412 ...

output:

58248

result:

ok single line: '58248'

Test #18:

score: 10
Accepted
time: 255ms
memory: 4816kb

input:

727 189
725 237 275 99 597 563 257 436 396 342 397 299 611 74 395 701 594 252 357 619 428 115 439 229 339 223 91 344 555 484 97 8 722 133 677 338 2 704 254 265 271 302 231 557 467 391 715 244 498 421 235 112 367 487 29 368 365 469 547 595 60 635 141 171 380 156 170 377 613 316 453 708 44 258 372 452...

output:

33166

result:

ok single line: '33166'

Subtask #3:

score: 15
Accepted

Dependency #2:

100%
Accepted

Test #19:

score: 15
Accepted
time: 129ms
memory: 5320kb

input:

5888 5
1718 907 2359 17 692 2242 39 1614 444 5711 2566 894 4118 3472 3556 2148 616 2871 1299 2933 1836 3376 2157 4664 4793 3123 1129 1074 521 2266 2864 5201 936 5803 4916 5196 1904 1427 4921 2065 2833 3131 3107 1555 2512 5250 4887 4839 3986 3143 4554 4304 4556 3166 1705 3365 3929 2879 4693 3086 4382...

output:

23844

result:

ok single line: '23844'

Test #20:

score: 15
Accepted
time: 206ms
memory: 5384kb

input:

5581 9
1454 2110 642 4210 4160 910 4349 5071 2724 989 5207 4612 2954 514 3283 1111 4301 389 4428 130 3064 3189 2505 3844 4866 3434 1858 2169 2386 4970 2726 3012 621 385 3410 5403 4050 4238 139 5431 3160 853 1133 4403 2176 1264 343 3760 570 4331 3214 2872 1550 5007 2772 2478 1636 4453 5172 1547 3202 ...

output:

35122

result:

ok single line: '35122'

Test #21:

score: 15
Accepted
time: 430ms
memory: 5344kb

input:

5757 21
5119 4440 256 3691 2048 4043 2239 5289 3247 3824 4734 219 3258 5392 2558 5011 5295 4672 4017 2728 5657 1606 3755 5241 5649 2970 1131 3922 2840 409 2435 327 5513 5074 2164 5291 5017 2974 512 2776 2972 4553 111 2687 3382 5008 2660 4464 1444 3873 3701 2548 2812 369 3823 2405 1481 2098 2912 5585...

output:

76420

result:

ok single line: '76420'

Test #22:

score: 15
Accepted
time: 731ms
memory: 5804kb

input:

9482 16
6630 3953 4314 2316 7432 8505 9370 3821 3527 1433 3063 5514 2601 4671 1032 5520 9211 2374 6594 7797 2261 6233 4325 2432 6214 1230 2628 6109 2581 6494 6064 2014 8654 566 8885 7677 1243 4025 1009 5396 1679 986 9476 9255 2431 6191 5922 6690 7665 1165 4559 4978 8575 173 4431 1783 5106 2053 6046 ...

output:

56384

result:

ok single line: '56384'

Test #23:

score: 15
Accepted
time: 652ms
memory: 5456kb

input:

5610 34
4266 998 2201 4812 3413 2509 1223 1116 4063 3671 1759 3160 222 2046 4875 3184 2653 3609 3117 4597 1108 5308 1061 3699 4277 3426 3546 1444 5539 5305 4586 2976 3568 2916 1090 2950 5029 2772 441 3078 2235 476 4699 1944 5240 2207 372 1358 2516 197 1109 4166 4512 2324 402 506 2757 3553 4934 4441 ...

output:

106410

result:

ok single line: '106410'

Test #24:

score: 15
Accepted
time: 648ms
memory: 5880kb

input:

9796 14
1330 3337 3269 3377 7636 1461 3560 4842 7829 980 2712 5041 3026 3171 8253 6779 6239 6098 430 7242 7983 7816 9174 6769 9414 1349 9276 7448 8086 5963 7310 9674 9435 421 8406 4821 5589 7640 4444 1720 7486 8343 895 13 3527 2576 8042 7738 7185 8066 6257 5046 2 4416 5539 9166 8442 5880 3978 6450 6...

output:

74648

result:

ok single line: '74648'

Test #25:

score: 15
Accepted
time: 730ms
memory: 5796kb

input:

9447 16
2362 8779 3329 982 4374 959 2428 880 3577 6494 5461 1677 5995 4832 3760 1770 1085 7667 8084 2342 7000 9117 7296 4447 3370 8757 1069 4492 7719 7433 2610 6570 3155 9157 785 5207 5367 3953 3788 569 521 4927 6102 314 6018 2216 2325 4665 5273 2474 8697 2456 2810 4183 3528 4491 7831 1402 4526 7089...

output:

56042

result:

ok single line: '56042'

Test #26:

score: 15
Accepted
time: 741ms
memory: 5992kb

input:

9709 19
2020 1513 8246 8366 775 3581 1546 8291 7922 4734 2941 2555 389 1723 4575 2928 7676 4249 16 9490 2133 4623 7311 3059 7686 6578 4847 1388 1017 1514 3900 7736 3566 709 6111 3032 3111 8087 6665 9462 9447 5042 573 8721 518 4466 9379 8163 8528 7963 3742 1899 3528 4696 8805 4380 8642 3029 6826 8872...

output:

123260

result:

ok single line: '123260'

Test #27:

score: 15
Accepted
time: 698ms
memory: 5828kb

input:

9151 16
3040 8481 1253 9082 6361 4127 8823 1041 2486 6558 9025 7962 9128 8996 14 8206 2511 4855 6154 2973 5494 6712 7699 5173 4548 2445 8320 1131 1840 8965 8922 3089 8200 7673 2659 5412 5577 7812 900 6984 6394 5158 9023 1627 6555 1006 1849 4605 384 143 3012 1085 7200 5050 8497 5601 2379 2713 1659 53...

output:

72992

result:

ok single line: '72992'

Test #28:

score: 15
Accepted
time: 761ms
memory: 5884kb

input:

9780 16
5936 8229 6637 3017 8301 7926 683 4261 6024 622 458 142 5084 4317 6367 2096 6368 1457 4768 2467 7292 3571 3039 5186 1001 7039 1034 3334 6807 2454 4219 5990 9682 2659 7821 9057 6324 3269 8363 4995 8014 161 3245 3554 1346 3506 5637 5683 6342 3879 2497 4285 1621 4163 5710 7705 2701 9455 1838 61...

output:

58460

result:

ok single line: '58460'

Subtask #4:

score: 0
Runtime Error

Test #29:

score: 10
Accepted
time: 480ms
memory: 6468kb

input:

14878 6
1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...

output:

78002

result:

ok single line: '78002'

Test #30:

score: 10
Accepted
time: 1053ms
memory: 7580kb

input:

24636 7
12167 7049 12913 9008 23642 14034 22429 15360 16128 13951 22411 23901 14309 23575 3041 22948 18403 6420 23362 8205 22117 8264 4985 10566 23946 6690 23211 3785 12602 8004 14405 12431 16247 11244 2609 20547 15874 22972 15889 23107 5516 15507 7804 9729 23913 3705 21184 9988 15513 15567 5773 245...

output:

139942

result:

ok single line: '139942'

Test #31:

score: 10
Accepted
time: 962ms
memory: 8128kb

input:

28777 5
3114 25599 5461 12359 9126 3562 15686 26516 7699 13051 21415 23352 17530 15810 5010 26644 5003 10378 28447 643 4248 5626 27227 28668 10795 7453 8223 23524 13021 4276 19362 3886 14958 19905 658 23584 20028 28592 5590 10278 11631 14519 8792 18493 6786 25778 21658 6891 26711 14785 25071 24747 2...

output:

115786

result:

ok single line: '115786'

Test #32:

score: 0
Runtime Error

input:

52146 3
33314 45821 7078 37782 29370 29486 46473 44353 24139 1100 34550 38329 47860 26414 24351 33775 23138 36289 49679 23188 47590 34612 50336 44211 30105 43691 39467 32464 24104 44400 43750 28231 39219 31463 750 45411 45673 28738 46536 52132 32660 39989 37018 25355 43732 26363 42964 9967 40492 272...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #1:

0%