#include "candies.h"
#include <bits/stdc++.h>
#define FOR(i,l,r) for(int i=(l); i<=(r); ++i)
#define REP(i,l,r) for(int i=(l); i<(r); ++i)
#define FORD(i,r,l) for(int i=(r); i>=(l); --i)
#define REPD(i,r,l) for(int i=(r)-1; i>=(l); --i)
using namespace std;
const int N = 2e5 + 5;
int n, q;
long long lazy[N * 4], vmin[N * 4], vmax[N * 4];
long long minF, maxF;
vector<int> Q[N];
void down(int range, int pos) {
if (lazy[pos]) {
vmin[pos] += lazy[pos];
vmax[pos] += lazy[pos];
if (range) {
lazy[pos * 2] += lazy[pos];
lazy[pos * 2 + 1] += lazy[pos];
}
lazy[pos] = 0;
}
}
void update(int x, int y, int val, int l = 0, int r = q, int pos = 1) {
if (x > r || y < l || x > y) return;
if (x <= l && r <= y) {
lazy[pos] += val;
return;
}
down(r - l, pos);
int mid = (l + r) / 2;
update(x, y, val, l, mid, pos * 2);
update(x, y, val, mid + 1, r, pos * 2 + 1);
vmin[pos] = min(vmin[pos * 2] + lazy[pos * 2], vmin[pos * 2 + 1] + lazy[pos * 2 + 1]);
vmax[pos] = max(vmax[pos * 2] + lazy[pos * 2], vmax[pos * 2 + 1] + lazy[pos * 2 + 1]);
}
int query(int diff, int l = 0, int r = q, int pos = 1) {
down(r - l, pos);
if (max(maxF, vmax[pos]) - min(minF, vmin[pos]) < diff) {
maxF = max(maxF, vmax[pos]);
minF = min(minF, vmin[pos]);
return l - 1;
}
if (l == r) {
maxF = max(maxF, vmax[pos]);
minF = min(minF, vmin[pos]);
if (maxF == vmax[pos]) maxF = minF + diff;
else minF = maxF - diff;
return l;
}
int mid = (l + r) / 2;
int tmp = query(diff, mid + 1, r, pos * 2 + 1);
if (tmp > mid) return tmp;
return query(diff, l, mid, pos * 2);
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = c.size();
q = v.size();
vector<int> ans(n, 0);
REP(i, 0, q) {
Q[l[i]].push_back(i);
Q[r[i] + 1].push_back(i + q);
}
REP(i, 0, n) {
for(int id : Q[i]) {
if (id < q) update(0, id, v[id]);
else update(0, id - q, -v[id - q]);
}
minF = maxF = 0;
int tmp = query(c[i]);
ans[i] = maxF;
}
return ans;
}
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:78:13: warning: unused variable 'tmp' [-Wunused-variable]
78 | int tmp = query(c[i]);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
5 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
397 ms |
30696 KB |
Output is correct |
2 |
Correct |
370 ms |
34044 KB |
Output is correct |
3 |
Correct |
396 ms |
33812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
236 ms |
21348 KB |
Output is correct |
3 |
Correct |
75 ms |
10940 KB |
Output is correct |
4 |
Correct |
405 ms |
31812 KB |
Output is correct |
5 |
Correct |
411 ms |
33572 KB |
Output is correct |
6 |
Correct |
371 ms |
32696 KB |
Output is correct |
7 |
Correct |
338 ms |
32132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5008 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
115 ms |
20968 KB |
Output is correct |
4 |
Correct |
70 ms |
8672 KB |
Output is correct |
5 |
Correct |
162 ms |
26052 KB |
Output is correct |
6 |
Correct |
174 ms |
26624 KB |
Output is correct |
7 |
Correct |
191 ms |
26868 KB |
Output is correct |
8 |
Correct |
166 ms |
25916 KB |
Output is correct |
9 |
Correct |
202 ms |
26924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5012 KB |
Output is correct |
3 |
Correct |
5 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5204 KB |
Output is correct |
6 |
Correct |
397 ms |
30696 KB |
Output is correct |
7 |
Correct |
370 ms |
34044 KB |
Output is correct |
8 |
Correct |
396 ms |
33812 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
236 ms |
21348 KB |
Output is correct |
11 |
Correct |
75 ms |
10940 KB |
Output is correct |
12 |
Correct |
405 ms |
31812 KB |
Output is correct |
13 |
Correct |
411 ms |
33572 KB |
Output is correct |
14 |
Correct |
371 ms |
32696 KB |
Output is correct |
15 |
Correct |
338 ms |
32132 KB |
Output is correct |
16 |
Correct |
3 ms |
5008 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
115 ms |
20968 KB |
Output is correct |
19 |
Correct |
70 ms |
8672 KB |
Output is correct |
20 |
Correct |
162 ms |
26052 KB |
Output is correct |
21 |
Correct |
174 ms |
26624 KB |
Output is correct |
22 |
Correct |
191 ms |
26868 KB |
Output is correct |
23 |
Correct |
166 ms |
25916 KB |
Output is correct |
24 |
Correct |
202 ms |
26924 KB |
Output is correct |
25 |
Correct |
3 ms |
5004 KB |
Output is correct |
26 |
Correct |
70 ms |
8780 KB |
Output is correct |
27 |
Correct |
247 ms |
23352 KB |
Output is correct |
28 |
Correct |
350 ms |
33600 KB |
Output is correct |
29 |
Correct |
390 ms |
34192 KB |
Output is correct |
30 |
Correct |
391 ms |
34152 KB |
Output is correct |
31 |
Correct |
398 ms |
33112 KB |
Output is correct |
32 |
Correct |
425 ms |
31700 KB |
Output is correct |