QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#610942 | #5363. ZYB 的游览计划 | Hridaya | 35 | 6535ms | 27896kb | C++14 | 3.6kb | 2024-10-04 18:10:37 | 2024-10-04 18:10:38 |
Judging History
answer
// Writer: Hridaya Time:2024.10.4 By:NFLS
// #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define foru(i,x,y) for (register int i (x); i <= (y); ++ i)
#define ford(i,x,y) for (register int i (x); i >= (y); -- i)
#define Linf 9223372036854775807
#define Iinf 2147483647
#define Sinf 32767
namespace IO {
char ch ('~'); string name = "";
template<typename Type>
inline Type read() {
Type x(0);
bool m(0);
while (!isdigit(ch)) {
m |= (ch == '-');
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return m ? (~x + 1) : x;
}
template<typename Type>
inline void write(Type x) {
if (x < 0) putchar('-'), x = (~x + 1);
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
template<typename Type>
inline void writes(Type x, char c) {
write(x), putchar(c);
}
#define read() read<int>()
#define Fre_Input \
freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout);
} using namespace IO;
#define pb emplace_back
const int N (2e5 + 10), M(20);
int n, k, in[N], out[N], vis[N], a[N];
vector<int> g[N];
ll f[M][N], res;
int IN[N << 1], OUT[N << 1];
set<int> s;
namespace LCA {
int h[N], up[N][M];
#define SWAP(x, y) (x ^= y, y ^= x, x ^= y)
inline int lca(int u, int v) {
if (h[u] > h[v]) SWAP(u, v);
int o(h[v] - h[u]);
foru(i, 0, M - 1) if (o & (1 << i)) v = up[v][i];
if (u == v) return u;
ford(i, M - 1, 0) if (up[u][i] ^ up[v][i])
u = up[u][i], v = up[v][i];
return up[u][0];
}
} using namespace LCA;
namespace MoTeam {
int L, R;
#define lb lower_bound
#define DEFINE if (a[i] == 1) return ; s.insert(in[a[i]]); \
auto F(s.lb(in[a[i]])); --F; \
auto J(s.lb(in[a[i]])); ++J; \
int u(IN[*F]), v(IN[*J]);
inline ll W(int u, int v) {
if (h[u] > h[v]) SWAP(u, v); int x (lca(u, v));
return x == u ? h[v] - h[u] : (h[u] - h[x]) + (h[v] - h[x]);
}
inline void update(int l, int r) {
auto ins = [&] (int i) -> void { DEFINE res += (W(u, a[i]) + W(a[i], v) - W(u, v)); } ;
auto ers = [&] (int i) -> void { DEFINE s.erase(in[a[i]]); res -= (W(u, a[i]) + W(a[i], v) - W(u, v)); } ;
while (L > l) ins(--L); while (R < r) ins(++R);
while (L < l) ers(L++); while (R > r) ers(R--);
}
} using MoTeam::update;
#define val1 f[1][mid]
#define val2 f[0][k - 1] + res
inline void cal(int l, int r, int L, int R) {
if (l > r) return ;
int mid(l + r >> 1), MID;
foru(k, L, min(mid, R)) {
update(k, mid);
if (val1 <= val2) val1 = val2, MID = k;
}
cal(l, mid - 1, L, MID), cal(mid + 1, r, MID, R);
}
namespace Hridaya {
int idx(0);
inline void init() {
foru(i, 1, n) s.insert(in[i]), IN[in[i]] = OUT[out[i]] = i;
IN[out[1]] = 1, s.insert(out[1]), res = (n - 1) << 1;
}
inline void dfs(int u) {
in[u] = idx++;
for (auto v : g[u]) if (!vis[v]) {
vis[v] = 1, h[v] = h[u] + 1, up[v][0] = u;
foru(i, 1, 19) up[v][i] = up[up[v][i - 1]][i - 1];
dfs(v);
}
out[u] = idx++;
}
signed main() {
n = read(), k = read();
MoTeam::L = 1; MoTeam::R = n;
foru(i, 1, n) a[i] = read();
foru(i, 2, n) {
int u(read()), v(read());
g[u].pb(v), g[v].pb(u);
}
vis[1] = 1; h[1] = 1; dfs(1); init();
foru(i, 1, k) {
cal(1, n, 1, n);
foru(j, 0, n) f[0][j] = f[1][j], f[1][j] = 0;
}
write(f[0][n]); return 0;
}
}
#define ONLINE_JUDGE
signed main() {
#ifdef ONLINE_JUDGE
Hridaya::main();
#else
Fre_Input
Hridaya::main();
#endif
// cerr << "\nTime: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 5
Accepted
time: 0ms
memory: 17964kb
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
Time Limit Exceeded
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: 871ms
memory: 18140kb
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: 105ms
memory: 18096kb
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: 458ms
memory: 18084kb
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: 398ms
memory: 16100kb
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: 950ms
memory: 18128kb
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: 719ms
memory: 18064kb
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: 499ms
memory: 14008kb
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: 928ms
memory: 18180kb
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: 710ms
memory: 16052kb
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: 319ms
memory: 18940kb
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: 540ms
memory: 16868kb
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: 1291ms
memory: 16920kb
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: 1719ms
memory: 20964kb
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: 1955ms
memory: 20744kb
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: 1659ms
memory: 19552kb
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: 1728ms
memory: 19516kb
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: 2251ms
memory: 19636kb
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: 1723ms
memory: 20968kb
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: 1818ms
memory: 17520kb
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: 10
Accepted
Test #29:
score: 10
Accepted
time: 1292ms
memory: 21392kb
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: 3227ms
memory: 21736kb
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: 2924ms
memory: 23368kb
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: 10
Accepted
time: 5145ms
memory: 27148kb
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:
171986
result:
ok single line: '171986'
Test #33:
score: 10
Accepted
time: 5052ms
memory: 27100kb
input:
50849 3 39565 47876 84 11231 35992 14685 25222 48911 10169 43305 39934 46476 17222 17449 14869 48016 10568 33399 45577 2023 8134 32067 39984 44765 25555 36738 27234 35615 47330 50729 40495 39647 23364 21371 39116 31307 2126 8911 21905 29909 10832 50772 47388 39033 49574 25804 33111 30930 45516 13313...
output:
186762
result:
ok single line: '186762'
Test #34:
score: 10
Accepted
time: 6535ms
memory: 27192kb
input:
59553 3 11160 47277 7851 15595 56570 40146 58938 12198 57479 35980 18855 54715 29926 59357 28832 27143 28798 4408 50852 23333 54308 21117 10817 16337 46412 58906 5619 25987 3412 58950 20704 46605 28555 14807 17148 17185 28412 175 42110 33226 2458 33583 6886 40543 42672 26156 21014 32467 27091 11872 ...
output:
220062
result:
ok single line: '220062'
Test #35:
score: 10
Accepted
time: 6398ms
memory: 26300kb
input:
50000 4 45738 45181 32512 6389 15451 16925 31260 442 48643 43861 47173 37110 9763 28249 27947 9411 35857 38856 38454 13040 42902 49193 3095 24815 31282 5337 7746 35832 2770 36210 35432 19435 24512 32197 38769 49628 28763 21840 41823 45000 40728 38326 25058 47003 36008 6040 13825 40103 36794 28733 46...
output:
184530
result:
ok single line: '184530'
Test #36:
score: 10
Accepted
time: 3198ms
memory: 21340kb
input:
20000 10 14868 15331 9254 1880 19085 19308 6895 8083 15248 10502 16877 11536 16186 5244 2000 9564 7218 10907 18935 16890 15667 2657 7991 2017 6117 12725 6304 11785 7626 19147 3438 4241 17891 3934 12863 16907 8957 10903 5970 17954 11070 5894 1328 11794 18282 2304 15320 14053 9805 1069 15459 10790 101...
output:
102964
result:
ok single line: '102964'
Test #37:
score: 10
Accepted
time: 1451ms
memory: 18380kb
input:
2000 100 1773 1655 1366 357 1930 977 1690 1617 85 294 43 1327 1864 336 1802 1780 1665 681 686 231 94 64 1754 246 557 1976 1425 212 1472 1940 725 297 1112 1262 1560 1721 1556 1737 210 1243 615 1019 1550 767 1005 1404 1495 526 1483 1450 1761 1061 531 1951 1909 1169 835 518 748 1809 1320 595 1570 1593 ...
output:
33116
result:
ok single line: '33116'
Test #38:
score: 10
Accepted
time: 1853ms
memory: 18764kb
input:
5000 40 4418 4807 2144 4386 953 3374 2445 456 2855 3472 3906 12 3360 3854 2233 2109 4416 3843 409 2102 1818 909 2191 1242 3469 1612 4703 391 2951 4117 3381 2256 3334 3669 122 4231 893 3511 4415 1048 4256 4816 323 4811 3538 2480 2467 2726 4070 1841 1882 4970 4420 4393 4047 833 1109 667 2323 4369 4074...
output:
209522
result:
ok single line: '209522'
Test #39:
score: 10
Accepted
time: 2100ms
memory: 27896kb
input:
50000 4 50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 4995...
output:
399980
result:
ok single line: '399980'
Test #40:
score: 10
Accepted
time: 3550ms
memory: 23684kb
input:
32767 5 18474 28974 16167 19316 17139 8871 6525 13889 32540 21248 2355 10010 7072 28823 32234 8311 9993 23456 5481 30525 6320 21723 16139 31091 30178 26090 31360 1871 10189 9857 27187 10773 10620 24757 13466 29073 32092 19727 16964 2095 17391 32526 30369 31634 6164 21185 6829 6233 21408 24409 6107 1...
output:
145290
result:
ok single line: '145290'
Test #41:
score: 10
Accepted
time: 4160ms
memory: 23552kb
input:
32767 6 27120 27461 27104 30174 17357 17628 21948 25708 19784 10109 13732 29942 32746 20876 1033 2090 27457 7193 29496 13105 597 24910 605 13987 17697 27197 29951 27510 17470 20783 12200 28735 20806 26578 9783 32001 26472 9923 23739 16446 6215 138 31237 16693 21384 22993 4686 26770 7911 1592 14024 2...
output:
157096
result:
ok single line: '157096'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%