#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
const ll inf = 1e18;
int n, q;
vector < int > s;
ll pref[maxn], cap[maxn];
vector < int > add[maxn], rem[maxn];
struct node
{
ll max_val, min_val;
node(ll _max_val = 0, ll _min_val = 0)
{
max_val = _max_val;
min_val = _min_val;
}
};
node tree[4 * maxn];
ll lazy[4 * maxn];
void push_lazy(int root, int left, int right)
{
tree[root].max_val += lazy[root];
tree[root].min_val += lazy[root];
if (left != right)
{
lazy[root * 2] += lazy[root];
lazy[root * 2 + 1] += lazy[root];
}
lazy[root] = 0;
}
node merge_node(node left, node right)
{
node cur;
cur.max_val = max(left.max_val, right.max_val);
cur.min_val = min(left.min_val, right.min_val);
return cur;
}
void update_range(int root, int left, int right, int qleft, int qright, ll val)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return;
if (left >= qleft && right <= qright)
{
lazy[root] += val;
push_lazy(root, left, right);
return;
}
int mid = (left + right) / 2;
update_range(root * 2, left, mid, qleft, qright, val);
update_range(root * 2 + 1, mid + 1, right, qleft, qright, val);
tree[root] = merge_node(tree[root * 2], tree[root * 2 + 1]);
}
node query(int root, int left, int right, int qleft, int qright)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return node(-inf, inf);
if (left >= qleft && right <= qright)
return tree[root];
int mid = (left + right) / 2;
return merge_node(query(root * 2, left, mid, qleft, qright),
query(root * 2 + 1, mid + 1, right, qleft, qright));
}
bool check(int pos, ll val)
{
node ver = query(1, 0, q + 1, pos, q + 1);
ll min_height = ver.min_val;
ll max_height = ver.max_val;
///cout << pos << " : " << val << " : " << ver.max_val << " " << ver.min_val << endl;
/**for (int i = pos; i < q + 2; i ++)
{
min_height = min(min_height, pref[i]);
max_height = max(max_height, pref[i]);
}*/
return (max_height - val > min_height);
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v)
{
n = c.size();
q = l.size();
s.resize(n, 0);
for (int i = 0; i < n; i ++)
cap[i] = c[i];
for (int i = 0; i < q; i ++)
{
add[l[i]].push_back(i);
rem[r[i]].push_back(i);
}
update_range(1, 0, q + 1, 0, q + 1, inf);
update_range(1, 0, q + 1, 1, q + 1, -inf);
ll fin = 0;
for (int i = 0; i < n; i ++)
{
for (int idx : add[i])
{
///cout << idx << endl;
update_range(1, 0, q + 1, idx + 2, q + 1, v[idx]), fin += v[idx];
}
/**cout << "check " << i << endl;
for (int j = 0; j < q + 2; j ++)
cout << query(1, 0, q + 1, j, j).max_val << " ";
cout << endl;*/
/**pref[0] = inf;
pref[1] = 0;
for (int j = 0; j < q; j ++)
{
pref[j + 2] = pref[j + 1];
if (l[j] <= i && r[j] >= i)
{
///cout << "here" << endl;
pref[j + 2] += v[j];
}
}*/
/**for (int j = 0; j < q + 2; j ++)
cout << pref[j] << " ";
cout << endl;*/
int lf = 1, rf = q + 1;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (check(mf, c[i]))
lf = mf + 1;
else
rf = mf - 1;
}
node ver = query(1, 0, q + 1, rf, q + 1);
ll min_height = ver.min_val;
ll max_height = ver.max_val;
/**for (int i = pos; i < q + 2; i ++)
{
min_height = min(min_height, pref[i]);
max_height = max(max_height, pref[i]);
}*/
//cout << "final " << min_height << " " << max_height << " " << rf << endl;
ll base =query(1, 0, q + 1, rf, rf).max_val;
//cout << base << " " << fin << " " << max_height << endl;
if (max_height == query(1, 0, q + 1, rf, rf).max_val)
s[i] = fin - min_height;
else
s[i] = fin - (max_height - c[i]);//, cout << "yes" << endl;
for (int idx : rem[i])
{
///cout << idx << endl;
update_range(1, 0, q + 1, idx + 2, q + 1, -v[idx]), fin -= v[idx];
}
}
return s;
}
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:171:12: warning: unused variable 'base' [-Wunused-variable]
171 | ll base =query(1, 0, q + 1, rf, rf).max_val;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
10 ms |
22228 KB |
Output is correct |
3 |
Correct |
11 ms |
22336 KB |
Output is correct |
4 |
Correct |
13 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1336 ms |
42912 KB |
Output is correct |
2 |
Correct |
1318 ms |
46024 KB |
Output is correct |
3 |
Correct |
1260 ms |
46028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
274 ms |
33928 KB |
Output is correct |
3 |
Correct |
385 ms |
28512 KB |
Output is correct |
4 |
Correct |
1229 ms |
43296 KB |
Output is correct |
5 |
Correct |
1251 ms |
49292 KB |
Output is correct |
6 |
Correct |
1243 ms |
49632 KB |
Output is correct |
7 |
Correct |
1242 ms |
48952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22228 KB |
Output is correct |
2 |
Correct |
11 ms |
22228 KB |
Output is correct |
3 |
Correct |
179 ms |
32760 KB |
Output is correct |
4 |
Correct |
401 ms |
27096 KB |
Output is correct |
5 |
Correct |
1070 ms |
37532 KB |
Output is correct |
6 |
Correct |
1075 ms |
41432 KB |
Output is correct |
7 |
Correct |
1093 ms |
41400 KB |
Output is correct |
8 |
Correct |
1051 ms |
40932 KB |
Output is correct |
9 |
Correct |
1041 ms |
41404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22228 KB |
Output is correct |
2 |
Correct |
10 ms |
22228 KB |
Output is correct |
3 |
Correct |
11 ms |
22336 KB |
Output is correct |
4 |
Correct |
13 ms |
22228 KB |
Output is correct |
5 |
Correct |
16 ms |
22356 KB |
Output is correct |
6 |
Correct |
1336 ms |
42912 KB |
Output is correct |
7 |
Correct |
1318 ms |
46024 KB |
Output is correct |
8 |
Correct |
1260 ms |
46028 KB |
Output is correct |
9 |
Correct |
11 ms |
22228 KB |
Output is correct |
10 |
Correct |
274 ms |
33928 KB |
Output is correct |
11 |
Correct |
385 ms |
28512 KB |
Output is correct |
12 |
Correct |
1229 ms |
43296 KB |
Output is correct |
13 |
Correct |
1251 ms |
49292 KB |
Output is correct |
14 |
Correct |
1243 ms |
49632 KB |
Output is correct |
15 |
Correct |
1242 ms |
48952 KB |
Output is correct |
16 |
Correct |
10 ms |
22228 KB |
Output is correct |
17 |
Correct |
11 ms |
22228 KB |
Output is correct |
18 |
Correct |
179 ms |
32760 KB |
Output is correct |
19 |
Correct |
401 ms |
27096 KB |
Output is correct |
20 |
Correct |
1070 ms |
37532 KB |
Output is correct |
21 |
Correct |
1075 ms |
41432 KB |
Output is correct |
22 |
Correct |
1093 ms |
41400 KB |
Output is correct |
23 |
Correct |
1051 ms |
40932 KB |
Output is correct |
24 |
Correct |
1041 ms |
41404 KB |
Output is correct |
25 |
Correct |
10 ms |
22236 KB |
Output is correct |
26 |
Correct |
413 ms |
28340 KB |
Output is correct |
27 |
Correct |
310 ms |
36540 KB |
Output is correct |
28 |
Correct |
1239 ms |
47476 KB |
Output is correct |
29 |
Correct |
1220 ms |
47988 KB |
Output is correct |
30 |
Correct |
1322 ms |
48056 KB |
Output is correct |
31 |
Correct |
1270 ms |
48252 KB |
Output is correct |
32 |
Correct |
1246 ms |
48464 KB |
Output is correct |