#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
using namespace std;
const int MAX = (1 << 18) - 1;
pii tree[MAX * 3];
ll lazy[MAX * 3];
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;
}
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]){
if(a.first == 0){
ans[i] = a.second;
}
else{
ans[i] = 0;
}
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;
a = ask(1, 0, MAX, b, MAX);
if(p == a.first){
ans[i] = c[i] - (a.second - end);
}
else{
ans[i] = end - a.first;
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6868 KB |
Output is correct |
2 |
Correct |
3 ms |
6868 KB |
Output is correct |
3 |
Incorrect |
7 ms |
7116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1016 ms |
30720 KB |
Output is correct |
2 |
Correct |
1049 ms |
34268 KB |
Output is correct |
3 |
Correct |
903 ms |
34172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
6996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
6868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6868 KB |
Output is correct |
2 |
Correct |
3 ms |
6868 KB |
Output is correct |
3 |
Incorrect |
7 ms |
7116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |