#include "candies.h"
#include "bits/stdc++.h"
using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int MN = 2e5 + 10, ME = 4 * MN;
struct node{
ll su, maI, miD;
ll miP, maP, miS, maS;
};
node seg[ME];
node comb(node lef, node rig) {
return {lef.su + rig.su, max({lef.maI, rig.maI, lef.maS + rig.maP}), min({lef.miD, rig.miD, lef.miS + rig.miP}),
min(lef.miP, lef.su + rig.miP), max(lef.maP, lef.su + rig.maP), min(rig.miS, rig.su + lef.miS), max(rig.maS, rig.su + lef.maS)};
}
void upd(int v, int nl, int nr, int i, ll to) {
if(nl == i && nr == i) seg[v] = {to, max(to, 0ll), min(to, 0ll), min(to, 0ll), max(to, 0ll), min(to, 0ll), max(to, 0ll)};
else {
int mid = (nl+nr)/2;
if(i <= mid) upd(2*v, nl, mid, i, to);
else upd(2*v+1, mid+1, nr, i, to);
seg[v] = comb(seg[2*v], seg[2*v+1]);
}
}
node qu(int v, int nl, int nr, int l, int r) {
if(l > r) return {0, 0, 0};
else if(nl == l && nr == r) return seg[v];
else {
int mid = (nl+nr)/2;
return comb(
qu(2*v, nl, mid, l, min(mid, r)),
qu(2*v+1, mid+1, nr, max(mid+1, l), r)
);
}
}
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(), m=l.size();
vi ch(m);
vvi addOp(n), remOp(n);
forR(i, m) {
addOp[l[i]].push_back(i);
if(r[i] + 1 < n) remOp[r[i]+1].push_back(i);
}
vi ret(n);
vi tested(n);
forR(i, n) {
for(int j : addOp[i]) {
ch[j] = v[j];
upd(1, 0, m-1, j, v[j]);
}
for(int j : remOp[i]) {
ch[j] = 0;
upd(1, 0, m-1, j, 0);
}
// [besInd, m) has NO interval <= -c or >= c
ll uc=0, dc=0;
int lo=-1, hi=m;
// cerr << i << ": \n";
while(hi - lo > 1) {
int mid = (lo+hi)/2;
node info = qu(1, 0, m-1, mid, m-1);
// cout << mid << ' ' << info.su << ' ' << info.maI << ' ' << info.miD << '\n';
if(info.maI >= c[i] || info.miD <= -c[i]) {
lo = mid;
uc=info.maI, dc=info.miD;
} else hi = mid;
}
int besInd = hi;
// for(int j : ch) cerr << j << ' ';
// cerr << '\n';
// cerr << besInd << ' ' << uc << ' ' << dc << '\n';
if(uc >= c[i]) {
assert(dc >= -c[i]);
node info = qu(1, 0, m-1, besInd, m-1);
int cur = min(c[i], (int) (c[i] + info.su - info.maP));
ret[i] = cur;
} else {
assert(besInd == 0 || dc <= -c[i]);
node info = qu(1, 0, m-1, besInd, m-1);
int cur = max(0, (int) (info.su - info.miP));
ret[i] = cur;
}
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
5 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1113 ms |
54412 KB |
Output is correct |
2 |
Correct |
1113 ms |
54380 KB |
Output is correct |
3 |
Correct |
1163 ms |
58272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
198 ms |
37368 KB |
Output is correct |
3 |
Correct |
334 ms |
13144 KB |
Output is correct |
4 |
Correct |
1122 ms |
54356 KB |
Output is correct |
5 |
Correct |
1089 ms |
60896 KB |
Output is correct |
6 |
Correct |
1109 ms |
61072 KB |
Output is correct |
7 |
Correct |
1104 ms |
60400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
92 ms |
38276 KB |
Output is correct |
4 |
Correct |
301 ms |
14468 KB |
Output is correct |
5 |
Correct |
904 ms |
51616 KB |
Output is correct |
6 |
Correct |
878 ms |
52144 KB |
Output is correct |
7 |
Correct |
872 ms |
52680 KB |
Output is correct |
8 |
Correct |
863 ms |
51396 KB |
Output is correct |
9 |
Correct |
960 ms |
52952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
5 ms |
2908 KB |
Output is correct |
6 |
Correct |
1113 ms |
54412 KB |
Output is correct |
7 |
Correct |
1113 ms |
54380 KB |
Output is correct |
8 |
Correct |
1163 ms |
58272 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
198 ms |
37368 KB |
Output is correct |
11 |
Correct |
334 ms |
13144 KB |
Output is correct |
12 |
Correct |
1122 ms |
54356 KB |
Output is correct |
13 |
Correct |
1089 ms |
60896 KB |
Output is correct |
14 |
Correct |
1109 ms |
61072 KB |
Output is correct |
15 |
Correct |
1104 ms |
60400 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
92 ms |
38276 KB |
Output is correct |
19 |
Correct |
301 ms |
14468 KB |
Output is correct |
20 |
Correct |
904 ms |
51616 KB |
Output is correct |
21 |
Correct |
878 ms |
52144 KB |
Output is correct |
22 |
Correct |
872 ms |
52680 KB |
Output is correct |
23 |
Correct |
863 ms |
51396 KB |
Output is correct |
24 |
Correct |
960 ms |
52952 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
295 ms |
14584 KB |
Output is correct |
27 |
Correct |
196 ms |
40020 KB |
Output is correct |
28 |
Correct |
1050 ms |
58720 KB |
Output is correct |
29 |
Correct |
1105 ms |
59316 KB |
Output is correct |
30 |
Correct |
1078 ms |
59364 KB |
Output is correct |
31 |
Correct |
1102 ms |
59492 KB |
Output is correct |
32 |
Correct |
1104 ms |
59908 KB |
Output is correct |