QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#410634 | #6662. 기지 간소화 | nguyentunglam | 11 | 2757ms | 33380kb | C++17 | 2.9kb | 2024-05-14 10:41:25 | 2024-05-14 10:41:26 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7;
int st[N], ed[N], rev_st[N], dd[N], cnt[2], sz[N], cost[N];
int pref[2][N << 2], suff[2][N << 2], g[N << 2], sum[N << 2];
vector<pair<int, int> > adj[N];
int timer, n;
long long ans;
void up(int s, int l, int r, int pos, int val) {
if (l == r) {
pref[0][s] = pref[1][s] = suff[0][s] = suff[1][s] = 0;
pref[val][s] = suff[val][s] = 1;
// sum[s] = 0;
sum[s] = 1;
g[s] = val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) up(s << 1, l, mid, pos, val);
else up(s << 1 | 1, mid + 1, r, pos, val);
g[s] = g[s << 1] + g[s << 1 | 1];
sum[s] = (sum[s << 1] + sum[s << 1 | 1]) % mod;
for(int j = 0; j < 2; j++) {
pref[j][s] = pref[j][s << 1];
// if (pos == n - 1) {
// cout << sum[s << 1] <<
// }
if (g[s << 1] == (mid - l + 1) * j) pref[j][s] += pref[j][s << 1 | 1];
suff[j][s] = suff[j][s << 1 | 1];
if (g[s << 1 | 1] == (r - mid) * j) suff[j][s] += suff[j][s << 1];
sum[s] = (sum[s] + 1LL * suff[j][s << 1] * pref[j][s << 1 | 1] % mod) % mod;
}
// if (pos == n - 1) cout << sum[s] << " " << l << " " << r << endl;
}
void prep (int u, int p) {
// cout << u << endl;
st[u] = ++timer;
rev_st[timer] = u;
sz[u] = 1;
for(auto &[v, c] : adj[u]) if (v != p) {
cost[v] = c;
prep(v, u);
sz[u] += sz[v];
}
ed[u] = timer;
}
void dfs(int u, int p) {
int heavy = 0;
// cout << u << endl;
for(auto &[v, w] : adj[u]) if (v != p && sz[v] > sz[heavy]) heavy = v;
for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
dfs(v, u);
for(int j = st[v]; j <= ed[v]; j++) {
up(1, 0, n - 1, rev_st[j], 0);
}
}
if (heavy) dfs(heavy, u);
// dd[u] = 1;
up(1, 0, n - 1, u, 1);
for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
for(int j = st[v]; j <= ed[v]; j++) {
up(1, 0, n - 1, rev_st[j], 1);
}
}
int ways = (1LL * n * (n + 1) / 2 % mod - sum[1] + mod) % mod * cost[u] % mod;
ans += ways;
ans %= mod;
// for(int l = 0; l < n; l++) {
// cnt[0] = cnt[1] = 0;
// for(int r = l; r < n; r++) {
// cnt[dd[r]] = 1;
// if (cnt[0] && cnt[1]) {
// ans += cost[u];
// ans %= mod;
// }
// }
// }
}
int maintenance_costs_sum (vector<int> U, vector<int> V, vector<int> W) {
// n = 5;
n = U.size() + 1;
for(int i = 0; i < n; i++) up(1, 0, n - 1, i, 0);
for(int i = 0; i < n - 1; i++) {
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
}
prep(0, 0);
dfs(0, 0);
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int tot = maintenance_costs_sum({0, 2, 2, 0}, {2, 1, 4, 3}, {2, 1, 1, 1});
cout << tot << endl;
}
#endif // ngu
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 2ms
memory: 28200kb
input:
2 1 0 1
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 8ms
memory: 28084kb
input:
299 72 263 662908099 170 230 583287081 181 87 461245480 117 116 41098264 282 218 300936390 238 128 561969086 175 105 305200697 152 28 927649982 211 58 290232523 233 188 304617152 246 74 61325892 283 120 724838352 207 94 123021801 138 241 893659708 171 283 82846115 104 250 142703714 111 63 547249068 ...
output:
93964028
result:
ok single line: '93964028'
Test #3:
score: 0
Accepted
time: 13ms
memory: 29004kb
input:
300 116 249 999999948 23 182 999999956 174 147 999999978 21 178 999999981 138 238 999999941 215 6 999999955 190 256 999999990 107 203 999999992 142 221 999999943 129 236 999999957 55 154 999999992 183 35 999999979 200 284 999999958 36 222 999999988 88 62 999999960 219 222 999999950 95 38 999999960 2...
output:
534442086
result:
ok single line: '534442086'
Test #4:
score: 0
Accepted
time: 2ms
memory: 28144kb
input:
5 0 2 2 2 1 3 2 4 6 0 3 5
output:
98
result:
ok single line: '98'
Test #5:
score: 0
Accepted
time: 0ms
memory: 27640kb
input:
10 1 5 1 6 2 10 8 1 3 3 9 1 8 0 10 2 3 6 7 0 5 2 4 7 4 7 3
output:
1625
result:
ok single line: '1625'
Test #6:
score: 0
Accepted
time: 0ms
memory: 28560kb
input:
10 2 5 242840396 4 7 425058513 7 9 975802175 7 1 647540462 3 7 239781206 2 0 873867121 4 0 132645393 6 4 862552539 0 8 542726039
output:
711424766
result:
ok single line: '711424766'
Test #7:
score: 0
Accepted
time: 0ms
memory: 31428kb
input:
299 107 270 74 187 221 79 142 129 40 77 272 27 94 152 1 117 171 74 64 79 87 263 36 91 257 176 1 183 191 82 109 67 23 91 90 30 116 228 76 276 152 15 59 165 40 160 235 71 235 116 82 102 44 88 130 289 72 241 150 14 62 176 23 277 50 83 206 38 11 66 250 72 84 120 66 137 259 54 46 287 82 209 263 57 151 24...
output:
399821685
result:
ok single line: '399821685'
Test #8:
score: 0
Accepted
time: 2ms
memory: 28972kb
input:
300 111 245 844422933 103 173 980761620 210 18 509066965 36 51 134507716 264 28 523343821 249 88 748193505 30 46 832843261 286 33 489900279 125 288 575144383 106 87 191375234 259 126 882094013 10 57 842095866 73 65 254207339 175 280 353530327 21 145 883665290 130 110 818300025 137 48 259121083 0 102...
output:
156555229
result:
ok single line: '156555229'
Test #9:
score: 0
Accepted
time: 0ms
memory: 29524kb
input:
292 37 89 397871374 184 205 437399619 14 234 93902029 40 155 84065200 12 60 367994608 143 35 623301168 125 0 307800516 68 281 653696611 146 260 978361615 177 79 385187016 251 192 369734594 245 159 471356654 109 53 96376804 248 103 681417305 85 288 837771859 25 110 145156090 129 46 874442695 252 249 ...
output:
702681209
result:
ok single line: '702681209'
Test #10:
score: 0
Accepted
time: 0ms
memory: 31504kb
input:
285 93 162 959772147 32 172 751202610 173 257 284177666 199 112 94146918 80 117 514639361 245 265 282507802 135 109 829039102 53 38 832201899 166 245 984506327 247 224 843352696 4 98 415992963 189 33 989751728 117 153 801360684 106 213 848238622 119 77 300949830 177 29 951903134 110 27 991808126 102...
output:
281677588
result:
ok single line: '281677588'
Test #11:
score: 0
Accepted
time: 7ms
memory: 30904kb
input:
300 238 117 820410457 230 235 506467445 200 74 653603782 62 76 485420791 105 275 392365065 20 121 28250604 203 87 723682049 109 151 342880635 133 79 302983289 47 297 671031835 257 299 444969934 255 116 860542983 91 92 332672418 205 280 290258390 103 160 769766288 106 137 789215359 159 204 831106721 ...
output:
144479203
result:
ok single line: '144479203'
Test #12:
score: 0
Accepted
time: 12ms
memory: 31212kb
input:
298 157 32 579243060 277 30 236601044 95 45 746850490 123 94 873131928 185 95 376156675 66 150 606904039 262 124 953899592 219 279 373193513 152 99 546195756 113 100 936194271 216 240 22619249 38 110 395200927 89 55 159263634 265 77 151240149 90 232 529211969 88 177 115952104 43 208 657967542 196 45...
output:
75917669
result:
ok single line: '75917669'
Test #13:
score: 0
Accepted
time: 0ms
memory: 31232kb
input:
300 86 72 35281851 86 65 772642113 86 85 267144561 68 86 237759697 225 86 275794715 188 86 708990293 86 224 212292280 61 86 139649599 141 86 852035124 131 86 522883805 62 86 374119946 86 107 661877081 218 86 365176681 14 86 676939669 86 254 678988772 86 189 741173114 86 175 101646645 86 10 776575255...
output:
970519677
result:
ok single line: '970519677'
Test #14:
score: 0
Accepted
time: 2ms
memory: 28056kb
input:
296 167 280 308339229 42 120 44980047 172 173 31087131 280 172 346584469 68 172 765593132 172 11 55592625 172 60 294200085 172 177 829489003 172 26 368114672 172 6 906646562 239 172 311081319 199 172 89488635 264 172 782569720 172 53 614220967 172 192 111994073 70 172 348033132 172 37 5375089 10 108...
output:
685676493
result:
ok single line: '685676493'
Test #15:
score: 0
Accepted
time: 5ms
memory: 29720kb
input:
299 219 189 428591428 214 280 645468138 239 124 44092787 278 109 973174819 229 28 708387042 118 105 231265201 106 23 115324206 127 163 182711436 61 68 171248721 67 218 585337538 198 30 576082156 147 49 179613791 109 244 356639356 122 205 714546250 179 207 831521336 292 188 754667151 91 55 962847082 ...
output:
442304238
result:
ok single line: '442304238'
Test #16:
score: 0
Accepted
time: 3ms
memory: 28508kb
input:
300 72 138 240810351 80 111 534427405 105 137 731389559 86 95 740641088 86 126 67440531 151 147 975723723 84 98 889126297 60 37 235430076 98 236 462897489 183 146 446541268 42 135 730090491 92 259 6083997 189 161 972025910 3 29 546634976 53 270 249844443 28 122 103308828 105 275 560940747 121 124 94...
output:
51045998
result:
ok single line: '51045998'
Test #17:
score: 0
Accepted
time: 8ms
memory: 28692kb
input:
298 15 284 446739499 274 218 9244552 64 69 913186698 79 63 70124148 253 46 192851114 165 30 798777080 30 123 824050137 199 104 363328962 23 70 471441 170 54 206153629 18 49 203147626 83 136 841021519 158 184 342247212 72 139 144579819 237 6 976320498 235 224 141859373 169 39 844270142 231 273 615291...
output:
874031476
result:
ok single line: '874031476'
Test #18:
score: 0
Accepted
time: 8ms
memory: 28164kb
input:
300 211 34 999999974 255 27 999999997 50 98 999999974 16 21 999999946 207 243 999999999 39 64 999999983 279 79 999999975 95 248 999999960 69 276 999999977 94 6 999999960 137 281 999999969 127 49 999999945 122 72 999999958 124 63 999999991 125 52 999999963 152 92 999999994 26 120 999999989 29 275 999...
output:
558463020
result:
ok single line: '558463020'
Subtask #2:
score: 6
Accepted
Dependency #1:
100%
Accepted
Test #19:
score: 6
Accepted
time: 1740ms
memory: 30264kb
input:
3987 1834 2601 895910969 1701 1884 820508615 3406 1155 439833658 2977 3131 370614789 617 2254 829077229 1648 1576 977756493 597 2928 624126121 1165 3147 11258087 3888 2644 341340941 3007 2676 281856885 3971 957 644422867 3791 1181 166275464 3089 2273 36236019 1918 1080 747406617 3032 2602 102478217 ...
output:
695949490
result:
ok single line: '695949490'
Test #20:
score: 0
Accepted
time: 2457ms
memory: 29784kb
input:
3996 2787 2080 999999558 3557 3990 999999513 1092 476 999999510 3925 539 999999701 2334 2431 999999969 895 152 999999232 219 3637 999999392 2547 2072 999999641 356 3936 999999660 741 3794 999999597 554 1962 999999246 1562 847 999999437 2515 3167 999999541 1793 2493 999999388 2723 1175 999999976 2394...
output:
274545286
result:
ok single line: '274545286'
Test #21:
score: 0
Accepted
time: 151ms
memory: 28828kb
input:
3929 2164 3410 1 1115 3069 1 313 571 1 1913 135 1 739 3902 1 3735 1024 1 1888 2039 1 2732 2069 1 276 3073 1 3568 1533 1 2550 3456 1 1519 3224 1 3692 463 1 3013 2755 1 104 2875 1 2838 926 1 1755 880 1 1263 1285 1 2823 492 1 2920 2177 1 3369 1547 1 527 1876 1 416 3588 1 2985 3903 1 3566 454 1 3438 244...
output:
130042608
result:
ok single line: '130042608'
Test #22:
score: 0
Accepted
time: 126ms
memory: 26224kb
input:
3979 1701 2739 947 550 1740 148 698 3394 439 998 1930 516 2860 789 927 3189 1474 727 708 525 93 3380 2998 356 1170 3170 161 3273 441 367 2735 912 327 3676 1798 652 2147 2986 902 374 1300 176 1209 2051 994 2466 2554 652 2952 1243 582 1283 3491 396 3230 3169 323 1357 2960 963 2061 2863 486 374 1551 74...
output:
477691640
result:
ok single line: '477691640'
Test #23:
score: 0
Accepted
time: 138ms
memory: 29836kb
input:
3993 129 1932 486250 3451 3517 971847 1040 2697 612116 2857 2720 474764 2266 1736 25882 608 1032 505927 885 2105 392299 1919 2570 296078 897 799 762212 3365 2413 202700 1016 356 738055 1782 1920 347257 3411 1463 42033 1517 3437 239957 2215 2285 472713 2088 2204 397307 64 1841 65633 2112 2683 695743 ...
output:
260910030
result:
ok single line: '260910030'
Test #24:
score: 0
Accepted
time: 163ms
memory: 28720kb
input:
4000 102 66 280319606 889 2757 123671658 573 410 214275598 1812 1239 562948670 3870 2162 811676687 3666 3927 782714296 743 1959 698091997 3631 1350 693401332 1711 3285 823728351 3648 67 738528891 2207 210 876427103 3115 2668 374409224 3736 1282 355828430 255 1050 984348990 1204 1133 399430224 1924 1...
output:
320372497
result:
ok single line: '320372497'
Test #25:
score: 0
Accepted
time: 31ms
memory: 31488kb
input:
3999 2915 2669 358447038 3538 1529 848338205 1238 1652 883628661 2570 2404 223452927 799 3024 713067679 3364 1135 944572237 1115 1024 910103338 3191 2454 774565382 1908 486 217064756 2637 1209 971634195 1318 634 448430962 1541 1145 290974969 802 2090 33398467 2463 2097 270639399 131 1103 813548390 9...
output:
291075730
result:
ok single line: '291075730'
Test #26:
score: 0
Accepted
time: 96ms
memory: 31292kb
input:
3991 3043 3747 72450807 1035 2439 847359979 992 3356 367969130 2460 2892 203363882 1910 3198 819770022 552 2675 346346980 2432 2936 540637143 190 2609 410257275 2453 3575 355529268 3880 240 12331157 3785 3424 117046626 308 3405 307714020 1833 1500 713642516 2163 3286 125103861 2959 3314 131450008 16...
output:
383646236
result:
ok single line: '383646236'
Test #27:
score: 0
Accepted
time: 406ms
memory: 29808kb
input:
3992 3278 133 82819135 1138 1628 883942927 1518 1562 287462306 3906 2285 844307305 1123 3665 401186676 3421 2844 717868422 3912 2599 449585462 1058 204 196625144 2303 3869 74773481 3479 100 566381291 2638 3113 454696105 2492 2568 324166296 458 1181 267475836 2144 735 853935672 2661 1297 546999171 84...
output:
41229967
result:
ok single line: '41229967'
Test #28:
score: 0
Accepted
time: 2205ms
memory: 28320kb
input:
3988 892 3476 65536289 1143 2155 78839637 2281 3630 277410807 945 2594 77206150 2323 1428 493894594 2462 190 55532886 3692 3325 33104595 1172 3873 760727613 314 1369 239234837 3747 3958 475742505 546 452 197939417 2821 2508 957723778 3234 3065 101320967 3010 2914 19947142 985 959 567195224 2766 1066...
output:
855099036
result:
ok single line: '855099036'
Test #29:
score: 0
Accepted
time: 2757ms
memory: 31620kb
input:
3994 2898 1643 747208501 2589 2491 449233552 1952 108 167973884 809 516 700981441 2928 1529 486022338 996 3324 91061838 195 3011 976655798 2835 2673 861780462 255 1432 112332747 454 3339 275103950 3446 2406 525262110 698 2274 21319511 1906 2757 242526935 2008 3900 78216819 1939 3454 55978905 1478 47...
output:
926381062
result:
ok single line: '926381062'
Test #30:
score: 0
Accepted
time: 7ms
memory: 26200kb
input:
3999 3472 781 632697752 3913 3472 745809874 1012 3472 486404015 3472 1596 224135418 3472 391 174124536 2633 3472 668535346 3472 3820 309058783 3472 2891 828286963 3472 3137 207500886 3827 3472 55431764 3472 3426 830682140 3472 30 17107264 2734 3472 472803946 3472 1632 745035389 3472 1403 940800825 3...
output:
507959495
result:
ok single line: '507959495'
Test #31:
score: 0
Accepted
time: 3ms
memory: 27848kb
input:
4000 174 2644 47696590 169 2644 58166462 956 2644 460771360 2644 1815 520855324 3501 2644 986792612 1093 2644 506436717 966 2644 738217798 900 2644 925731103 2644 3182 714219046 2448 1924 763935310 2644 782 834386632 2516 2644 120793747 2644 1931 124334474 2644 2152 579180059 2644 2521 623628522 164...
output:
433113345
result:
ok single line: '433113345'
Test #32:
score: 0
Accepted
time: 201ms
memory: 33380kb
input:
3991 1764 2443 439882798 426 3349 992175122 2547 235 940448392 3812 2967 72708965 3066 3137 686248279 1981 112 936946216 3045 2145 956855424 3312 649 533586589 2188 2462 64477706 3422 1113 95138241 2078 3202 127346842 901 2425 482329548 3535 2014 303899910 2730 1390 831016026 2128 1974 105603514 17 ...
output:
134545619
result:
ok single line: '134545619'
Test #33:
score: 0
Accepted
time: 291ms
memory: 28704kb
input:
3989 583 3061 706872124 3869 629 162035787 1917 2195 400147505 115 541 844975714 3513 2008 667322023 1355 2871 481525925 1807 2969 702729114 900 2818 55373170 1638 2513 294382411 2375 3093 485828042 1605 507 778393073 3304 3618 865154341 3053 2947 676091124 2773 2732 941702348 1365 3111 167647093 17...
output:
277990633
result:
ok single line: '277990633'
Test #34:
score: 0
Accepted
time: 1005ms
memory: 31480kb
input:
4000 1300 3993 2591399 3255 1495 614218131 24 3304 493255228 1569 2170 381311184 3026 3582 712596783 1738 3970 293387004 3384 781 361133661 2795 2371 93547297 58 698 879035234 573 2317 31056223 2188 3953 11876411 2899 3835 395815431 1436 1529 317267294 2116 2218 285301158 1012 1141 909178003 2762 20...
output:
794389140
result:
ok single line: '794389140'
Test #35:
score: 0
Accepted
time: 1228ms
memory: 28796kb
input:
3996 2148 549 999999448 3741 562 999999858 3245 255 999999581 3611 762 999999421 1088 36 999999288 65 38 999999563 567 93 999999882 3413 841 999999485 3745 1149 999999267 3707 3415 999999905 2468 974 999999999 2085 2633 999999998 3339 1978 999999753 805 1857 999999996 3088 542 999999810 2596 3051 99...
output:
390647746
result:
ok single line: '390647746'
Subtask #3:
score: 0
Time Limit Exceeded
Test #36:
score: 0
Time Limit Exceeded
input:
249494 137621 137622 1 198127 198128 1 117358 117359 1 155777 155778 1 112742 112743 1 235668 235669 1 20632 20622 1 41333 41334 1 170699 170692 1 130269 130268 1 137208 137207 1 37791 37767 1 130187 130178 1 89795 89817 1 91408 91359 1 243301 243574 1 22017 22018 1 116466 116467 1 160094 160095 1 4...
output:
Unauthorized output
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #48:
score: 0
Time Limit Exceeded
input:
249876 133760 107716 545485826 57898 35556 542825636 159559 78814 588304799 9037 105623 735470824 242676 240955 258616989 58653 143501 194781066 36591 97835 699376285 95049 123298 35300031 91751 203920 865003045 53908 18382 376452723 211462 200538 638209824 48693 89388 132147210 238496 151742 322726...
output:
Unauthorized output
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%