#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;
};
node seg[ME];
node comb(node lef, node rig) {
return {lef.su + rig.su, max(lef.maI, lef.su + rig.maI), min(lef.miD, lef.su + rig.miD)};
}
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)};
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, n) has NO interval <= -c or >= c
int besInd=m;
ll uc=0, dc=0;
for(int j = m-1; j >= 0; --j) {
node info = qu(1, 0, m-1, j, m-1);
uc = info.maI;
dc = info.miD;
if(uc >= c[i] || dc <= -c[i]) break;
else besInd = j;
}
// cerr << i << ": \n";
// for(int j : ch) cerr << j << ' ';
// cerr << '\n';
// cerr << besInd << ' ' << uc << ' ' << dc << '\n';
if(uc >= c[i]) {
assert(dc >= -c[i]);
int cur = c[i];
REP(j, besInd, m) {
cur = min(c[i], cur + ch[j]);
assert(cur >= 0);
}
ret[i] = cur;
} else {
assert(besInd == 0 || dc <= -c[i]);
int cur = 0;
REP(j, besInd, m) {
cur = max(0, cur + ch[j]);
assert(cur <= c[i]);
}
ret[i] = cur;
}
int cur = 0;
forR(j, m) cur = max(0, min(c[i], cur + ch[j]));
assert(cur == ret[i]);
}
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
628 KB |
Output is correct |
5 |
Correct |
47 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5098 ms |
35152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
5097 ms |
20988 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Execution timed out |
5090 ms |
19252 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
5 ms |
628 KB |
Output is correct |
5 |
Correct |
47 ms |
604 KB |
Output is correct |
6 |
Execution timed out |
5098 ms |
35152 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |