QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#876855 | #7884. Campus Partition | bamboo123 | ML | 1008ms | 780768kb | C++14 | 3.4kb | 2025-01-31 14:10:34 | 2025-01-31 14:10:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, a[maxn], ls[maxn], tot;
struct node {
int l, r;
__int128 mx, mx1;
};
int rt[maxn];
struct Segtree {
node tr[maxn * 50];
__int128 tag[maxn * 50], tot;
void pushup(int t) {
tr[t].mx = max(tr[tr[t].l].mx, tr[tr[t].r].mx);
tr[t].mx1 = max(tr[tr[t].l].mx1, tr[tr[t].r].mx1);
}
void addtag(int t, __int128 val) {
tr[t].mx += val;
tr[t].mx1 += val;
tag[t] += val;
}
void pushdown(int t) {
if(tr[t].l)
addtag(tr[t].l, tag[t]);
if(tr[t].r)
addtag(tr[t].r, tag[t]);
tag[t] = 0;
}
int modify(int l, int r, int pos, int t, __int128 val) {
if(!t)
t = ++tot;
if(l == r) {
tr[t].mx = val;
tr[t].mx1 = val + ls[l];
return t;
}
pushdown(t);
int mid = l + r >> 1;
if(pos <= mid)
tr[t].l = modify(l, mid, pos, tr[t].l, val);
else
tr[t].r = modify(mid + 1, r, pos, tr[t].r, val);
pushup(t);
return t;
}
int mrg(int l, int r, int x, int y, __int128 &res, __int128 mxx, __int128 mxy, __int128 v1, __int128 v2) {
// if(v1 < 0 || v2 < 0)
// exit(1);
// if(x && tr[x].mx < 0)
// exit(1);
// if(y && tr[y].mx < 0)
// exit(1);
// cout << l << " " << r << " " << mxx << " " << mxy << " " << tr[x].mx << " " << tr[y].mx << endl;
if(!x) {
res = max(res, tr[y].mx1 + mxx);
addtag(y, v2);
return y;
}
if(!y) {
res = max(res, tr[x].mx1 + mxy);
addtag(x, v1);
return x;
}
if(l == r) {
res = max(res, max(max(mxx + tr[y].mx, mxy + tr[x].mx), tr[x].mx + tr[y].mx) + ls[l]);
tr[x].mx = max(tr[x].mx + v1, tr[y].mx + v2);
tr[x].mx1 = tr[x].mx + ls[l];
return x;
}
int mid = l + r >> 1;
pushdown(x), pushdown(y);
tr[x].l = mrg(l, mid, tr[x].l, tr[y].l, res, max(mxx, tr[tr[x].r].mx), max(mxy, tr[tr[y].r].mx), v1, v2);
tr[x].r = mrg(mid + 1, r, tr[x].r, tr[y].r, res, mxx, mxy, v1, v2);
pushup(x);
return x;
}
int query(int l, int r, int pos, int t) {
if(l == r)
return tr[t].mx;
int mid = l + r >> 1;
pushdown(t);
if(pos <= mid)
return query(l, mid, pos, tr[t].l);
else
return query(mid + 1, r, pos, tr[t].r);
}
} tree;
__int128 g[maxn], sum[maxn];
vector<int> e[maxn];
void dfs(int u, int fa) {
sum[u] = ls[a[u]];
__int128 s = 0;
rt[u] = tree.modify(0, n, a[u], rt[u], 0);
tree.modify(0, n, 0, rt[u], 0);
for (int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if(v == fa)
continue;
dfs(v, u);
if(sum[u] < 0)
exit(1);
if(s > sum[u])
exit(1);
rt[u] = tree.mrg(0, n, rt[u], rt[v], g[u], (__int128)(-1e15), (__int128)-1e15, g[v], s);
s += g[v];
rt[u] = tree.modify(0, n, 0, rt[u], g[u]);
sum[u] += sum[v];
// cout << u << " " << v << " " << lst << " " << g[u] << " " << tree.query(0, n, 0, rt[u]) << endl;
}
// cout << u << " " << g[u] << endl;
}
void write(__int128 x) {
if(x <= 9) {
putchar(x + '0');
return ;
}
write(x / 10);
putchar(x % 10 + '0');
}
signed main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], ls[i] = a[i];
sort(ls + 1, ls + n + 1);
for (int i = 1; i <= n; i++)
a[i] = lower_bound(ls + 1, ls + n + 1, a[i]) - ls;
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
e[x].push_back(y);
e[y].push_back(x);
}
tree.tr[0].mx = tree.tr[0].mx1 = -1e15;
dfs(1, 0);
write(g[1]), putchar('\n');
return 0;
}
/*
8
2 5 4 5 3 1 1 3
1 2
1 3
1 4
2 5
2 6
3 7
4 8
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 25004kb
input:
8 2 5 4 5 3 1 1 3 1 2 1 3 1 4 2 5 2 6 3 7 4 8
output:
8
result:
ok 1 number(s): "8"
Test #2:
score: 0
Accepted
time: 1008ms
memory: 780768kb
input:
500000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000...
output:
250000000000000
result:
ok 1 number(s): "250000000000000"
Test #3:
score: 0
Accepted
time: 1ms
memory: 24076kb
input:
10 7 5 6 2 10 10 4 5 4 9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
output:
25
result:
ok 1 number(s): "25"
Test #4:
score: 0
Accepted
time: 0ms
memory: 24756kb
input:
10 6 8 7 3 1 3 1 1 9 4 1 5 6 5 7 8 8 4 9 5 5 2 2 4 4 3 3 10
output:
14
result:
ok 1 number(s): "14"
Test #5:
score: 0
Accepted
time: 1ms
memory: 25160kb
input:
10 4 1 5 9 4 6 7 4 6 10 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10
output:
9
result:
ok 1 number(s): "9"
Test #6:
score: 0
Accepted
time: 1ms
memory: 22568kb
input:
10 4 2 2 2 1 5 10 6 7 9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
output:
18
result:
ok 1 number(s): "18"
Test #7:
score: 0
Accepted
time: 2ms
memory: 23636kb
input:
10 2 4 5 4 6 8 6 10 3 9 1 2 2 3 3 4 4 5 4 6 2 7 4 8 2 9 4 10
output:
14
result:
ok 1 number(s): "14"
Test #8:
score: 0
Accepted
time: 1ms
memory: 22384kb
input:
10 5 3 1 5 9 5 10 2 6 3 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10
output:
16
result:
ok 1 number(s): "16"
Test #9:
score: 0
Accepted
time: 0ms
memory: 25248kb
input:
10 9 2 10 10 10 10 9 9 1 2 1 2 2 3 1 4 2 5 5 6 3 7 7 8 3 9 1 10
output:
30
result:
ok 1 number(s): "30"
Test #10:
score: 0
Accepted
time: 0ms
memory: 22512kb
input:
10 8 1 3 9 5 3 7 8 8 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
output:
26
result:
ok 1 number(s): "26"
Test #11:
score: 0
Accepted
time: 4ms
memory: 23700kb
input:
10 4 1 9 2 7 2 3 3 2 1 1 6 2 3 6 5 5 4 9 3 3 4 4 8 8 7 7 10
output:
12
result:
ok 1 number(s): "12"
Test #12:
score: 0
Accepted
time: 0ms
memory: 23844kb
input:
10 4 10 7 10 10 3 6 4 2 4 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10
output:
10
result:
ok 1 number(s): "10"
Test #13:
score: 0
Accepted
time: 1ms
memory: 25456kb
input:
10 7 8 4 3 3 1 2 6 3 5 1 2 2 3 3 4 4 5 5 6 6 7 6 8 6 9 6 10
output:
15
result:
ok 1 number(s): "15"
Test #14:
score: 0
Accepted
time: 0ms
memory: 22504kb
input:
10 7 10 6 6 5 8 5 9 1 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 3 10
output:
26
result:
ok 1 number(s): "26"
Test #15:
score: 0
Accepted
time: 1ms
memory: 24044kb
input:
10 3 4 5 5 2 1 8 4 1 9 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10
output:
14
result:
ok 1 number(s): "14"
Test #16:
score: 0
Accepted
time: 1ms
memory: 25288kb
input:
10 3 3 1 3 3 7 4 6 2 10 1 2 2 3 1 4 1 5 1 6 5 7 1 8 3 9 8 10
output:
14
result:
ok 1 number(s): "14"
Test #17:
score: 0
Accepted
time: 1ms
memory: 24544kb
input:
100 75 663 978 51 901 936 15 953 5 73 996 29 962 939 932 52 906 79 919 554 927 902 36 110 56 176 95 944 938 916 53 985 37 56 922 48 79 380 887 60 30 480 10 966 12 16 942 79 74 62 10 255 77 84 248 87 19 22 71 57 18 915 80 917 22 606 31 96 51 966 95 88 722 721 1000 14 22 744 75 31 34 70 298 6 922 74 5...
output:
15374
result:
ok 1 number(s): "15374"
Test #18:
score: 0
Accepted
time: 1ms
memory: 23844kb
input:
100 80 60 24 922 68 968 28 29 53 25 28 468 914 41 30 942 53 903 901 61 20 52 32 88 81 66 98 950 11 961 37 900 75 971 101 23 764 981 8 53 913 969 24 6 15 949 99 970 52 976 51 595 397 981 96 44 6 41 903 935 968 74 17 78 44 608 977 911 1000 8 942 918 27 51 806 55 94 923 996 189 166 14 747 472 66 79 48 ...
output:
15348
result:
ok 1 number(s): "15348"
Test #19:
score: 0
Accepted
time: 2ms
memory: 24700kb
input:
100 960 907 926 64 33 66 75 27 68 769 902 931 684 975 903 97 42 7 905 13 46 79 937 93 40 76 11 955 15 39 54 300 968 919 988 66 979 980 169 2 955 952 59 383 52 396 942 64 62 966 59 904 96 924 884 80 823 937 762 915 743 20 998 903 34 713 913 987 49 18 946 541 76 959 717 52 21 73 951 963 4 151 19 12 81...
output:
998
result:
ok 1 number(s): "998"
Test #20:
score: 0
Accepted
time: 0ms
memory: 24188kb
input:
100 129 57 62 25 909 98 55 3 440 842 34 27 933 227 78 18 918 43 956 67 39 703 911 509 78 30 476 29 88 95 927 988 74 69 66 8 969 91 954 983 787 13 5 63 987 504 88 945 72 913 67 363 309 91 51 37 913 57 2 28 612 66 68 64 303 71 949 29 987 949 96 85 901 68 904 992 93 21 6 52 786 71 53 99 907 933 976 865...
output:
9930
result:
ok 1 number(s): "9930"
Test #21:
score: 0
Accepted
time: 0ms
memory: 25116kb
input:
100 54 4 906 997 8 73 35 79 98 402 77 922 310 943 76 5 8 36 462 986 32 32 8 948 37 62 18 56 992 336 669 903 33 85 11 51 2 967 62 976 67 98 840 35 589 920 76 6 17 501 975 407 14 998 84 96 901 9 934 906 289 23 904 990 990 523 86 999 3 92 43 17 95 88 924 68 19 70 73 578 64 14 19 983 397 6 878 510 999 4...
output:
15055
result:
ok 1 number(s): "15055"
Test #22:
score: 0
Accepted
time: 1ms
memory: 25048kb
input:
100 27 919 452 70 74 37 14 11 80 62 844 918 64 12 950 896 986 971 43 7 58 925 15 65 39 27 32 928 64 15 97 987 937 33 999 26 93 986 69 343 85 92 953 917 880 616 221 32 926 8 16 5 961 31 73 303 90 6 57 87 36 935 471 84 12 821 21 989 657 1 340 296 11 97 57 523 91 29 994 927 7 870 918 70 10 68 17 963 96...
output:
14225
result:
ok 1 number(s): "14225"
Test #23:
score: 0
Accepted
time: 1ms
memory: 24068kb
input:
100 18 1 89 931 3 69 95 88 994 83 84 83 641 806 59 912 8 75 15 959 51 86 921 70 66 81 934 35 36 970 3 970 76 981 44 101 93 479 907 7 990 907 33 59 7 45 8 980 38 56 726 88 44 157 5 944 429 924 22 66 87 94 923 942 2 630 68 63 92 44 96 49 970 904 77 5 18 604 49 37 57 3 86 23 904 40 30 984 19 975 367 95...
output:
12436
result:
ok 1 number(s): "12436"
Test #24:
score: 0
Accepted
time: 0ms
memory: 23640kb
input:
100 629 909 904 632 485 339 719 758 724 769 180 866 743 470 103 114 871 523 19 826 224 381 445 978 978 814 729 622 75 899 94 484 108 719 29 897 671 311 421 965 616 381 394 866 681 990 826 65 443 3 495 997 708 956 47 181 756 856 783 518 335 614 4 223 222 63 512 620 685 545 163 740 303 718 935 667 885...
output:
22674
result:
ok 1 number(s): "22674"
Test #25:
score: 0
Accepted
time: 0ms
memory: 25444kb
input:
100 39 270 297 328 551 74 560 69 727 734 175 863 441 680 355 266 868 413 710 91 428 404 269 484 151 827 672 348 569 913 501 853 609 266 364 229 922 383 140 557 3 171 885 237 735 319 685 206 462 950 459 750 562 82 575 240 651 653 536 505 828 569 380 188 755 769 84 583 77 125 389 28 183 898 16 865 366...
output:
17692
result:
ok 1 number(s): "17692"
Test #26:
score: 0
Accepted
time: 0ms
memory: 24744kb
input:
100 961 142 986 617 810 618 105 379 538 698 682 668 242 185 312 609 969 303 506 355 143 940 492 990 323 248 126 371 358 631 804 222 302 621 892 664 572 238 755 854 902 369 376 416 598 135 840 835 993 385 936 696 224 696 998 298 546 155 993 980 424 524 564 857 584 75 655 250 764 897 214 612 960 375 2...
output:
993
result:
ok 1 number(s): "993"
Test #27:
score: 0
Accepted
time: 3ms
memory: 24832kb
input:
100 883 798 675 314 581 353 947 497 349 662 781 769 940 395 564 761 70 384 301 915 859 964 124 688 600 454 773 393 852 156 211 590 995 168 420 995 119 309 962 446 290 863 868 83 948 656 699 976 11 332 197 937 374 414 718 357 442 952 450 967 621 992 236 822 926 973 523 917 155 182 439 196 136 659 856...
output:
23051
result:
ok 1 number(s): "23051"
Test #28:
score: 0
Accepted
time: 1ms
memory: 24936kb
input:
100 294 967 68 602 839 896 788 103 56 819 480 278 742 413 112 401 67 274 801 180 871 795 243 193 476 875 419 119 345 170 322 663 689 522 755 430 665 869 577 743 85 60 359 158 107 985 854 309 30 767 970 691 36 836 949 415 233 454 907 250 114 651 317 299 755 679 94 880 842 466 369 483 17 431 936 563 8...
output:
18295
result:
ok 1 number(s): "18295"
Test #29:
score: 0
Accepted
time: 3ms
memory: 25468kb
input:
100 512 327 460 299 906 440 630 710 163 783 283 379 439 622 261 744 168 164 493 444 587 523 171 699 945 81 169 950 431 183 921 32 190 365 987 762 211 236 1000 335 984 258 850 529 266 506 713 42 561 713 935 932 699 450 669 474 424 251 660 724 311 414 989 967 288 281 962 547 233 942 786 771 602 716 20...
output:
18827
result:
ok 1 number(s): "18827"
Test #30:
score: 0
Accepted
time: 2ms
memory: 24232kb
input:
100 434 792 149 292 869 175 367 828 166 747 790 889 945 832 513 896 973 350 584 708 599 59 98 205 222 502 816 972 116 709 32 697 883 720 323 93 566 795 206 632 371 48 637 900 320 323 868 184 283 852 708 686 553 873 92 829 24 753 821 711 804 369 661 932 926 179 533 214 112 227 419 355 674 896 289 766...
output:
19266
result:
ok 1 number(s): "19266"
Test #31:
score: -100
Memory Limit Exceeded
input:
500000 10 8 8 2 3 7 3 1 1 10 10 8 8 2 2 3 1 9 8 9 10 3 1 1 7 5 4 6 10 9 4 7 7 9 4 2 10 8 8 6 5 9 10 10 4 8 3 2 5 9 5 7 4 5 7 2 1 10 6 1 7 2 9 4 10 2 3 2 6 3 7 10 7 9 2 4 3 4 5 5 6 4 1 2 6 2 5 9 5 9 3 3 7 8 6 7 1 10 6 2 1 2 1 7 10 10 10 10 10 2 2 3 4 6 6 4 4 5 3 6 4 6 1 5 2 3 4 6 8 6 1 4 7 4 10 5 1 3...
output:
1114634