#include "candies.h"
#include <cstdio>
#include <vector>
using namespace std;
using ll = long long;
int n, q;
ll mx[1 << 19], mn[1 << 19], add[1 << 19];
void lazy(int i) {
mn[i + i] += add[i];
mn[i + i + 1] += add[i];
mx[i + i] += add[i];
mx[i + i + 1] += add[i];
add[i + i] += add[i];
add[i + i + 1] += add[i];
add[i] = 0;
}
void update(int i, int s, int e, int x, int v) {
if (e < x) return;
if (x <= s) {
mn[i] += v;
mx[i] += v;
add[i] += v;
return;
}
lazy(i);
int m = (s + e) / 2;
update(i + i, s, m, x, v);
update(i + i + 1, m + 1, e, x, v);
mn[i] = min(mn[i + i], mn[i + i + 1]);
mx[i] = max(mx[i + i], mx[i + i + 1]);
}
ll low, high;
int find(int i, int s, int e, ll low, ll high, int c) {
if (max(high, mx[i]) - min(low, mn[i]) <= c) return -1;
if (s == e) {
::low = min(low, mn[i]);
::high = max(high, mx[i]);
return s;
}
lazy(i);
int m = (s + e) / 2;
int r = find(i + i + 1, m + 1, e, low, high, c);
if (r != -1) return r;
return find(i + i, s, m, min(low, mn[i + i + 1]), max(high, mx[i + i + 1]), c);
}
ll get(int i, int s, int e, int x) {
if (s == e) return add[i];
lazy(i);
int m = (s + e) / 2;
if (x <= m) return get(i + i, s, m, x);
else return get(i + i + 1, m + 1, e, x);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
n = c.size();
l.insert(l.begin(), 0);
l.insert(l.begin(), 0);
r.insert(r.begin(), n - 1);
r.insert(r.begin(), n - 1);
v.insert(v.begin(), 0);
v.insert(v.begin(), 0);
q = v.size();
vector<vector<int>> A(n, vector<int>({ 0, 1 })), R(n, vector<int>({ 0, 1 }));
for (int i = 2; i < q; ++i) {
A[l[i]].push_back(i);
R[r[i]].push_back(i);
}
vector<int> ans;
for (int i = 0; i < n; ++i) {
v[0] = c[i];
v[1] = -c[i];
for (int j : A[i]) update(1, 0, q, j + 1, v[j]);
int x = find(1, 0, q, 1e18, -1e18, c[i]);
if (x == -1) ans.push_back(get(1, 0, q, q));
else {
ll p = get(1, 0, q, x);
if (p == low) ans.push_back(c[i] + get(1, 0, q, q) - high);
else ans.push_back(get(1, 0, q, q) - low);
}
for (int j : R[i]) update(1, 0, q, j + 1, -v[j]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1249 ms |
47116 KB |
Output is correct |
2 |
Correct |
1228 ms |
48504 KB |
Output is correct |
3 |
Correct |
1197 ms |
48300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
216 ms |
23304 KB |
Output is correct |
3 |
Correct |
290 ms |
27228 KB |
Output is correct |
4 |
Correct |
1185 ms |
50516 KB |
Output is correct |
5 |
Correct |
1135 ms |
50692 KB |
Output is correct |
6 |
Correct |
1264 ms |
51072 KB |
Output is correct |
7 |
Correct |
1217 ms |
50624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
177 ms |
23232 KB |
Output is correct |
4 |
Correct |
307 ms |
26408 KB |
Output is correct |
5 |
Correct |
1160 ms |
48028 KB |
Output is correct |
6 |
Correct |
1132 ms |
48668 KB |
Output is correct |
7 |
Correct |
1145 ms |
49176 KB |
Output is correct |
8 |
Correct |
1106 ms |
48012 KB |
Output is correct |
9 |
Correct |
1022 ms |
49404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
724 KB |
Output is correct |
6 |
Correct |
1249 ms |
47116 KB |
Output is correct |
7 |
Correct |
1228 ms |
48504 KB |
Output is correct |
8 |
Correct |
1197 ms |
48300 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
216 ms |
23304 KB |
Output is correct |
11 |
Correct |
290 ms |
27228 KB |
Output is correct |
12 |
Correct |
1185 ms |
50516 KB |
Output is correct |
13 |
Correct |
1135 ms |
50692 KB |
Output is correct |
14 |
Correct |
1264 ms |
51072 KB |
Output is correct |
15 |
Correct |
1217 ms |
50624 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
177 ms |
23232 KB |
Output is correct |
19 |
Correct |
307 ms |
26408 KB |
Output is correct |
20 |
Correct |
1160 ms |
48028 KB |
Output is correct |
21 |
Correct |
1132 ms |
48668 KB |
Output is correct |
22 |
Correct |
1145 ms |
49176 KB |
Output is correct |
23 |
Correct |
1106 ms |
48012 KB |
Output is correct |
24 |
Correct |
1022 ms |
49404 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
316 ms |
26324 KB |
Output is correct |
27 |
Correct |
219 ms |
22816 KB |
Output is correct |
28 |
Correct |
1291 ms |
49096 KB |
Output is correct |
29 |
Correct |
1265 ms |
49440 KB |
Output is correct |
30 |
Correct |
1277 ms |
49564 KB |
Output is correct |
31 |
Correct |
1285 ms |
49864 KB |
Output is correct |
32 |
Correct |
1307 ms |
49964 KB |
Output is correct |