#include "candies.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 2e5 + 7;
const ll oo = 1e18;
int c[N], n, q;
V<pi> ev[N];
struct node {
ll mn, mx, lz;
node() {
mn = mx = lz = 0;
}
} t[N * 4];
void apply(int v, ll x) {
t[v].lz += x;
t[v].mn += x;
t[v].mx += x;
}
void push(int v) {
apply(v * 2, t[v].lz);
apply(v * 2 + 1, t[v].lz);
t[v].lz = 0;
}
void pull(int v) {
t[v].mn = min(t[v * 2].mn, t[v * 2 + 1].mn);
t[v].mx = max(t[v * 2].mx, t[v * 2 + 1].mx);
}
void add(int l, int r, ll x, int v = 1, int tl = 0, int tr = q + 1) {
if(l <= tl && tr <= r) {
apply(v, x);
return;
}
push(v);
int tm = (tl + tr) / 2;
if(l < tm) add(l, r, x, v * 2, tl, tm);
if(r > tm) add(l, r, x, v * 2 + 1, tm, tr);
pull(v);
}
ll qmin(int l, int r, int v = 1, int tl = 0, int tr = q + 1){
if(l <= tl && tr <= r)
return t[v].mn;
int tm = (tl + tr) / 2;
ll res = oo;
if(l < tm) res = min(res, qmin(l, r, v * 2, tl, tm));
if(r > tm) res = min(res, qmin(l, r, v * 2 + 1, tm, tr));
return res;
}
ll qmax(int l, int r, int v = 1, int tl = 0, int tr = q + 1){
if(l <= tl && tr <= r)
return t[v].mx;
int tm = (tl + tr) / 2;
ll res = -oo;
if(l < tm) res = max(res, qmax(l, r, v * 2, tl, tm));
if(r > tm) res = max(res, qmax(l, r, v * 2 + 1, tm, tr));
return res;
}
vi distribute_candies(vi _c, vi _l, vi _r, vi _v) {
n = SZ(_c), q = SZ(_l);
for(int i = 0; i < n; i++)
c[i] = _c[i];
vi ans(n);
for(int i = 1; i <= q; i++) {
int l = _l[i - 1], r = _r[i - 1], v = _v[i - 1];
ev[l].EB(i, v);
ev[r + 1].EB(i, -v);
}
for(int i = 0; i < n; i++) {
for(auto [pos, val]:ev[i])
add(pos, q + 1, val);
ll final = qmax(q, q + 1);
ll mx = qmax(0, q + 1);
ll mn = qmin(0, q + 1);
if(mx - mn <= c[i]) {
ans[i] = final - mn;
} else {
int lb = 0, rb = q;
while(lb <= rb) {
int mb = (lb + rb) / 2;
ll mx = qmax(mb, q + 1);
ll mn = qmin(mb, q + 1);
if(mx - mn <= c[i])
rb = mb - 1;
else
lb = mb + 1;
}
ll mx = qmax(rb, q + 1);
ll mn = qmin(rb, q + 1);
assert(mx - mn > c[i]);
ll val = qmax(rb, rb + 1);
if(val == mx) {
ans[i] = final - mn;
} else {
ans[i] = final - (mx - c[i]);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
11 ms |
23764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
607 ms |
38652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
23780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23684 KB |
Output is correct |
2 |
Correct |
11 ms |
23796 KB |
Output is correct |
3 |
Correct |
106 ms |
31796 KB |
Output is correct |
4 |
Correct |
154 ms |
27076 KB |
Output is correct |
5 |
Correct |
369 ms |
34984 KB |
Output is correct |
6 |
Correct |
405 ms |
36172 KB |
Output is correct |
7 |
Correct |
409 ms |
36196 KB |
Output is correct |
8 |
Correct |
356 ms |
37708 KB |
Output is correct |
9 |
Correct |
901 ms |
38652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Incorrect |
11 ms |
23764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |