QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#614530 | #8237. Sugar Sweet II | LeoG | TL | 304ms | 15244kb | C++20 | 2.3kb | 2024-10-05 16:31:32 | 2024-10-05 16:31:32 |
Judging History
answer
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, MOD = 1e9 + 7;
vector<int> a, b, w, fa, Arrangement(N), dep;
int mpow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
res = 1ll * res * a % MOD;
a = 1ll * a * a % MOD;
b >>= 1;
}
return res;
}
int find(int x) {
std::stack<int> stack;
stack.push(x);
while (!stack.empty()) {
int current = stack.top();
stack.pop();
if (dep[current]) {
continue;
}
if (fa[current] && fa[current] != current) {
stack.push(current);
stack.push(fa[current]);
} else if (fa[current] == current) {
dep[current] = 1;
} else {
dep[current] = -MOD;
}
if (!stack.empty() && dep[stack.top()]) {
dep[current] = dep[stack.top()] + 1;
}
}
return dep[x];
}
int solve(int x)
{
if (fa[x] == 0)
return a[x] % MOD;
else if (fa[x] == x)
return (a[x] + w[x]) % MOD;
int d = find(x);
if (d < 0)
return a[x] % MOD;
return (a[x] + 1ll * w[x] * mpow(Arrangement[d], MOD - 2) % MOD) % MOD;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
Arrangement[0] = 1;
for (int i = 1; i < N; i++)
Arrangement[i] = 1ll * Arrangement[i - 1] * i % MOD;
while (T--)
{
int n;
cin >> n;
a.resize(n + 5);
b.resize(n + 5);
w.resize(n + 5);
fa.resize(n + 5);
dep.resize(n + 5);
for (int i = 1; i <= n; i++)
cin >> a[i], fa[i] = dep[i] = 0;
for (int i = 1; i <= n; i++)
cin >> b[i];
for (int i = 1; i <= n; i++)
cin >> w[i];
for (int i = 1; i <= n; i++)
{
if (i == b[i])
fa[i] = 0;
else if (a[i] < a[b[i]])
fa[i] = i;
else if (a[i] < w[b[i]] + a[b[i]])
fa[i] = b[i];
else
fa[i] = 0;
}
for (int i = 1; i <= n; i++)
cout << solve(i) << " ";
cout << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5048kb
input:
4 4 2 5 5 2 4 2 1 3 3 2 1 4 3 5 4 3 1 1 1 6 6 6 3 5 4 3 2 3 1 1 2 3 5 2 1 3 2 1 5 1 1 3 4 1 3 4 2 4
output:
500000007 5 5 6 5 10 9 166666673 5 6 500000006 4 3 4 5
result:
ok 15 numbers
Test #2:
score: 0
Accepted
time: 152ms
memory: 5196kb
input:
50000 5 508432375 168140163 892620793 578579275 251380640 3 4 4 1 3 346232959 736203130 186940774 655629320 607743104 1 863886789 1 364158084 18 864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...
output:
854665334 904343293 590444253 906393935 859123744 863886789 871186919 814243920 968784984 206455474 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959 665922935 577926775 132646723 421298438 601054667 99438820...
result:
ok 500000 numbers
Test #3:
score: 0
Accepted
time: 139ms
memory: 5196kb
input:
5000 32 770948244 768935990 679008477 140059459 126718762 172369567 60723299 139563987 671220914 149354390 157693423 281776861 412464718 869245476 446234952 827569915 294423239 672541652 309942421 252597259 603376135 207343159 409718246 366254256 59153054 589167713 171168426 802508752 545258815 7373...
output:
351791786 607445427 53351025 96277100 671164280 94237502 491532542 689573864 38427324 382704130 816373728 309284273 690430261 869245476 291526004 827569915 99656414 748890295 609504084 467918578 603376135 501064338 501009010 85120176 17055939 969361082 892391697 802508752 545258815 333168524 1165920...
result:
ok 500000 numbers
Test #4:
score: 0
Accepted
time: 144ms
memory: 5048kb
input:
500 831 941014154 653817226 737317394 548891909 650240324 784152288 337897920 919008275 807254058 243382923 935726688 583553418 88833789 750399807 577623567 595406246 982133524 532836513 286249019 780939608 102125785 501372622 567570217 761966021 24185161 261377492 548514161 60622277 424507766 53868...
output:
230438528 510098604 738011151 548891909 580268181 784152288 162823835 919008275 807254058 787780699 169718491 688535564 555077367 834193932 577623567 199204617 982133524 727940533 893934195 780939608 85179822 570950549 313144085 761966021 26675845 421401245 433344217 526647729 682184775 211847212 73...
result:
ok 500000 numbers
Test #5:
score: 0
Accepted
time: 146ms
memory: 6436kb
input:
50 15547 79925051 123631785 451773599 290189979 8044136 985464173 25844832 289071231 239507420 289791589 915514277 479789432 442504231 802244771 740816861 403881580 976651260 224945729 212313439 170590725 774102963 484364764 878553828 980521523 949147335 322115091 853616905 280879526 946658136 41530...
output:
284534137 370503506 451773599 700040615 326578027 985464173 354118840 239415089 726041909 289791589 556747412 978820837 939334538 802244771 214118527 173005433 375935904 451502781 253004725 744007077 774102963 484364764 542287768 980521523 949147335 322115091 853616905 154636248 189607066 65863811 8...
result:
ok 500000 numbers
Test #6:
score: 0
Accepted
time: 157ms
memory: 11504kb
input:
5 323943 564718673 657489855 373282330 667584659 850348020 593942770 903492853 897217447 211655411 409828915 29599937 288981803 845363118 245960658 704846394 990499066 857206811 623803672 777109873 572212135 214230853 504844223 538282312 203265013 863644873 788795254 168640248 169295455 678434394 38...
output:
204335171 290429080 749926264 271550943 90669489 593942770 395887561 897217447 1122646 649466642 138487066 705990516 34851501 408992627 704846394 990499066 593266173 831686749 216979707 572212135 604730070 581128644 538282312 915779172 863644873 788795254 107879446 16737318 389993995 484044594 99298...
result:
ok 500000 numbers
Test #7:
score: 0
Accepted
time: 151ms
memory: 14916kb
input:
1 500000 167959139 634074578 773386884 503722503 2087506 443917727 691952768 150414369 45277106 674936674 279198849 421436316 140515506 451394766 695984050 254786900 17672277 228874183 25408976 166546966 609238004 783615461 523051181 854261286 433389755 773727734 127680943 43034811 892657961 4051531...
output:
504430027 374470866 773386884 605046474 48849004 443917727 489848651 669510599 807540660 887381332 434390308 135903469 373981470 557045185 817416129 330927981 295776527 272925008 971275424 706959112 736278170 783615461 722484387 90619431 433389755 621669631 773800724 379052020 48101859 531350264 344...
result:
ok 500000 numbers
Test #8:
score: 0
Accepted
time: 304ms
memory: 5032kb
input:
500000 1 641679119 1 46363815 1 409126743 1 301850965 1 479434398 1 8087428 1 411052661 1 274814245 1 186109822 1 557051627 1 832961130 1 468245461 1 885774901 1 205966212 1 640022376 1 40112818 1 451543633 1 116777249 1 301353585 1 865639134 1 978071440 1 696105130 1 54386476 1 950481378 1 64069307...
output:
641679119 409126743 479434398 411052661 186109822 832961130 885774901 640022376 451543633 301353585 978071440 54386476 640693072 310901737 164748344 943205363 136533544 727803925 460499039 232286575 665455084 837511803 315243095 26199131 205377168 16924774 899338264 331544...
result:
ok 500000 numbers
Test #9:
score: 0
Accepted
time: 152ms
memory: 10204kb
input:
10 994 192416571 736204386 760505341 281487941 959877974 851233214 663685576 509485978 365899785 537198264 317546803 126718048 460789228 897818658 18680833 984634479 787676854 563354285 458421086 514363741 55602674 442874164 399256047 391675537 579236972 193191514 216578877 252675142 31271148 194871...
output:
80356619 157454092 293206050 6022041 731069905 151604390 514334525 563895392 669575403 774624127 317546803 558641523 992472593 125739893 949366972 984634479 881030593 213655841 746280399 717378810 243072174 891873405 399256047 391675537 579236972 807047987 154410058 377650673 806701544 229640743 643...
result:
ok 500000 numbers
Test #10:
score: 0
Accepted
time: 145ms
memory: 7748kb
input:
10 4059 462418415 299614005 191197342 44762066 300901267 961385491 493309329 213597634 708323640 936922772 23497231 461479602 140639435 670254967 996272440 567967593 778428919 911913371 691558703 798524068 217290311 543452971 177158172 239882043 702125314 764626435 303826249 933606746 990809366 4915...
output:
462418415 299614005 191197342 44762066 300901267 961385491 493309329 213597634 708323640 936922772 23497231 461479602 140639435 670254967 996272440 567967593 778428919 911913371 691558703 798524068 217290311 543452971 177158172 239882043 702125314 764626435 303826249 933606746 990809366 491533949 60...
result:
ok 500000 numbers
Test #11:
score: 0
Accepted
time: 98ms
memory: 8588kb
input:
10 49657 477209301 840947435 604333484 622155980 678038014 465127998 957089089 894355510 968377771 949800543 862532505 722250589 268937725 969156606 245573854 500968667 899760449 185359545 330216049 769402977 835596137 37232643 40464607 103622719 539578359 384115371 545015501 704344659 315733366 841...
output:
24386105 149263245 522545322 156355561 933854795 205614293 995498647 387478497 31080083 918162413 730147773 712199885 489347745 311275833 154244381 933432658 13230008 733866425 156882463 932040286 265929098 20750751 332234430 298250715 885880707 802538280 397378619 111913092 274794813 303506899 8109...
result:
ok 500000 numbers
Test #12:
score: 0
Accepted
time: 151ms
memory: 14916kb
input:
1 500000 236077977 86078372 260030447 395969828 987989549 57339217 275325872 747113738 476061771 412580153 589144151 967633191 693832436 289364785 777281154 23767823 119804469 146277545 336859935 601994339 832084033 682171073 644911370 660086987 120599826 211375398 248467132 452293864 933711769 2134...
output:
376442474 992314650 260030447 347914044 987989549 261512576 620193765 252324505 790146989 865733342 834724719 967633191 693832436 494604333 184612685 915370675 330814922 883875013 449533217 314709314 691938928 269064599 644911370 355292009 913699960 679983605 248467132 35400898 933711769 841252659 7...
result:
ok 500000 numbers
Test #13:
score: 0
Accepted
time: 155ms
memory: 15244kb
input:
1 500000 754060142 541221496 818408807 292833795 269734500 93049742 705655834 477052382 289531753 577836666 901816346 592911223 178433769 399478363 283922345 827396041 473750582 281252938 115479501 635570951 994423568 64580695 992394937 399396922 971794709 932519987 788927885 774996990 50650116 2299...
output:
754060142 541221496 818408807 292833795 269734500 93049742 705655834 477052382 289531753 577836666 901816346 592911223 178433769 399478363 283922345 827396041 473750582 281252938 115479501 635570951 994423568 64580695 992394937 399396922 971794709 932519987 788927885 774996990 50650116 229975458 916...
result:
ok 500000 numbers
Test #14:
score: 0
Accepted
time: 127ms
memory: 15048kb
input:
1 500000 948465494 889673067 78604955 288678380 624259497 587725418 914612913 470414324 640293852 69403930 732304399 984202467 475189347 358661723 895292367 904751346 482451942 380615981 38838062 846352252 118278338 801007154 879314174 400606456 470425193 156860717 555128174 660373938 534085518 6964...
output:
682100510 69430093 885818571 433752951 55853128 688792518 262586376 340748896 446059734 917268853 348594032 2950636 384831840 755020970 899489920 635829311 684006178 834972925 840082011 289298627 171986021 9622553 639011662 141474012 211951889 579556713 801939340 729112576 48106562 188754659 4252982...
result:
ok 500000 numbers
Test #15:
score: -100
Time Limit Exceeded
input:
8960 4 1 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 2 1 1 1 1 4 1 1 1 1 1 1 1 3 1 1 1 1 4 1 1 1 1 1 1 1 4 1 1 1 1 4 1 1 1 1 1 1 2 1 1 1 1 1 4 1 1 1 1 1 1 2 2 1 1 1 1 4 1 1 1 1 1 1 2 3 1 1 1 1 4 1 1 1 1 1 1 2 4 1 1 1 1 4 1 1 1 1 1 1 3 1 1 1 1 1 4 1 1 1 1 1 1 3 2 1 1 1 1 4 1 1 1 1 1 1 3 3 1 1 1 1 4 1 1 1 1...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1