QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#746500 | #5659. Watching Cowflix | _8_8_# | 29.166667 | 2391ms | 100136kb | C++23 | 5.4kb | 2024-11-14 14:50:24 | 2024-11-14 14:50:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int)2e5 + 12, MOD = (int)1e9 + 7, b = 19;
#define int ll
vector<int> g[N], f[N];
bool ok[N], blocked[N];
int n, r[N], s[N], up[N][20], dep[N], tin[N], tout[N], timer;
void build(int v, int pr) {
up[v][0] = pr;
for(int i = 1; i < b; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
tin[v] = ++timer;
for(int to:g[v]) {
if(to != pr) {
r[to] = v;
dep[to] = dep[v] + 1;
build(to, v);
}
}
tout[v] = ++timer;
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
if(is(v, u)) return v;
if(is(u, v)) return u;
for(int i = b - 1; i >= 0; i--) {
if(!is(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
int get(int v, int u) {
return dep[v] + dep[u] - 2 * dep[lca(u, v)] - 1;
}
void dfs(int v, int pr = -1) {
s[v] = 1;
for(int to:g[v]) {
if(to == pr || blocked[to]) continue;
dfs(to, v);
s[v] += s[to];
}
}
int find(int v, int pr, int total) {
for(int to:g[v]) {
if(to != pr && !blocked[to] && s[to] > total / 2) {
return find(to, v, total);
}
}
return v;
}
void decompose(int v, int pr = 0) {
dfs(v);
v = find(v, -1, s[v]);
blocked[v] = 1;
r[v] = pr;
for(int to:g[v]) {
if(!blocked[to]) {
decompose(to, v);
}
}
}
set<pair<int, int>> st[N];
set<array<int, 3>> all;
int dp[N], c, L[N];
array<int, 3> get(int v) {
auto it = (st[v].begin());
int x = (*it).second, u = 1 + (*it).first;
it++;
int y = (*it).second;
u += (*it).first;
return {u, x, y};
}
void make(int v) {
auto [val, i] = (*st[v].begin());
st[v].erase(st[v].begin());
while(!st[v].empty()) {
auto [x, j] = (*st[v].begin());
if(i == j) {
st[v].erase(st[v].begin());
continue;
} else {
break;
}
}
st[v].insert({val, i});
}
void add(int v, int val) {
int x = v;
while(v) {
if((int)st[v].size() >= 2) {
all.erase(get(v));
}
st[v].insert({get(x, v), val});
make(v);
if((int)st[v].size() >= 2) {
all.insert(get(v));
}
v = r[v];
}
}
void er(int v, int val) {
int x = v;
while(v) {
if((int)st[v].size() >= 2) {
all.erase(get(v));
}
st[v].erase({get(v, x), val});
if((int)st[v].size() >= 2) {
all.insert(get(v));
}
v = r[v];
}
}
vector<int> v;
void slow() {
int lst = -1;
c =0 ;
for(int i = 1; i <= n; ++i) {
if(ok[i]) {
ok[i] = 1;
c++;
if(lst != -1) {
v.push_back(i - lst - 1);
}
lst = i;
}
}
dp[c] = c;
sort(v.rbegin(), v.rend());
for(int i = c - 1; i >= 1; i--) {
dp[i] = dp[i + 1];
dp[i] += v.back();
v.pop_back();
}
for(int k = 1; k <= n; k++) {
ll res = (ll)1e17;
for(int j = 1; j <= c; j++) {
res = min(res, j * 1ll * k + dp[j]);
}
cout << res << '\n';
}
}
void test() {
cin >> n;
for(int i = 1; i <= n; ++i) {
char x;cin >> x;
L[i] = i;
if(x == '1') {
ok[i] = 1;
c++;
}
f[i].push_back(i);
}
if(c == 1) {
for(int i = 1; i <= n; i++) {
cout << 1 + i << '\n';
}
return;
}
bool sti = 1;
for(int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
if(v != u + 1) {
sti = 0;
}
g[u].push_back(v);
g[v].push_back(u);
}
if(sti) {
slow();
return;
}
dep[1] = 1;
build(1, 1);
decompose(1);
dp[c] = c;
for(int i = 1; i <= n; i++) {
if(ok[i]) {
add(i, i);
}
}
auto merge = [&](int x, int y) {
if(x == y) {
exit(-1);
}
if((int)f[x].size() > (int)f[y].size()) {
swap(x, y);
}
int o = lca(L[x], L[y]);
auto dd = [&](int l, int r) {
while(l != r) {
if(!ok[l]) {
ok[l] = 1;
add(l, y);
}
l = up[l][0];
}
};
dd(L[x], o);
dd(L[y], o);
L[y] = o;
for(int i:f[x]) {
er(i, x);
add(i, y);
f[y].push_back(i);
}
f[x].clear();
};
for(int i = c - 1; i >= 1; i--) {
dp[i] = dp[i + 1];
array<int, 3> val = (*all.begin());
merge(val[1], val[2]);
dp[i] += val[0];
}
for(int i = 1; i <= c; i++) {
if(dp[i] > n) {
exit(-1);
}
}
for(int k = 1; k <= n; k++) {
ll res = (ll)1e18;
for(int j = 1; j <= c; j++) {
res = min(res, j * 1ll * k + dp[j]);
}
cout << res << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
详细
Pretests
Final Tests
Test #1:
score: 4.16667
Accepted
time: 2ms
memory: 19984kb
input:
5 10001 1 2 2 3 3 4 4 5
output:
4 6 8 9 10
result:
ok 5 number(s): "4 6 8 9 10"
Test #2:
score: 4.16667
Accepted
time: 3ms
memory: 26120kb
input:
7 0001010 7 4 5 6 7 2 5 1 6 3 2 5
output:
4 6 8 9 10 11 12
result:
ok 7 numbers
Test #3:
score: 0
Wrong Answer
time: 9ms
memory: 26932kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
921 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 103...
result:
wrong answer 1st numbers differ - expected: '711', found: '921'
Test #4:
score: 0
Wrong Answer
time: 7ms
memory: 29108kb
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 ...
result:
wrong answer 1st numbers differ - expected: '890', found: '586'
Test #5:
score: 0
Wrong Answer
time: 5ms
memory: 26980kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 ...
result:
wrong answer 1st numbers differ - expected: '794', found: '468'
Test #6:
score: 4.16667
Accepted
time: 2054ms
memory: 40444kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30804 41189 48182 51855 52223 52591 52958 53324 53690 54055 54419 54782 55144 55505 55865 56224 56582 56939 57295 57650 58004 58357 58709 59061 59412 59762 60111 60459 60806 61152 61497 61841 62184 62526 62867 63207 63546 63884 64222 64559 64895 65230 65564 65898 66231 66563 66894 67224 67554 67883 ...
result:
ok 200000 numbers
Test #7:
score: 4.16667
Accepted
time: 2035ms
memory: 40692kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30644 41023 48123 51892 52268 52643 53017 53390 53763 54135 54506 54876 55245 55613 55980 56346 56712 57077 57441 57804 58166 58528 58889 59249 59608 59966 60323 60679 61034 61388 61741 62093 62445 62796 63146 63495 63843 64190 64536 64881 65225 65568 65910 66251 66591 66931 67270 67608 67945 68281 ...
result:
ok 200000 numbers
Test #8:
score: 4.16667
Accepted
time: 2071ms
memory: 38708kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30736 41111 48167 51868 52239 52609 52978 53346 53714 54081 54447 54812 55176 55539 55902 56264 56625 56985 57344 57702 58060 58417 58774 59130 59485 59839 60192 60544 60895 61245 61594 61942 62289 62635 62980 63324 63668 64011 64353 64695 65036 65377 65717 66057 66396 66734 67071 67408 67744 68079 ...
result:
ok 200000 numbers
Test #9:
score: 0
Wrong Answer
time: 563ms
memory: 59552kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 ...
result:
wrong answer 1st numbers differ - expected: '12231', found: '14270'
Test #10:
score: 0
Wrong Answer
time: 634ms
memory: 62852kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 ...
result:
wrong answer 1st numbers differ - expected: '15993', found: '9388'
Test #11:
score: 0
Wrong Answer
time: 596ms
memory: 61764kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 ...
result:
wrong answer 1st numbers differ - expected: '14608', found: '8673'
Test #12:
score: 0
Wrong Answer
time: 578ms
memory: 57824kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16477 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 ...
result:
wrong answer 1st numbers differ - expected: '12198', found: '16477'
Test #13:
score: 0
Wrong Answer
time: 627ms
memory: 64744kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 ...
result:
wrong answer 1st numbers differ - expected: '16072', found: '8687'
Test #14:
score: 0
Wrong Answer
time: 602ms
memory: 60604kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 ...
result:
wrong answer 1st numbers differ - expected: '14721', found: '8647'
Test #15:
score: 0
Wrong Answer
time: 562ms
memory: 58868kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 ...
result:
wrong answer 1st numbers differ - expected: '12121', found: '14474'
Test #16:
score: 0
Wrong Answer
time: 624ms
memory: 64256kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 ...
result:
wrong answer 1st numbers differ - expected: '16192', found: '9482'
Test #17:
score: 0
Wrong Answer
time: 628ms
memory: 60644kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 ...
result:
wrong answer 1st numbers differ - expected: '14705', found: '8646'
Test #18:
score: 0
Wrong Answer
time: 588ms
memory: 57944kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 ...
result:
wrong answer 1st numbers differ - expected: '12282', found: '16576'
Test #19:
score: 4.16667
Accepted
time: 146ms
memory: 57756kb
input:
100000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 25...
result:
ok 100000 numbers
Test #20:
score: 0
Wrong Answer
time: 2370ms
memory: 100072kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 ...
result:
wrong answer 1st numbers differ - expected: '32208', found: '17345'
Test #21:
score: 0
Wrong Answer
time: 2278ms
memory: 95712kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 ...
result:
wrong answer 1st numbers differ - expected: '29187', found: '17006'
Test #22:
score: 0
Wrong Answer
time: 2140ms
memory: 89596kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
32606 33432 33433 33434 33435 33436 33437 33438 33439 33440 33441 33442 33443 33444 33445 33446 33447 33448 33449 33450 33451 33452 33453 33454 33455 33456 33457 33458 33459 33460 33461 33462 33463 33464 33465 33466 33467 33468 33469 33470 33471 33472 33473 33474 33475 33476 33477 33478 33479 33480 ...
result:
wrong answer 1st numbers differ - expected: '24124', found: '32606'
Test #23:
score: 0
Wrong Answer
time: 2391ms
memory: 100136kb
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 ...
result:
wrong answer 1st numbers differ - expected: '32229', found: '17907'
Test #24:
score: 4.16667
Accepted
time: 253ms
memory: 84452kb
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 25...
result:
ok 200000 numbers