#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;
}
//cout<<l<<"___"<<r<<" "<<tree[u]<<endl;
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 <= 2000 && q <= 2000) {
// 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, 0, n-1);
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, 0, n-1);
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:85:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int i = 0; i < c.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
7 ms |
412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
13672 KB |
Output is correct |
2 |
Correct |
121 ms |
12984 KB |
Output is correct |
3 |
Correct |
119 ms |
12772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
123 ms |
9660 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
208 ms |
9236 KB |
Output is correct |
4 |
Correct |
120 ms |
12212 KB |
Output is correct |
5 |
Correct |
548 ms |
20652 KB |
Output is correct |
6 |
Correct |
561 ms |
21408 KB |
Output is correct |
7 |
Correct |
581 ms |
21884 KB |
Output is correct |
8 |
Correct |
590 ms |
20544 KB |
Output is correct |
9 |
Correct |
133 ms |
13764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
7 ms |
412 KB |
Output is correct |
6 |
Correct |
134 ms |
13672 KB |
Output is correct |
7 |
Correct |
121 ms |
12984 KB |
Output is correct |
8 |
Correct |
119 ms |
12772 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
123 ms |
9660 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |