#include <bits/stdc++.h>
#include "candies.h"
using namespace std;
#define ll long long
vector<int> res;
int n, q;
bool scbl = true;
ll _res[201010];
ll t[801010];
ll tAdd[801010];
ll tPress[801010];
pair<ll, ll> Up[201010];
void push(int v, int vl, int vr) {
if (tPress[v] == -1 && tAdd[v] == 0)return;
if (vl == vr) {
if (tPress[v] != -1){t[v] = tPress[v];t[v] = min(t[v], Up[vl].first);}
if (tAdd[v] != 0)t[v] += tAdd[v];
if (t[v] < 0 || t[v] > Up[vl].first) {
exit(1);
}
} else {
if (tPress[v] != -1) {
tAdd[2 * v] = tAdd[v];
tPress[2 * v] = tPress[v];
tAdd[2 * v + 1] = tAdd[v];
tPress[2 * v + 1] = tPress[v];
} else if (tAdd[v] != 0) {
tAdd[2 * v] += tAdd[v];
tAdd[2 * v + 1] += tAdd[v];
}
}
tPress[v] = -1; tAdd[v] = 0;
}
void updAdd(int v, int vl, int vr, int l, int r, ll val) {
push(v, vl, vr);
if (l <= vl && vr <= r) {
tAdd[v] += val;
push(v, vl, vr);
return;
}
if (r < vl || vr < l)return;
int vm = (vl + vr) / 2;
updAdd(2 * v, vl, vm, l, r, val);
updAdd(2 * v + 1, vm + 1, vr, l, r, val);
}
void updPress(int v, int vl, int vr, int l, int r, int val) {
push(v, vl, vr);
if (l <= vl && vr <= r) {
tPress[v] = val;
push(v, vl, vr);
return;
}
if (r < vl || vr < l)return;
int vm = (vl + vr) / 2;
updPress(2 * v, vl, vm, l, r, val);
updPress(2 * v + 1, vm + 1, vr, l, r, val);
}
int gt(int v, int vl, int vr, int pos) {
push(v, vl, vr);
if (vl == vr)return t[v];
int vm = (vl + vr) / 2;
if (pos <= vm)return gt(2 * v, vl, vm, pos);
return gt(2 * v + 1, vm + 1, vr, pos);
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
n = c.size(); q = v.size();
res.resize(n, 0);
for (int i = 0; i < q; i++)
if (v[i] < 0){scbl = false;break;}
if (n * q <= 100000000) {
for (int i = 0; i < q; i++) {
for (int j = l[i]; j <= r[i]; j++) {
res[j] += v[i];
if (res[j] < 0)res[j] = 0;
else if (res[j] > c[j])res[j] = c[j];
}
}
return res;
}
if (scbl) {
for (int i = 0; i < q; i++) {
_res[l[i]] += v[i];
_res[r[i] + 1] -= v[i];
}
for (int i = 1; i < n; i++)
_res[i] += _res[i - 1];
for (int i = 0; i < n; i++) {
_res[i] = min(_res[i], (ll)c[i]);
res[i] = _res[i];
}
return res;
}
for (int i = 0; i < n; i++)
Up[i] = {c[i], i};
sort(Up, Up + n, [](pair<ll, ll> q, pair<ll, ll> w) {
return q.first < w.first;
});
memset(tPress, -1, sizeof(tPress));
for (int i = 0; i < n; i++) {
cout << Up[i].first << " " << Up[i].second << endl;
}
for (int id = 0; id < q; id++) {
int AllLeft = -1;
{
int l = -1, r = n;
while (l + 1 < r) {
int mid = (l + r) / 2;
int el = gt(1, 0, n - 1, mid);
if (v[id] < 0) {
if (el + v[id] <= 0)l = mid;
else r = mid;
} else {
if (el + v[id] >= Up[mid].first)l = mid;
else r = mid;
}
}
AllLeft = l;
}
if (v[id] < 0) {
if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 0);
if (AllLeft != n - 1)updAdd(1, 0, n - 1, AllLeft + 1, n - 1, v[id]);
} else {
if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 1101010101010101010);
if (AllLeft != n - 1)updAdd(1, 0, n - 1, AllLeft + 1, n - 1, v[id]);
}
}
for (int i = 0; i < n; i++)
res[Up[i].second] = gt(1, 0, n - 1, i);
return res;
}
/**
5
8 9 3 5 10
3
0 5 3
0 5 5
0 5 -4
8 8 3 5 8
3
10 15 13
2
0 2 20
0 1 -11
0 4 13
*/
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:130:65: warning: overflow in conversion from 'long int' to 'int' changes value from '1101010101010101010' to '2094199570' [-Woverflow]
130 | if (AllLeft != -1)updPress(1, 0, n - 1, 0, AllLeft, 1101010101010101010);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
7 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
9700 KB |
Output is correct |
2 |
Correct |
150 ms |
9720 KB |
Output is correct |
3 |
Correct |
144 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
228 ms |
11416 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 |
Incorrect |
348 ms |
11556 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
7 ms |
332 KB |
Output is correct |
6 |
Correct |
142 ms |
9700 KB |
Output is correct |
7 |
Correct |
150 ms |
9720 KB |
Output is correct |
8 |
Correct |
144 ms |
9708 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Incorrect |
228 ms |
11416 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |