#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
using namespace std;
const int MAX = 2e5 + 5;
pii tree[(MAX + 5) * 4];
ll lazy[(MAX + 5) * 4];
void relax(int node, int l, int r){
if(lazy[node] == 0) return;
tree[node].first += lazy[node];
tree[node].second += lazy[node];
if(l != r){
lazy[node * 2] += lazy[node];
lazy[node * 2 + 1] += lazy[node];
}
lazy[node] = 0;
}
pii combine(pii a, pii b){
return {min(a.first, b.first), max(a.second, b.second)};
}
void update(int node, int l, int r, int ql, int qr, ll val){
relax(node, l, r);
if(ql > r || qr < l) return;
if(ql <= l && r <= qr){
lazy[node] += val;
relax(node, l, r);
return;
}
relax(node, l, r);
int mid = (l + r) / 2;
update(node * 2, l, mid, ql, qr, val);
update(node * 2 + 1, mid + 1, r, ql, qr, val);
tree[node] = combine(tree[node * 2], tree[node * 2 + 1]);
}
const pii dummy = {1e18, -1e18};
pii ask(int node, int l, int r, int ql, int qr){
if(ql > r || qr < l) return dummy;
relax(node, l, r);
if(ql <= l && r <= qr){
return tree[node];
}
int mid = (l + r) / 2;
return combine(ask(node * 2, l, mid, ql, qr), ask(node * 2 + 1, mid + 1, r, ql, qr));
}
vector<array<int, 2>> events[MAX];
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n = c.size();
int q = l.size();
for (int i = 0; i < q; i++)
{
events[l[i]].push_back({1, i + 1});
events[r[i] + 1].push_back({0, i + 1});
}
vector<int> ans(n);
for (int i = 0; i < n; i++)
{
for(auto e:events[i]){
int idx = e[1];
if(e[0]){
update(1, 0, MAX, idx, MAX, v[idx - 1]);
}
else{
update(1, 0, MAX, idx, MAX, -v[idx - 1]);
}
}
ll end = ask(1, 0, MAX, MAX, MAX).second;
pii a = ask(1, 0, MAX, 0, MAX);
if(a.second - a.first <= c[i]){
ans[i] = end;
continue;
}
int l = 0, r = MAX;
int b = 0;
while(l <= r){
int mid = (l + r) / 2;
a = ask(1, 0, MAX, mid, MAX);
ll d = a.second - a.first;
if(d > c[i]){
l = mid + 1;
b = mid;
}
else{
r = mid - 1;
}
}
ll p = ask(1, 0, MAX, b, b).first;
pii z = ask(1, 0, MAX, b, MAX);
if(p == z.first){
ans[i] = c[i] - (z.second - end);
}
else{
ans[i] = end - z.first;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
825 ms |
36372 KB |
Output is correct |
2 |
Correct |
832 ms |
35468 KB |
Output is correct |
3 |
Correct |
752 ms |
35316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Correct |
6 ms |
5520 KB |
Output is correct |
3 |
Incorrect |
98 ms |
27836 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5460 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |