QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#624537 | #8649. Escape Route 2 | hhoppitree | 14 | 307ms | 68896kb | C++17 | 4.7kb | 2024-10-09 16:03:09 | 2024-10-09 16:03:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int tl[N], tr[N], nxt[N], Vnxt[N], pre[N], Vpre[N];
pair<int, int> nd[N];
int Nxt[N][320], BIGNxt[N][320];
long long VNxt[N][320], BIGVNxt[N][320];
int Pre[N][320], BIGPre[N][320];
long long VPre[N][320], BIGVPre[N][320];
signed main() {
int n, T, tot = 0; scanf("%d%d", &n, &T);
for (int i = 1, x; i < n; ++i) {
scanf("%d", &x);
tl[i] = tot + 1, tr[i] = tot + x;
for (int j = 1; j <= x; ++j) {
++tot;
scanf("%d%d", &nd[tot].first, &nd[tot].second);
}
}
for (int i = 1; i + 1 < n; ++i) {
vector< pair< pair<int, int>, int> > o;
pair<int, int> mn = {T + 1, 0};
for (int j = tl[i + 1]; j <= tr[i + 1]; ++j) {
o.push_back({nd[j], j});
mn = min(mn, {nd[j].second, j});
}
sort(o.begin(), o.end());
vector< pair<int, int> > to;
for (int j = tl[i]; j <= tr[i]; ++j) {
to.push_back({nd[j].second, j});
}
sort(to.begin(), to.end());
for (int i = (int)to.size() - 1, j = (int)o.size() - 1, tv = T + 1, wh = 0; ~i; --i) {
while (~j && o[j].first.first >= to[i].first) {
if (o[j].first.second < tv) {
tv = o[j].first.second, wh = o[j].second;
}
--j;
}
if (tv <= T) nxt[to[i].second] = wh, Vnxt[to[i].second] = tv - to[i].first;
else nxt[to[i].second] = mn.second, Vnxt[to[i].second] = T - to[i].first + mn.first;
}
}
for (int i = 1; i + 1 < n; ++i) {
vector< pair< pair<int, int>, int> > o;
pair<int, int> mx = {-1, 0};
for (int j = tl[i]; j <= tr[i]; ++j) {
o.push_back({{nd[j].second, nd[j].first}, j});
mx = max(mx, {nd[j].first, j});
}
sort(o.rbegin(), o.rend());
vector< pair<int, int> > to;
for (int j = tl[i + 1]; j <= tr[i + 1]; ++j) {
to.push_back({nd[j].first, j});
}
sort(to.rbegin(), to.rend());
for (int i = (int)to.size() - 1, j = (int)o.size() - 1, tv = -1, wh = 0; ~i; --i) {
while (~j && o[j].first.first <= to[i].first) {
if (o[j].first.second > tv) {
tv = o[j].first.second, wh = o[j].second;
}
--j;
}
if (tv >= 0) pre[to[i].second] = wh, Vpre[to[i].second] = to[i].first - tv;
else pre[to[i].second] = mx.second, Vpre[to[i].second] = to[i].first + T - mx.first;
}
}
int sz = sqrt(n) + 1;
for (int i = 1; i <= tot; ++i) {
Nxt[i][0] = i;
for (int j = 1; j <= sz; ++j) {
Nxt[i][j] = nxt[Nxt[i][j - 1]];
VNxt[i][j] = VNxt[i][j - 1] + Vnxt[Nxt[i][j - 1]];
}
BIGNxt[i][0] = i, BIGNxt[i][1] = Nxt[i][sz], BIGVNxt[i][1] = VNxt[i][sz];
}
for (int i = 1; i <= tot; ++i) {
for (int j = 2; j <= sz; ++j) {
BIGNxt[i][j] = BIGNxt[BIGNxt[i][j - 1]][1];
BIGVNxt[i][j] = BIGVNxt[i][j - 1] + BIGVNxt[BIGNxt[i][j - 1]][1];
}
}
for (int i = tot; i >= 1; --i) {
Pre[i][0] = i;
for (int j = 1; j <= sz; ++j) {
Pre[i][j] = pre[Pre[i][j - 1]];
VPre[i][j] = VPre[i][j - 1] + Vpre[Pre[i][j - 1]];
}
BIGPre[i][0] = i, BIGPre[i][1] = Pre[i][sz], BIGVPre[i][1] = VPre[i][sz];
}
for (int i = tot; i >= 1; --i) {
for (int j = 2; j <= sz; ++j) {
BIGPre[i][j] = BIGPre[BIGPre[i][j - 1]][1];
BIGVPre[i][j] = BIGVPre[i][j - 1] + BIGVPre[BIGPre[i][j - 1]][1];
}
}
int q; scanf("%d", &q);
map< pair<int, int>, long long> M;
while (q--) {
int l, r; scanf("%d%d", &l, &r);
if (M.count({l, r})) {
printf("%lld\n", M[{l, r}]);
continue;
}
long long res = 1e18;
int step = r - l - 1;
if (tr[l] - tl[l] < tr[r - 1] - tl[r - 1]) {
for (int i = tl[l]; i <= tr[l]; ++i) {
long long S = nd[i].second - nd[i].first;
int now = i;
S += VNxt[now][step % sz] + BIGVNxt[Nxt[now][step % sz]][step / sz];
res = min(res, S);
}
} else {
for (int i = tl[r - 1]; i <= tr[r - 1]; ++i) {
long long S = nd[i].second - nd[i].first;
int now = i;
S += VPre[now][step % sz] + BIGVPre[Pre[now][step % sz]][step / sz];
res = min(res, S);
}
}
printf("%lld\n", M[{l, r}] = res);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 6
Accepted
Test #1:
score: 6
Accepted
time: 39ms
memory: 16256kb
input:
2 1000000000 1 359893566 955414858 300000 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 ...
output:
595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 ...
result:
ok 300000 lines
Test #2:
score: 6
Accepted
time: 165ms
memory: 56252kb
input:
1384 702597566 1 93593482 288383752 1 483624997 516514674 1 217174776 378882844 1 381889032 694179867 1 143192510 343368096 1 20552425 654877612 1 34995000 223673833 1 86047336 507288111 1 58193455 564074888 1 543118270 579455813 1 42236607 257802041 1 244371899 634806939 1 173261583 634917538 1 245...
output:
152061320763 364193581975 101659406868 515885206553 273965799122 114948644944 78108129814 549857539900 166576516139 266640269522 36194858709 249707922175 12419530470 164111155048 607789899481 370597406072 100093371327 351888389540 72528927782 102643452509 26254171517 335577444460 126061743618 214062...
result:
ok 235294 lines
Test #3:
score: 6
Accepted
time: 241ms
memory: 62620kb
input:
2000 1000000000 1 251243678 591560449 1 994358883 999558886 1 322667352 514836853 1 538977337 603533309 1 249401760 363153703 1 104249966 416969473 1 103160611 933539967 1 300026318 706474995 1 637853185 969624295 1 612852422 686323121 1 890842468 964096005 1 127364216 656085651 1 565856726 79766828...
output:
804591361552 615732551026 616673957607 255388778080 246824759617 250452018635 3920166700 411598001493 191141891280 437294118321 839203030077 237616086785 395724762439 24493946848 261496520138 440921377339 879523097721 632991245786 629587780307 208737211703 514022647807 1235201434706 1239644739996 51...
result:
ok 300000 lines
Test #4:
score: 6
Accepted
time: 192ms
memory: 60312kb
input:
2000 702597566 1 234199188 250686543 1 187177414 485066634 1 187177414 584601655 1 187177414 584601655 1 472618361 588604455 1 619085294 688959957 1 619085294 661784753 1 218487968 619085294 1 619085294 642882128 1 260718505 642882128 1 599405824 642882128 1 609069701 699150927 1 609069701 702336507...
output:
1015957000190 680210081647 1242056863771 237697116977 4956604203 134440244240 408395990203 826647972707 545108473847 444013368984 460270771687 398420505294 739873557581 11886903864 782516902871 622468211441 160076268243 501156545599 68094738139 94221928973 846316719734 79740931643 53785803546 225843...
result:
ok 235294 lines
Test #5:
score: 6
Accepted
time: 287ms
memory: 68780kb
input:
2000 1000000000 1 68118109 979507132 1 314757325 876264736 1 314757325 876264736 1 67889892 777031974 1 482602023 935398234 1 262404428 482602023 1 339427172 407785939 1 387917774 407785939 1 326338674 387917774 1 470606759 626121253 1 479458047 617726881 1 497240208 588812091 1 27983270 580400619 1...
output:
1058954050793 1702912646711 1725931854894 847704535346 735930701349 957982043978 689218108574 1014888485270 1253926159509 5082345003 326705049247 551722648510 670690834144 772997962207 64935508636 584174883094 608974709405 109201814393 118555421706 191964764942 1461647551481 1384656185989 3160951073...
result:
ok 300000 lines
Test #6:
score: 6
Accepted
time: 293ms
memory: 68896kb
input:
2000 1000000000 1 0 318307689 1 221844870 244163115 1 22662231 115199498 1 74219194 235801812 1 2902409 380433342 1 168375604 683138088 1 11701354 403914303 1 168632344 336967772 1 71867910 459961453 1 152644723 678746968 1 600952102 753759227 1 623175732 906107261 1 630957186 647533253 1 283639625 ...
output:
1501632404753 1875961436118 1998999999999 230648011712 554515884946 1810332112158 1414199310905 464373332232 756701388718 1232357794991 594169425622 207539360194 1483145438544 601736569489 71450010792 12219064269 262122526111 724970057025 409281412575 949114178961 415313478501 59936197914 1681006375...
result:
ok 300000 lines
Test #7:
score: 6
Accepted
time: 217ms
memory: 55744kb
input:
1384 702597566 1 91563503 395118179 1 272969378 336163563 1 93593482 288383752 1 641844047 657030228 1 24174550 474302755 1 483624997 516514674 1 223419444 649396752 1 277453784 660782113 1 217174776 378882844 1 315641289 693735319 1 251526833 482373541 1 381889032 694179867 1 103075862 361871540 1 ...
output:
320794118590 115764966304 385279850930 10774072710 147778102717 240803564048 431539589908 290543953614 569466251816 22113396174 135588695866 228196386401 212848381901 88271083633 28719105225 481452322932 162201684606 579266864 322128177133 432606465712 97136428753 117887869786 563426915735 106522969...
result:
ok 235294 lines
Test #8:
score: 6
Accepted
time: 307ms
memory: 63932kb
input:
2000 1000000000 1 351194706 960606958 1 63449901 293827916 1 251243678 591560449 1 130608720 233558964 1 711982590 994358883 1 64238880 999558886 1 373830184 840719930 1 450224484 514836853 1 322667352 903701287 1 258721955 861235059 1 229925996 538977337 1 546018536 603533309 1 656999554 814169249 ...
output:
917540572198 186307563686 364348161985 671810713222 368143529864 751354141909 398419559458 596576418307 19619304924 8364848267 507434978682 500879172382 200335632587 667972088008 955278113757 1106615668453 1200860863382 717853707983 277931060293 664485590193 152444963228 296133779011 563469393776 33...
result:
ok 300000 lines
Subtask #2:
score: 8
Accepted
Dependency #1:
100%
Accepted
Test #9:
score: 8
Accepted
time: 194ms
memory: 51412kb
input:
401 1000000000 5 220371372 336231535 896142843 932588962 50422118 103225530 657147900 709375447 431588410 552424272 5 640842473 746383340 810978611 953826580 275021460 368433859 462990882 571587967 58700188 103678512 5 671571439 779339183 471320804 598246091 2249112 160803576 865566830 948052278 222...
output:
58015892805 52459605973 79838077191 17461955998 14664444928 41031111167 44824194366 23803651939 61071315346 12828269099 37025588232 22632816685 3687297996 23869715688 53256120071 26611104179 27660022154 43657337431 43421554887 33052332827 444354092 2245599618 3207243655 52828823244 29244408625 18672...
result:
ok 300000 lines
Test #10:
score: 8
Accepted
time: 197ms
memory: 54196kb
input:
401 1000000000 5 530204134 539929589 13144227 22318244 346386878 374628522 243477806 318307686 24550570 176767937 5 318307686 346386878 539929589 592275682 176767937 243477806 22318244 24550570 374628522 530204134 5 24550570 176767937 243477806 318307686 13144227 22318244 530204134 539929589 3463868...
output:
37011406343 19273967996 39579131455 169618941 18197860585 17273967996 13296451783 27169618941 3415507745 1333242651 26169618941 23517611345 8273967996 21517611345 1009174017 12526785362 27131150716 10333242651 20230333579 14056320836 22011406343 17296451783 3131150716 18011406343 20517611345 4352310...
result:
ok 300000 lines
Test #11:
score: 8
Accepted
time: 195ms
memory: 49868kb
input:
401 1000000000 4 848784509 854990717 82539068 388749940 876647585 917845619 434252359 592889838 5 917845619 963887179 388749940 434252359 854990717 876647585 592889838 848784509 79661360 82539068 4 434252359 592889838 876647585 917845619 82539068 388749940 848784509 854990717 5 79661360 82539068 854...
output:
39069061110 33115102670 49881348111 37483593260 28483593260 20529095679 9838184259 28108896462 16575137239 27529095679 6838184259 40027863076 11108896462 30062854902 23881348111 15370997341 27069061110 11529634820 13370997341 4069061110 38115102670 28575137239 16006206208 324955781 10529095679 23115...
result:
ok 300000 lines
Test #12:
score: 8
Accepted
time: 188ms
memory: 50476kb
input:
401 1000000000 4 494963273 820408129 83880819 120938110 842771080 916665396 126044810 290806569 5 64152734 83880819 820408129 842771080 290806569 494963273 120938110 126044810 916665396 991266558 4 126044810 290806569 83880819 120938110 842771080 916665396 494963273 820408129 5 820408129 842771080 6...
output:
44795843296 23631081537 49907385739 29907385739 32283273730 2448035489 23056785376 14674555144 35056785376 3206925750 22206925750 11674555144 6448035489 13470398440 2061892076 13448035489 26061892076 674555144 1037057291 19907385739 5206925750 8631081537 2283273730 631081537 15470398440 42795843296 ...
result:
ok 300000 lines
Test #13:
score: 8
Accepted
time: 256ms
memory: 56732kb
input:
601 1000000000 1 1875338 891632702 5 25665841 55060068 659511452 708955140 481273243 580847341 711209775 812323888 232189221 411128242 1 1875338 891632702 5 481273243 580847341 711209775 812323888 25665841 55060068 659511452 708955140 232189221 411128242 1 1875338 891632702 5 232189221 411128242 711...
output:
132889757364 545053184730 599053184730 349053184730 367180422927 378889757364 181053184730 214889757364 25053184730 119343850293 51180422927 165180422927 293180422927 367180422927 569343850293 449343850293 445053184730 115180422927 341053184730 218889757364 189053184730 107180422927 105343850293 272...
result:
ok 300000 lines
Test #14:
score: 8
Accepted
time: 40ms
memory: 18104kb
input:
2 1000000000 5 562116384 862271817 248809078 388152605 82837362 988267899 133736084 529736108 468265107 868556204 300000 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2...
output:
139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 139343527 ...
result:
ok 300000 lines
Test #15:
score: 8
Accepted
time: 255ms
memory: 62648kb
input:
1343 1000000000 1 96816138 421462953 1 69729216 475015093 1 283255283 678004962 1 278183246 599578883 1 286424703 774388986 1 406934648 652124894 2 248195189 472085327 391961014 813907536 1 501741026 753654366 2 245060443 436672588 574556343 749900723 2 68441219 101302096 259996265 760303051 1 72484...
output:
413507395543 186533587467 765911134712 208868482018 750404829806 371009807668 207418964932 82428131468 227003029730 376762196348 758844081281 297910927314 116614837962 284886215974 115872729613 739953553340 22687331071 38063290673 8397417592 12376556003 111406561501 687952964749 12358874327 12241911...
result:
ok 300000 lines
Test #16:
score: 8
Accepted
time: 200ms
memory: 63828kb
input:
688 1000000000 3 53194187 264960020 92885755 475277088 211238915 509880505 3 6401156 232019227 469999920 577170462 554975681 644492768 2 60786527 521736047 379366223 840946783 5 419561 60298271 65502307 288235251 144008397 577008215 394990239 683091070 590837826 726885528 5 34176214 267349290 223828...
output:
68635449756 17399644966 829516162 145106947024 213624911279 172079871542 83795296584 86980633523 92389124653 242968110321 130973680158 126653516847 55386113669 195171481102 213220346364 18461195442 46294965858 108144706452 39788445054 253134167975 234247708477 234680737798 241401743091 222796922152 ...
result:
ok 300000 lines
Test #17:
score: 8
Accepted
time: 282ms
memory: 62832kb
input:
1343 1000000000 1 109453710 560177810 1 661213206 913808522 1 96816138 421462953 1 193580993 240625496 1 418456250 930846898 1 69729216 475015093 2 36690281 582664217 176453344 242348112 1 283255283 678004962 2 62188679 608199380 90371766 461894091 2 278183246 599578883 255503843 861802590 1 5501148...
output:
755163082049 346535864122 512232407863 978898058885 290552425232 97892014092 196290407373 506354231170 334498014469 284757164242 115371682131 62648300672 346925420676 384891531485 477119667452 63592230021 727135372228 847646760248 640469173746 892644815785 815536555442 725960919828 92996821933 41864...
result:
ok 300000 lines
Test #18:
score: 8
Accepted
time: 217ms
memory: 60016kb
input:
688 1000000000 3 423977827 524383260 470022669 702490366 226130327 278353378 3 297592874 610688336 79889464 986165439 132108640 133164848 2 92885755 509880505 53194187 211238915 5 264960020 475277088 262659468 993117970 127768091 586472847 838692371 844797839 414832478 764033423 5 132569548 29971860...
output:
326730918498 121718776974 78578695164 28500146125 259901663572 262316540686 278578980688 83091064134 24828685743 280173281064 305172418316 69583809374 329513749863 295544205317 40964341057 32266429473 20655624467 142566212889 70252960623 218573155987 10170931375 250505042674 221374816018 41528906065...
result:
ok 300000 lines
Subtask #3:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #19:
score: 0
Time Limit Exceeded
input:
78947 750547470 1 163829932 170313421 1 34754818 519560348 1 93869768 456876196 1 202438570 204178463 1 98944286 525531897 1 179303298 240997860 1 134306886 372058731 1 187793519 715404428 1 126696643 473999960 1 394050514 679516860 1 286238164 460635018 1 489600305 744982147 1 157363526 311748138 1...
output:
6205466879608 20259844164215 41141586068816 37267587927279 39826818642517 4093515572405 43823341232727 17711628858751 28238993418477 40884190272133 26104208475807 13508775709102 41012054001186 35352426273830 461627182416 19671175078208 21652186528249 28225447363916 41145479364020 41071413078809 3081...
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%
Subtask #5:
score: 0
Memory Limit Exceeded
Test #39:
score: 0
Memory Limit Exceeded
input:
301 1000000000 300 863578477 865166395 261293731 262628986 290161866 292035987 31029640 32135494 288138979 289416854 321254857 322352244 163393949 166291828 897880953 899050317 840019366 842900569 100947276 102350870 520716771 522094941 820182602 822928836 766708508 769688128 727827782 728874133 740...
output:
996840913 213467673 996840913 350088722 393643222 660161043 23398481 83378757 386772057 550058707 116797789 66795163 230046137 430022213 50052816 646976316 223372288 443414533 153481147 43516132 10186037 656745708 93473524 443593864 613442576 306857640 606706973 613462088 456791451 276831487 1034634...
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%