#include "candies.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define oo 1e16
using namespace std;
int n, q;
const int MAX = 2e5 + 5;
pii tree[4 * MAX];
ll lazy[4 * MAX];
void relax(int node, int l, int r){
if(!lazy[node]) return;
tree[node].first += lazy[node];
tree[node].second += lazy[node];
if(l == r){
lazy[node] = 0;
return;
}
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
lazy[node] = 0;
}
pii comb(pii a, pii b){
return {min(a.first, b.first), max(a.second, b.second)};
}
void update(int node, int l, int r, int ul, int ur, int v){
relax(node, l, r);
if(ur < l || r < ul){
return;
}
if(ul <= l && r <= ur){
lazy[node] += v;
relax(node, l, r);
return;
}
int mid = (l + r) / 2;
update(2 * node, l, mid, ul, ur, v);
update(2 * node + 1, mid + 1, r, ul, ur, v);
tree[node] = comb(tree[2 * node], tree[2 * node + 1]);
}
pii ask(int node, int l, int r, int ql, int qr){
if(qr < l || r < ql) return {oo, -oo};
relax(node, l, r);
if(ql <= l && r <= qr) return tree[node];
int mid = (l + r) / 2;
return comb(ask(2 * node, l, mid, ql, qr), ask(2 * node + 1, mid + 1, r, ql, qr));
}
vector<pii> E[MAX];
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = c.size(), q = l.size();
l.insert(l.begin(), 0);
r.insert(r.begin(), 0);
v.insert(v.begin(), 0);
c.insert(c.begin(), 0);
for(int i = 0; i <= 4 * q; i++){
tree[i] = {0, 0};
lazy[i] = 0;
}
for(int i = 1; i <= q; i++){
l[i]++;
r[i]++;
E[l[i]].push_back({i, 1});
E[r[i]].push_back({i, 0});
}
vector<int> res;
for(int i = 1; i <= n; i++){
for(pii t : E[i]){
if(t.second){
update(1, 0, q, t.first, q, v[t.first]);
}
}
int L = 0, R = q;
int ans = -1;
while(L <= R){
int mid = (L + R) / 2;
pii a = ask(1, 0, q, mid, q);
if(a.second - a.first > c[i]){
L = mid + 1;
ans = mid;
}
else{
R = mid - 1;
}
}
ll lst = ask(1, 0, q, q, q).first;
if(ans == -1){
ll mn = ask(1, 0, q, 0, q).first;
res.push_back(lst - mn);
continue;
}
pii a = ask(1, 0, q, ans, q);
ll frs = ask(1, 0, q, ans, ans).first;
ll bottom = 0;
if(a.first == frs){
bottom = a.second - c[i];
}
else{
bottom = a.first;
}
res.push_back(lst - bottom);
for(pii t : E[i]){
if(!t.second){
update(1, 0, q, t.first, q, -v[t.first]);
}
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Incorrect |
3 ms |
8832 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
920 ms |
46380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Incorrect |
189 ms |
40032 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
3 ms |
8792 KB |
Output is correct |
3 |
Correct |
144 ms |
42588 KB |
Output is correct |
4 |
Correct |
231 ms |
13620 KB |
Output is correct |
5 |
Correct |
749 ms |
44536 KB |
Output is correct |
6 |
Correct |
708 ms |
42488 KB |
Output is correct |
7 |
Correct |
728 ms |
44296 KB |
Output is correct |
8 |
Correct |
696 ms |
41212 KB |
Output is correct |
9 |
Correct |
744 ms |
44296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Incorrect |
3 ms |
8832 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |