#include "candies.h"
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e16;
vector<pair<ll, ll>> tree;
vector<ll> lazy;
vector<int> query[200002];
vector<int> ans;
void propagate(int i, int l, int r)
{
tree[i].first += lazy[i];
tree[i].second += lazy[i];
if(l != r)
{
lazy[i*2] += lazy[i];
lazy[i*2+1] += lazy[i];
}
lazy[i] = 0;
}
void update(int i, int l, int r, int s, int e, ll v)
{
propagate(i, l, r);
if(e < l || r < s) return;
if(s <= l && r <= e)
{
lazy[i] += v;
propagate(i, l, r);
return;
}
int m = (l + r)/2;
update(i*2, l, m, s, e, v);
update(i*2+1, m+1, r, s, e, v);
tree[i].first = max(tree[i*2].first, tree[i*2+1].first);
tree[i].second = min(tree[i*2].second, tree[i*2+1].second);
}
ll sum = 0;
void solve(int i, int l, int r, ll C, int k, ll Ma, ll mi)
{
propagate(i, l, r);
if(l == r)
{
if(max(tree[i].first,Ma) - min(tree[i].first,mi) <= C) ans[k] = sum - min(tree[i].first, mi);
else if(tree[i].first > Ma) ans[k] = sum - mi;
else ans[k] = C - Ma + sum;
return;
}
int m = (l + r)/2;
propagate(i*2+1, m+1, r);
if(max(tree[i*2+1].first, Ma) - min(tree[i*2+1].second, mi) <= C)
{
Ma = max(tree[i*2+1].first, Ma), mi = min(tree[i*2+1].second, mi);
solve(i*2, l, m, C, k, Ma, mi);
}
else solve(i*2+1, m+1, r, C, k, Ma, mi);
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = c.size();
int q = l.size();
tree.resize(4*q+4);
lazy.resize(4*q+4);
ans.resize(n);
for(int i=1;i<=q;i++)
{
query[l[i-1]].push_back(i);
query[r[i-1]+1].push_back(-i);
}
for(int i=0;i<n;i++)
{
for(int j : query[i])
{
if(j > 0) { update(1, 0, q, j, q, v[j-1]); sum += v[j-1]; }
else { update(1, 0, q, -j, q, -v[-j-1]); sum -= v[-j-1]; }
}
solve(1, 0, q, c[i], i, -INF, INF);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
6 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
603 ms |
37168 KB |
Output is correct |
2 |
Correct |
576 ms |
41244 KB |
Output is correct |
3 |
Correct |
567 ms |
41084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
345 ms |
33628 KB |
Output is correct |
3 |
Correct |
89 ms |
11700 KB |
Output is correct |
4 |
Correct |
551 ms |
42920 KB |
Output is correct |
5 |
Correct |
566 ms |
43636 KB |
Output is correct |
6 |
Correct |
566 ms |
43880 KB |
Output is correct |
7 |
Correct |
554 ms |
43208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
163 ms |
32824 KB |
Output is correct |
4 |
Correct |
89 ms |
9660 KB |
Output is correct |
5 |
Correct |
280 ms |
36912 KB |
Output is correct |
6 |
Correct |
288 ms |
37552 KB |
Output is correct |
7 |
Correct |
289 ms |
38152 KB |
Output is correct |
8 |
Correct |
277 ms |
36796 KB |
Output is correct |
9 |
Correct |
270 ms |
38268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
6 ms |
5240 KB |
Output is correct |
6 |
Correct |
603 ms |
37168 KB |
Output is correct |
7 |
Correct |
576 ms |
41244 KB |
Output is correct |
8 |
Correct |
567 ms |
41084 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
345 ms |
33628 KB |
Output is correct |
11 |
Correct |
89 ms |
11700 KB |
Output is correct |
12 |
Correct |
551 ms |
42920 KB |
Output is correct |
13 |
Correct |
566 ms |
43636 KB |
Output is correct |
14 |
Correct |
566 ms |
43880 KB |
Output is correct |
15 |
Correct |
554 ms |
43208 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
163 ms |
32824 KB |
Output is correct |
19 |
Correct |
89 ms |
9660 KB |
Output is correct |
20 |
Correct |
280 ms |
36912 KB |
Output is correct |
21 |
Correct |
288 ms |
37552 KB |
Output is correct |
22 |
Correct |
289 ms |
38152 KB |
Output is correct |
23 |
Correct |
277 ms |
36796 KB |
Output is correct |
24 |
Correct |
270 ms |
38268 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
90 ms |
9588 KB |
Output is correct |
27 |
Correct |
344 ms |
33360 KB |
Output is correct |
28 |
Correct |
553 ms |
41728 KB |
Output is correct |
29 |
Correct |
544 ms |
42180 KB |
Output is correct |
30 |
Correct |
551 ms |
42300 KB |
Output is correct |
31 |
Correct |
557 ms |
42520 KB |
Output is correct |
32 |
Correct |
548 ms |
42820 KB |
Output is correct |