QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863715 | #2471. Minimizing Haybales | rxlfd314 | 30 | 3ms | 3968kb | C++17 | 2.6kb | 2025-01-19 21:34:52 | 2025-01-19 21:34:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ari2 = array<int, 2>;
template <class T> using vt = vector<T>;
#define all(x) begin(x), end(x)
#define size(x) (int((x).size()))
#define REP(a,b,c,d) for(int a=(b);(d)>0?a<=(c):a>=(c);a+=(d))
#define FOR(a,b,c) REP(a,b,c,1)
#define ROF(a,b,c) REP(a,b,c,-1)
constexpr int mxN = 100005;
int N, K;
struct ST {
int lz[mxN], st[mxN];
#define lc i << 1
#define rc lc | 1
void push(const int i) {
if (!lz[i])
return;
st[lc] += lz[i];
st[rc] += lz[i];
lz[lc] += lz[i];
lz[rc] += lz[i];
lz[i] = 0;
}
void radd(const int ql, const int qr, const int v, const int i = 1, const int tl = 0, const int tr = N-1) {
if (tl > qr || tr < ql)
return;
if (ql <= tl && tr <= qr)
st[i] += v, lz[i] += v;
else {
push(i);
const int tm = tl + tr >> 1;
radd(ql, qr, v, lc, tl, tm);
radd(ql, qr, v, rc, tm+1, tr);
st[i] = min(st[lc], st[rc]);
}
}
int walk() {
int tl = 0, tr = N-1, i = 1;
while (tl < tr) {
push(i);
const int tm = tl + tr >> 1;
if (!st[lc])
tr = tm, i = lc;
else
tl = tm + 1, i = rc;
}
assert(st[i] == 0);
return tl;
}
#undef lc
#undef rc
} st;
struct BIT {
vt<int> fen;
BIT(const int n) : fen(n+5) {}
void update(int i, const int v) {
for (++i; i < size(fen); i += i & -i)
fen[i] += v;
}
int query(int i) {
int ret = 0;
for (++i; i > 0; i -= i & -i)
ret += fen[i];
return ret;
}
int query(const int l, const int r) {
return query(r) - query(l-1);
}
};
signed main() {
#ifndef DEBUG
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cin >> N >> K;
vt<int> A(N), ord(N);
for (auto &i : A)
cin >> i;
iota(all(ord), 0);
sort(all(ord), [&](const int &a, const int &b) {
return A[a] < A[b];
});
vt<int> pos(N), cmp(N);
FOR(i, 0, N-1)
pos[ord[i]] = i, cmp[i] = A[ord[i]];
BIT fen(N);
FOR(i, 0, N-1) {
const int l = lower_bound(all(cmp), A[i]-K) - begin(cmp);
const int r = upper_bound(all(cmp), A[i]+K) - begin(cmp) - 1;
st.radd(pos[i], pos[i], i - fen.query(l, r));
fen.update(pos[i], 1);
}
FOR(_, 1, N) {
const int i = st.walk();
cout << cmp[i] << '\n';
const int l = lower_bound(all(cmp), cmp[i]-K) - begin(cmp);
const int r = upper_bound(all(cmp), cmp[i]+K) - begin(cmp) - 1;
st.radd(0, l-1, -1);
st.radd(r+1, N-1, -1);
st.radd(i, i, INT_MAX);
}
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1ms
memory: 3712kb
input:
5 3 7 7 3 6 2
output:
6 7 7 2 3
result:
ok 5 lines
Test #2:
score: 5
Accepted
time: 1ms
memory: 3712kb
input:
100 500000276 870008509 927469041 935406318 944203501 797721875 788862150 829670356 907559578 718374630 509907701 522634718 660868664 786521954 514117566 807678427 549891686 591859900 641495303 610385740 767798197 477652779 748746451 738282716 590882275 255562601 627012345 570543971 246778294 546323...
output:
446452076 450190489 457330701 460651686 460776808 461454886 474154009 475530095 477466533 477652779 478979648 491341754 509907701 511989080 514117566 514156865 522634718 536476244 546016428 546323236 549891686 551547512 555501463 556127591 559365249 570543971 590882275 591859900 610385740 626472443 ...
result:
ok 100 lines
Test #3:
score: 5
Accepted
time: 3ms
memory: 3968kb
input:
5000 500000092 876873178 532909908 691231781 552601748 634859059 954722077 566913744 669033129 599030880 854902273 508962949 541446119 849630596 521171484 854895876 694002393 893301389 816678419 774687296 913625357 872369003 637310311 838609600 869273073 694873732 670936633 602033085 532798697 82220...
output:
474057275 474118290 474165237 474211691 474286361 474328081 474806475 474939204 475030713 475089865 475167222 475253362 475395832 475445731 475500181 475780488 475963620 476531278 476579992 476704749 476773812 476786185 477023255 477046377 477068547 477123436 477279964 477308036 477361210 477366468 ...
result:
ok 5000 lines
Test #4:
score: 5
Accepted
time: 3ms
memory: 3968kb
input:
5000 500000051 749316321 730453003 913575059 862976693 686029006 822819868 660394491 970141114 743840810 646769530 764878371 554715136 639686703 578137696 723949039 969360006 634776293 889588783 497384805 752832200 937051443 660927236 715318931 794792652 502702405 764452841 678276888 843647569 56528...
output:
474267507 474271404 474278271 474300880 474334065 474386850 474416640 474436878 474465440 474489938 474555946 474575746 474727262 474825095 474831863 475084999 475128503 475351079 475611854 475669867 475766168 475908282 475917084 475943330 475965005 476063803 476255945 476334073 476441476 476539638 ...
result:
ok 5000 lines
Test #5:
score: 5
Accepted
time: 2ms
memory: 3968kb
input:
5000 499999903 702811695 667164672 977793322 975303193 824282596 853496670 576220647 628576954 836044525 737259702 602176636 559657696 767313699 565325752 556326779 906531829 849279179 657326131 725965296 850271786 619939890 750919964 677011765 519852573 505008517 583852726 590379643 993871739 78868...
output:
493890731 494002838 494022450 494028649 494131900 494369938 494422316 494447334 494485029 494531231 494689152 494752080 494856395 494905365 494927294 495106280 495228276 495289896 495384277 495388810 495660130 495918240 496006866 496055693 496236012 496328240 496370388 496399098 496409924 496425206 ...
result:
ok 5000 lines
Test #6:
score: 5
Accepted
time: 2ms
memory: 3840kb
input:
5000 499999659 864999430 633751501 669999080 642742789 885259094 670121204 556089694 777210903 600413470 672705529 943764014 863356225 831596424 625421162 580776758 662988069 671341336 601389842 729095269 956874925 861105302 775856455 709104580 619579778 891488276 801782435 776726417 640060238 67894...
output:
481832753 481851893 481956990 481968365 482020931 482045243 482118840 482138212 482138413 482250207 482751132 482924209 482968904 483000625 483058415 483154289 483352788 483397770 483601861 483630428 483684825 484016979 484129639 484237319 484262016 484473250 484519653 484586352 484602372 484667937 ...
result:
ok 5000 lines
Test #7:
score: 0
Runtime Error
input:
100000 499999689 522308516 569502884 647941518 853228330 654821520 838881796 774403511 736611222 842053880 606723975 985926270 876060090 622595727 721250785 565765709 901002668 885374363 550172010 575300705 622879464 613382203 748567965 834846154 522172750 905759483 865754687 766509821 877494171 688...
output:
result:
Test #8:
score: 0
Runtime Error
input:
100000 500000173 990775780 688545127 646804699 788353753 676568883 868340410 671589000 953696111 686961724 632973188 690696234 715740373 606233240 986146202 878874984 876162656 805622883 872090728 943178960 549978890 534761731 920736091 922133710 550450440 521883016 957548511 618568746 900617604 684...
output:
result:
Test #9:
score: 0
Runtime Error
input:
100000 499999716 961588881 622373216 829499649 652387956 977516383 700720874 645867135 639942519 754115351 921339422 911466113 625208629 689630195 515383601 988113721 933210955 838954714 861832012 706977319 760273274 888479052 736553881 755517402 878593223 504701750 609043772 808564727 832346308 560...
output:
result:
Test #10:
score: 0
Runtime Error
input:
100000 499999965 586906664 785816875 559096748 568160809 770057713 850543684 756484838 907031258 527090020 522959969 641247309 949869480 758386508 932086404 515720675 576910674 593776180 949716385 627436089 821828286 824335653 731598966 718315347 948821073 835614578 811476996 933352148 966236635 539...
output:
result:
Test #11:
score: 0
Runtime Error
input:
100000 500000064 800447926 719877542 990685270 969136566 682887012 838675215 864935401 714661764 530927165 679393693 956117153 702480839 626387388 989915213 928788904 839482826 824747011 589575242 623294421 993905764 884185517 621887958 905767207 662279311 915752607 706993074 719873247 517440314 866...
output:
result:
Test #12:
score: 0
Runtime Error
input:
100000 500000060 589831436 926568471 502623185 947521508 706193200 557638536 997440264 787829453 918788862 565820605 533958147 520184188 591412798 858622437 566507151 505585645 951723905 663551950 693693740 835296306 871871823 597202943 779246896 557471338 608936726 730216642 745404224 962757882 534...
output:
result:
Test #13:
score: 0
Runtime Error
input:
100000 499999703 998339676 857775038 938914859 925714046 948420452 853099930 702807605 955309563 507771407 859625378 599746541 526518608 920399831 749583607 933937091 505693947 944593139 528674523 690902369 714593203 991924301 646559431 532682045 661736933 567787144 857175318 885277238 521134842 982...
output:
result:
Test #14:
score: 0
Runtime Error
input:
100000 499999711 695301689 560039379 672448134 824685585 739913206 539784434 829510060 577250390 747928670 789519051 700730081 983775810 680977333 944727662 719765522 500161311 542770816 777742933 893127614 559516227 835046023 984518913 596526140 984853995 970871415 705149090 603278546 582372794 636...
output:
result:
Test #15:
score: 0
Runtime Error
input:
100000 500000138 823666089 992527097 695616352 557997486 997702307 835297060 932083132 545631833 560499050 593683633 907385849 772551018 586965709 537600892 927874503 501188040 864814223 882492756 981357762 675430235 913880748 585118641 638762120 740292839 793367806 941851372 581443270 626820208 647...
output:
result:
Test #16:
score: 0
Runtime Error
input:
100000 499999705 865090747 621645284 675347345 579476019 926856164 807987057 743784877 648624896 903529805 506002020 730071335 825738820 851461903 952937940 976585398 862928172 557065763 852749968 724179644 776502490 808082334 879988163 576224601 678164425 510668021 517980414 736920241 608815772 707...
output:
result:
Test #17:
score: 0
Runtime Error
input:
100000 499999940 561770745 700083067 772417184 630455486 566589619 527373256 680269837 688334775 761016347 579979332 685620463 991340939 584822162 805408134 987166410 662089268 507799971 961997465 993145421 832196907 865751924 707107786 895138059 606455281 545671384 509993873 626782787 762183648 927...
output:
result:
Test #18:
score: 0
Runtime Error
input:
100000 499999942 776408462 987292704 934126535 949242623 825796635 551007297 855992436 600237028 622519904 803204712 913549773 618035582 974580612 887892434 842260331 627314120 825838659 674502706 620244094 993146835 852442975 998472146 882123654 822630659 984406455 654646083 568954801 826585448 838...
output:
result:
Test #19:
score: 0
Runtime Error
input:
100000 500000301 631259614 927632295 867812128 865876610 584436269 797968706 898096753 768508948 691818764 843399208 842839446 754773737 780882621 984461159 731777715 950749057 913447489 738723219 645875959 968509216 690880878 540952366 722322777 651452633 921593682 884544922 657336539 553175341 938...
output:
result:
Test #20:
score: 0
Runtime Error
input:
100000 499999684 846689960 872461622 903406427 821748465 508090323 670625273 962758034 722267965 825104333 996701168 997229041 977256026 950729017 532350029 525131720 529706299 849983259 848516122 943100355 746778579 857884167 536055778 960947123 759580048 962328565 722242046 629455900 779438545 619...