QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#137080 | #1140. Distributing Candies | bashkort# | 8 | 567ms | 49296kb | C++20 | 8.8kb | 2023-08-09 19:18:34 | 2024-07-04 01:28:19 |
Judging History
answer
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll infLL = 3e18;
struct Info {
pair<ll, int> mx{-infLL, 0}, mn{infLL, 0};
ll mxans = -infLL, mnans = infLL;
Info() = default;
Info(int i, ll x) : mx(x, i), mn(x, i) {}
};
Info operator+(const Info &l, const Info &r) {
Info res{};
res.mx = max(l.mx, r.mx);
res.mn = min(l.mn, r.mn);
res.mxans = max({l.mxans, r.mxans, r.mx.first - l.mn.first});
res.mnans = min({l.mnans, r.mnans, r.mn.first - l.mx.first});
return res;
}
namespace SegmentTree {
vector<Info> t;
vector<ll> tag;
int sz = 1;
void apply(int x, ll tg) {
tag[x] += tg;
t[x].mx.first += tg, t[x].mn.first += tg;
}
void push(int x) {
if (tag[x]) {
apply(x << 1, tag[x]);
apply(x << 1 | 1, tag[x]);
tag[x] = 0;
}
}
void pull(int x) {
t[x] = t[x << 1] + t[x << 1 | 1];
}
void init(int n) {
sz = 1 << __lg(n) + !!(n & (n - 1));
t.resize(sz << 1), tag.assign(sz << 1, 0);
for (int i = 0; i < n; ++i) {
t[i + sz] = Info(i, 0);
}
for (int i = sz - 1; i > 0; --i) {
pull(i);
}
}
void rangeAdd(int l, int r, int tg, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) {
return;
}
if (l <= lx && rx <= r) {
return apply(x, tg);
}
int mid = lx + rx >> 1;
push(x);
rangeAdd(l, r, tg, x << 1, lx, mid);
rangeAdd(l, r, tg, x << 1 | 1, mid, rx);
pull(x);
}
ll query(int i, int x = 1, int lx = 0, int rx = sz) {
while (lx + 1 < rx) {
push(x);
int mid = lx + rx >> 1;
if (i < mid) {
rx = mid;
x = x << 1;
} else {
lx = mid;
x = x << 1 | 1;
}
}
return t[x].mx.first;
}
pair<ll, int> findMin(int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) {
return {infLL, -1};
}
if (l <= lx && rx <= r) {
return t[x].mn;
}
int mid = lx + rx >> 1;
push(x);
return min(findMin(l, r, x << 1, lx, mid), findMin(l, r, x << 1 | 1, mid, rx));
}
pair<ll, int> findMax(int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) {
return {-infLL, -1};
}
if (l <= lx && rx <= r) {
return t[x].mx;
}
int mid = lx + rx >> 1;
push(x);
return max(findMax(l, r, x << 1, lx, mid), findMax(l, r, x << 1 | 1, mid, rx));
}
pair<ll, int> queryMax(ll aim, int x, int lx, int rx) {
assert(t[x].mx.first >= aim);
if (lx + 1 == rx) {
return t[x].mx;
}
int mid = lx + rx >> 1;
push(x);
if (t[x << 1 | 1].mx.first >= aim) {
return queryMax(aim, x << 1 | 1, mid, rx);
} else {
return queryMax(aim, x << 1, lx, mid);
}
}
pair<ll, int> queryMin(ll aim, int x, int lx, int rx) {
assert(t[x].mn.first <= aim);
if (lx + 1 == rx) {
return t[x].mn;
}
int mid = lx + rx >> 1;
push(x);
if (t[x << 1 | 1].mn.first <= aim) {
return queryMin(aim, x << 1 | 1, mid, rx);
} else {
return queryMin(aim, x << 1, lx, mid);
}
}
pair<ll, int> askMin(ll aim, int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r || t[x].mn.first > aim) {
return {infLL, -1};
}
if (lx + 1 == rx) {
return t[x].mn;
}
int mid = lx + rx >> 1;
push(x);
auto res = askMin(aim, l, r, x << 1 | 1, mid, rx);
if (res.second == -1) {
res = askMin(aim, l, r, x << 1, lx, mid);
}
return res;
}
pair<ll, int> askMax(ll aim, int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r || t[x].mx.first < aim) {
return {-infLL, -1};
}
if (lx + 1 == rx) {
return t[x].mx;
}
int mid = lx + rx >> 1;
push(x);
auto res = askMax(aim, l, r, x << 1 | 1, mid, rx);
if (res.second == -1) {
res = askMax(aim, l, r, x << 1, lx, mid);
}
return res;
}
pair<int, int> findSegmentBiggerThanC(int C, int x = 1, int lx = 0, int rx = sz) {
pair<int, int> s{-1, -1};
while (lx + 1 < rx && t[x].mxans >= C) {
int mid = lx + rx >> 1;
push(x);
if (t[x << 1 | 1].mx.first - t[x << 1].mn.first >= C) {
int R = queryMax(C + t[x << 1].mn.first, x << 1 | 1, mid, rx).second;
s = max(s, {R, t[x << 1].mn.second});
}
if (t[x << 1 | 1].mxans >= C) {
x = x << 1 | 1;
lx = mid;
} else {
x = x << 1;
rx = mid;
}
}
return s;
}
pair<int, int> findSegmentSmallerThanC(int C, int x = 1, int lx = 0, int rx = sz) {
assert(C <= 0);
pair<int, int> s{-1, -1};
while (lx + 1 < rx && t[x].mnans <= C) {
int mid = lx + rx >> 1;
push(x);
if (t[x << 1 | 1].mn.first - t[x << 1].mx.first <= C) {
int R = queryMin(C + t[x << 1].mx.first, x << 1 | 1, mid, rx).second;
s = max(s, {R, t[x << 1].mx.second});
}
if (t[x << 1 | 1].mnans <= C) {
x = x << 1 | 1;
lx = mid;
} else {
x = x << 1;
rx = mid;
}
}
return s;
}
};
void massert(bool f) {
while (!f) {
cout << "why" << endl;
}
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> L,
std::vector<int> R, std::vector<int> V) {
int n = c.size(), q = size(L);
std::vector<int> answers(n);
vector<vector<int>> updates(n + 1);
for (int i = 0; i < q; ++i) {
updates[L[i]].push_back(i);
updates[R[i] + 1].push_back(~i);
}
SegmentTree::init(q + 1);
ll sumAll = 0;
for (int i = 0; i < n; ++i) {
for (int j : updates[i]) {
if (j >= 0) {
sumAll += V[j];
SegmentTree::rangeAdd(j + 1, q + 1, V[j]);
} else {
j = ~j;
sumAll -= V[j];
SegmentTree::rangeAdd(j + 1, q + 1, -V[j]);
}
}
// int rbig = q + 1, lbig = -1;
// for (rbig -= 1; rbig >= 0; --rbig) {
// auto [x, y] = SegmentTree::findMin(0, rbig + 1);
// ll xx = SegmentTree::query(rbig);
// if (xx - x >= c[i]) {
// lbig = y;
// break;
// }
// }
// int rsmall = q + 1, lsmall = -1;
// for (rsmall -= 1; rsmall >= 0; --rsmall) {
// auto [x, y] = SegmentTree::findMax(0, rsmall + 1);
// ll xx = SegmentTree::query(rsmall);
// if (xx - x <= -c[i]) {
// lsmall = y;
// break;
// }
// }
auto [rbig, lbig] = SegmentTree::findSegmentBiggerThanC(c[i]);
auto [rsmall, lsmall] = SegmentTree::findSegmentSmallerThanC(-c[i]);
if (rsmall != -1) {
ll pr = SegmentTree::query(rsmall);
lsmall = SegmentTree::askMax(pr + c[i], lsmall, rsmall).second;
assert(SegmentTree::query(rsmall) - SegmentTree::query(lsmall) <= -c[i]);
rsmall = SegmentTree::findMin(lsmall, rsmall + 1).second;
}
if (rbig != -1) {
ll pr = SegmentTree::query(rbig);
lbig = SegmentTree::askMin(pr - c[i], lbig, rbig + 1).second;
assert(SegmentTree::query(rbig) - SegmentTree::query(lbig) >= c[i]);
rbig = SegmentTree::findMax(lbig, rbig + 1).second;
}
// cout << lbig << " " << rbig << endl;
// cout << lsmall << " " << rsmall << endl;
rsmall = max(rsmall, SegmentTree::findMin(0, q + 1).second);
if (rsmall <= rbig) {
ll got = c[i] + sumAll - SegmentTree::query(rbig);
massert(got >= 0 && got <= c[i]);
answers[i] = got;
} else {
ll got = sumAll - SegmentTree::query(rsmall);
assert(got >= 0 && got <= c[i]);
answers[i] = got;
}
// cout << i << endl;
}
return answers;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 3
Accepted
time: 1ms
memory: 3760kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 8 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 8 0 7 1 0 7 1 0 7 300000000 0 7 994967293 0 7 1 0 7 1000000000 0 7 1000000000 0 7 1000000000
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
result:
ok 3 lines
Test #2:
score: 3
Accepted
time: 1ms
memory: 3712kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 10 283 43634 101056 10340 6009 5133 30 2 3677888 210 10 1 8 26416 2 7 -51219 2 4 17793 3 7 75426 3 7 22307 1 1 60258 3 7 -29824 0 8 24839 2 8 -60304 0 1 -26411
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 0 17223 0 0 0 0 0 0 0 0
result:
ok 3 lines
Test #3:
score: 3
Accepted
time: 2ms
memory: 3836kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 10 5895610 429664 3124 17993 758457 101345 5102817 1127952 59 81146 2000 6 7 44356 5 7 77812 1 4 -41353 1 7 -81697 2 5 -26607 4 9 84461 4 7 -44947 1 6 42622 3 5 -99951 0 1 -77687 2 6 52280 5 9 5073 1 9 67601 6 8 -6669 0 6 42368 4 6 22221 1 3 48306 3 6 -23492 ...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 1520966 0 0 0 20922 69708 15240 91107 0 78774
result:
ok 3 lines
Test #4:
score: 0
Wrong Answer
time: 2ms
memory: 4124kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 100 1281 616650 26929 344 1231 263 183010 1 1 46 144770 17 1735 9520 7 1 39535 8307 2 5 32940 498570 644480 10107 1645 21 443708 4 28177 2857127 2 1 1350 17506 5 36 1985 42978 24123 73 114 230034 5561405 11263 9754875 4671 44 3 8982 299 452 15 2619 9 7 6259 8...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 0 19101 26929 344 1231 263 14968 1 1 46 40142 17 1735 9520 7 1 31323 8307 2 5 9807 87522 84360 5468 1645 21 93788 4 28177 158016 2 1 1350 17506 5 36 1985 42978 24123 73 114 230034 248401 11263 290026 4671 44 3 8982 299 452 15 2619 9 7 6259 68282 104034 177...
result:
wrong answer 3rd lines differ - expected: '0 19101 26929 344 1231 263 149...09117 11754 62260 87849 0 31650', found: '0 19101 26929 344 1231 263 149...09117 11754 62260 87849 0 31650'
Subtask #2:
score: 8
Accepted
Test #6:
score: 8
Accepted
time: 558ms
memory: 49196kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 200000 11408901 370732653 37843 28 53693 15782410 103 297546 1112427 170319071 26 1 6172 11614171 431 884673599 1 3 50085 154 57 28200 145886 898969 74758 72 845768 6 69787 11 31012465 55362 253 2363145 47186217 1103 19622 594 7867 1 4299 28130 48 4689582 12 ...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 87153 87153 37843 28 53693 406667 103 297546 766665 971468 26 1 6172 1257294 431 1536484 1 3 50085 154 57 28200 145886 898969 74758 72 845768 6 69787 11 3043755 55362 253 2363145 3459533 1103 19622 594 7867 1 4299 28130 48 4317336 12 431 123 4531465 4806 3...
result:
ok 3 lines
Test #7:
score: 8
Accepted
time: 555ms
memory: 49296kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 200000 36 596 2 302 1 22 7381829 295 411 221 267845 2822 635 22 45033 2 3 24 15 1 585 2832326 80 499271 110 192 6185 1752 302907 52967 3 3423576 5373 63 2196 35 113 1209 303 12 89 4572 4 13274 5867 10158 33467 3128 776575 59189 23 11698 637 3 330 1 1 18 3534 ...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 36 295 2 302 1 22 529 295 411 221 771 931 635 22 997 2 3 24 15 1 585 1803 80 1928 110 192 2072 1752 2113 2222 3 2336 2351 63 2196 35 113 1209 303 12 89 3734 4 3736 3736 3931 4234 3128 4408 4562 23 5099 637 3 330 1 1 18 3534 2589 6286 6406 1042 6596 1 6685 ...
result:
ok 3 lines
Test #8:
score: 8
Accepted
time: 567ms
memory: 49260kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 200000 423807 103641 5 2833 134 4447875 716134 10 300 7393 6 801 5256389 2604 521049 1670294 35 12249 12 29904 691656 393760 22 409 2 956844 8846653 19 1926 769 36 3577 55 524387 154184 165995 753 3709 29260 41947 89 27779 5115776 1 63 1 374 72 1788 41555 274...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 11 25 5 52 52 53 53 10 66 69 6 86 105 114 123 145 35 155 12 177 177 178 22 229 2 229 239 19 264 280 36 295 55 298 313 328 337 356 375 385 89 388 425 1 63 1 374 72 515 525 525 531 3 5 7 564 573 584 61 631 644 648 8 14 664 316 558 676 686 705 705 736 79 747 ...
result:
ok 3 lines
Subtask #3:
score: 0
Wrong Answer
Test #9:
score: 27
Accepted
time: 0ms
memory: 3760kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 3 lines
Test #10:
score: 0
Wrong Answer
time: 312ms
memory: 38988kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 2000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 1049802 936230 884511 204101 406074 550834 961642 2076245 1975297 2101513 2254310 1990108 1398298 1917785 2864344 2475931 2270774 401698970 402327667 402506418 401068637 399388438 398687119 398672863 397137012 397262070 396255448 395961553 394643443 394646...
result:
wrong answer 3rd lines differ - expected: '1049802 936230 884511 204101 4...877 441728121 110945553 1330162', found: '1049802 936230 884511 204101 4...877 441728121 110945553 1330162'
Subtask #4:
score: 0
Wrong Answer
Test #16:
score: 29
Accepted
time: 0ms
memory: 3716kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 10 11 440 51 41 11 1 3 108 148 14 10 0 9 60 0 9 -9 0 9 -30 0 9 41 0 9 82 0 9 69 0 9 -79 0 9 -39 0 9 72 0 9 41
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 11 208 51 41 11 1 3 108 143 14
result:
ok 3 lines
Test #17:
score: 29
Accepted
time: 1ms
memory: 3768kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 1000 6 129 1 3 18 414 46 7 33 2 29 3 395 143 120 62 343 102 568 40 49 1 37 7 31 66 12 1 330 4 3 10 3 216 2 375 15 786 1 156 243 411 519 14 13 13 667 2 382 294 1 556 53 2 368 1 32 5 201 13 376 369 91 11 14 5 584 65 3 443 1 989 889 22 8 177 140 7 481 6 371 142 ...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 68 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
ok 3 lines
Test #18:
score: 0
Wrong Answer
time: 133ms
memory: 38420kb
input:
lrts0z0ktpqc670i0etyqgyb45dt1dysq5ap2mzg 2000 4207825 17466917 11 20 10489 1278831 48720 43780703 37223309 28500011 76204785 631 321 1650 263304936 1382 1900 1 225756109 43424483 21143 218062355 851196097 633450 141629084 11494 1 19 12133 5908567 7 26138 1131 152662321 18 787906 312 11463 393 109417...
output:
4lpv73iykswp9e3nppi3jw2qggm5dfz29yy9nzpq OK 780706 1955314 11 20 10489 659178 48720 1955314 1955314 1955314 1955314 631 321 1650 1955314 1382 1900 1 1955314 1955314 21143 1955314 1955314 633450 1955314 11494 1 19 12133 1691591 7 26138 1131 1955314 18 659178 312 11463 393 659178 659178 1180 1955314 5...
result:
wrong answer 3rd lines differ - expected: '780706 1955314 11 20 10489 659...198192 1955314 832 19064 557026', found: '780706 1955314 11 20 10489 659...198192 1955314 832 19064 557026'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%