#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long p[N], pr[N];
int lazy[4 * N], tree[4 * N], pos[N];
#include <vector>
void update(int u, int start, int end, int l, int r, int idx) {
if (lazy[u]) {
tree[u] = lazy[u];
if (l != r) {
lazy[2 * u] = lazy[2 * u + 1] = lazy[u];
}
lazy[u] = 0;
}
if (l > end || r < start) return;
if (start <= l && r <= end) {
lazy[u] = idx;
tree[u] = lazy[u];
if (l != r) {
lazy[2 * u] = lazy[2 * u + 1] = lazy[u];
}
lazy[u] = 0;
return;
}
int mid = (l + r) / 2;
update(2 * u, start, end, l, mid, idx);
update(2 * u + 1, start, end, mid + 1, r, idx);
}
int getans(int u, int idx, int l, int r) {
if (lazy[u]) {
tree[u] = lazy[u];
if (l != r) {
lazy[2 * u] = lazy[2 * u + 1] = lazy[u];
}
lazy[u] = 0;
}
if (l == r) return tree[u];
int mid = (l + r) / 2;
if (idx > mid) return getans(2 * u + 1, idx, mid + 1, r);
return getans(2 * u, idx, l, mid);
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int mn = 0;
int n = c.size(); vector<int>ans(n);
int q = l.size();
for (int i = 0; i < q; i++) {
mn = min(mn, v[i]);
}
if (n <= 1 && q <= 1) {
// subtask #1
for (int i = 0; i <= q; i++) {
for (int j = l[i]; j <= r[i]; j++) {
ans[j] = min(c[j], max(0, ans[j] + v[i]));
}
}
return ans;
}
else if (mn == 0) {
for (int i = 0; i <= q; i++) {
p[l[i]] += v[i];
p[r[i] + 1] -= v[i];
}
for (int i = 0; i <= q; i++) {
if (i) p[i] += p[i - 1];
ans[i] = min((long long)c[i], p[i]);
}
return ans;
}
else {
vector<pair<int, int> > p;
for (int i = 0; i < n; i++) {
p.push_back({ c[i],i });
}
sort(p.begin(), p.end());
v.push_back(0);
for (int i = q; i >= 1; i--) {
v[i] = v[i - 1];
}v[0] = 0;
for (int i = 0; i < c.size(); i++) {
pos[p[i].second] = i;
}
for (int i = 1; i <= q; i++) {
pr[i] += pr[i - 1] + v[i];
if (v[i] <= 0) {
int l = 0, r = n - 1, idx = -1;
while (l <= r) {
int mid = (l + r) / 2;
int h = getans(1, mid, 1, n);
if ((v[h] <= 0 && pr[i] - pr[h] <= 0) || (v[h] > 0 && pr[i] - pr[h] + p[mid].first <= 0)) {
idx = mid;
l = mid + 1;
}
else r = mid - 1;
}
update(1, 0, idx, 0, n - 1, i);
}
else {
int l = 0, r = n - 1, idx = -1;
while (l <= r) {
int mid = (l + r) / 2;
int h = getans(1, mid, 1, n);
if ((v[h] <= 0 && pr[i] - pr[h] >= p[mid].first) || (v[h] > 0 && pr[i] - pr[h] >= 0)) {
idx = mid;
l = mid + 1;
}
else r = mid - 1;
}
update(1, 0, idx, 0, n - 1, i);
}
}
vector<int> ans;
for (int i = 0; i < n; i++) {
int h = getans(1, pos[i], 0, n - 1);
if (v[h] <= 0) ans.push_back(pr[q] - pr[h]);
else ans.push_back(pr[q] - pr[h] + c[i]);
}
return ans;
}
}
/*
#include <cassert>
#include <cstdio>
#include <vector>
int main() {
int n;
assert(1 == scanf("%d", &n));
std::vector<int> c(n);
for(int i = 0; i < n; ++i) {
assert(scanf("%d", &c[i]) == 1);
}
int q;
assert(1 == scanf("%d", &q));
std::vector<int> l(q), r(q), v(q);
for(int i = 0; i < q; ++i) {
assert(scanf("%d %d %d", &l[i], &r[i], &v[i]) == 3);
}
std::vector<int> ans = distribute_candies(c, l, r, v);
for(int i = 0; i < n; ++i) {
if (i > 0) {
printf(" ");
}
printf("%d", ans[i]);
}
printf("\n");
fclose(stdout);
return 0;
}*/
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:83:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < c.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
8804 KB |
Output is correct |
2 |
Correct |
123 ms |
8804 KB |
Output is correct |
3 |
Correct |
148 ms |
8896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |