#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
int get_max_r(int ind, vector<int>& ops, int tar, bool pref) {
int ret = ind - 1;
long long min_val = 0;
for (int i = ind; i < (int) ops.size(); ++i) {
if (min_val + ops[i] < tar && ops[i] < 0) {
ret = i;
}
if (!pref) {
min_val = min(min_val + ops[i], 0LL);
if (min_val < tar) {
min_val = 0;
assert(ret == i);
}
} else {
min_val += ops[i];
if (min_val < 0) {
min_val = 0;
assert(ret == i);
}
}
}
return ret;
}
int get_max_pos(int ind, vector<int>& ops, int tar) {
int ret = ind - 1;
long long max_val = 0;
for (int i = ind; i < (int) ops.size(); ++i) {
if (max_val + ops[i] > tar && ops[i] > 0) {
ret = i;
}
max_val = min(max_val + ops[i], (long long) tar);
}
return ret;
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
vector<vector<int>> ql(n), qr(n);
for (int i = 0; i < q; ++i) {
ql[l[i]].push_back(i);
qr[r[i]].push_back(i);
}
vector<int> s(n);
vector<int> ops(q);
for (int i = 0; i < n; ++i) {
for (int j : ql[i]) {
ops[j] = v[j];
}
int start_from = 0;
start_from = get_max_r(start_from, ops, -c[i], false) + 1;//maximumR s.t: some suf containing R is < -ci
//for (int j : ops) cout << j << ' ';
//cout << '\n';
//cout << "START: " << start_from << '\n';
start_from = get_max_r(start_from, ops, 0, true) + 1;//maximumR s.t: some pref is negative ( < 0)
//cout << "maximumR s.t: some suf containing R is < -ci: " << start_from << '\n';
//cout << "OPS: "; for (int j : ops) cout << j << ' ';cout << '\n';
//cout << "TMP: " << tmp << '\n';
int tmp = get_max_pos(start_from, ops, c[i]) + 1;
int sum = 0;
if (tmp != start_from) {
start_from = tmp;
sum = c[i];
}
for (int j = start_from; j < (int) ops.size(); ++j) {
//cout << ops[j] << ' ';
sum += ops[j];
}
//cout << '\n';
s[i] = sum;
for (int j : qr[i]) {
ops[j] = 0;
}
}
return s;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5045 ms |
24676 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1535 ms |
8724 KB |
Output is correct |
3 |
Correct |
1618 ms |
12216 KB |
Output is correct |
4 |
Execution timed out |
5072 ms |
24636 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1569 ms |
7692 KB |
Output is correct |
4 |
Correct |
1105 ms |
12124 KB |
Output is correct |
5 |
Execution timed out |
5038 ms |
19256 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
10 ms |
468 KB |
Output is correct |
6 |
Execution timed out |
5045 ms |
24676 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |