#include "candies.h"
#include<bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
constexpr int sz = 2e5 + 5;
constexpr long long inf = (long long)1E18 + 7;
int n, Q;
vector<int> a;
struct Node{
long long sm, mn, mx;
} st[sz << 2];
long long lazy[sz<<2], lazy1[sz<<2];
Node operator+(Node i, Node j){
return {i.sm + j.sm, min(i.mn, j.mn), max(i.mx, j.mx)};
}
inline void build(int l, int r, int in){
if(l == r){
st[in] = {0ll, inf, -inf};
return;
}
int mid = l + ((r - l) >> 1);
build(l, mid, in << 1);
build(mid + 1, r, in << 1 | 1);
st[in] = st[in << 1] + st[in << 1 | 1];
}
inline void relax(int l, int r, int in){
if(l ^ r){
lazy[in<<1] += lazy[in], lazy[in<<1|1] += lazy[in];
}
st[in].sm += (r - l + 1) * lazy[in];
st[in].mn += (r - l + 1) * lazy[in];
st[in].mx += (r - l + 1) * lazy[in];
lazy[in] = 0;
return;
}
inline void relax1(int l, int r, int in){
if(l ^ r){
lazy1[in<<1] = lazy1[in<<1|1] = lazy1[in];
}
st[in].sm = (r - l + 1) * lazy1[in];
st[in].mn = (r - l + 1) * lazy1[in];
st[in].mx = (r - l + 1) * lazy1[in];
lazy1[in] = -1;
return;
}
inline void update(int l, int r, int in, int L, int R, int val){
if(lazy[in]) relax(l, r, in);
if(l > R || r < L || r < l) return;
if(l >= L && r <= R){
lazy[in] = val;
relax(l, r, in);
return;
}else{
int mid = l + ((r - l) >> 1);
update(l, mid, in << 1, L, R, val);
update(mid + 1, r, in << 1 | 1, L, R, val);
st[in] = st[in << 1] + st[in << 1 | 1];
}
return;
}
inline void update1(int l, int r, int in, int L, int R, int val){
if(lazy[in]) relax(l, r, in);
if(lazy1[in] != -1) relax1(l, r, in);
if(l > R || r < L || r < l) return;
if(l >= L && r <= R){
lazy1[in] = val;
relax1(l, r, in);
return;
}else{
int mid = l + ((r - l) >> 1);
update1(l, mid, in << 1, L, R, val);
update1(mid + 1, r, in << 1 | 1, L, R, val);
st[in] = st[in << 1] + st[in << 1 | 1];
}
return;
}
Node zro = {0ll, inf, -inf};
Node get_ans(int l, int r, int in, int L, int R){
if(lazy[in]) relax(l, r, in);
if(lazy1[in] != -1) relax1(l, r, in);
if(l > R || r < L || r < l) return zro;
if(l >= L && r <= R) return st[in];
int mid = l + ((r - l) >> 1);
return get_ans(l, mid, in << 1, L, R) + get_ans(mid + 1, r, in << 1 | 1, L, R);
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
n = (int)c.size(), Q = (int)r.size();
a.assign(n, 0);
int mn = *min_element(all(v));
//
// if(n <= 2000 && Q <= 2000){
//
// for(register int q = 0; q < Q; ++q){
//
// for(register int i = l[q]; i <= r[q]; ++i){
//
// if(v[q] > 0) a[i] = min(c[i], a[i] + v[q]);
// else a[i] = max(0, a[i] + v[q]);
//
// }
//
// }
//
// return a;
// }
memset(lazy1, -1, sizeof(lazy1));
//
// if(mn >= 0){
//
// for(register int q = 0; q < Q; ++q) update(1, n, 1, l[q] + 1, r[q] + 1, v[q]);
//
// for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
//
// return a;
//
// }
if(*max_element(all(c)) == *min_element(all(c))){
build(1, n, 1);
for(register int q = 0; q < Q; ++q){
int x = l[q] + 1, y = r[q] + 1, val = v[q];
Node cur = get_ans(1, n, 1, x, y);
if(cur.mx <= abs(val) && val < 0){
update1(1, n, 1, x, y, 0ll);
continue;
}
if(val > 0 && cur.mn + val > c[0]){
update1(1, n, 1, x, y, c[0]);
continue;
}
update(1, n, 1, x, y, val);
cur = get_ans(1, n, 1, x, y);
while(cur.mx > c[0]){
int L = x, R = y, mid, best;
while(L <= R){
mid = L + ((R - L) >> 1);
if(get_ans(1, n, 1, x, mid).mx > c[0]){
best = mid, R = mid - 1;
}else{
L = mid + 1;
}
}
int ll = best;
int LL = ll, RR = y;
while(LL <= RR){
mid = ((LL + RR) >> 1);
if(get_ans(1, n, 1, ll, mid).mn > c[0]){
LL = mid + 1, best = mid;
}else{
RR = mid - 1;
}
}
int rr = best;
update1(1, n, 1, ll, rr, c[0]);
cur = get_ans(1, n, 1, x, y);
}
while(cur.mn < 0ll){
int L = x, R = y, mid, best;
while(L <= R){
mid = L + ((R - L) >> 1);
if(get_ans(1, n, 1, x, mid).mn < 0ll){
best = mid, R = mid - 1;
}else{
L = mid + 1;
}
}
int ll = best;
int LL = ll, RR = y;
while(LL <= RR){
mid = ((LL + RR) >> 1);
if(get_ans(1, n, 1, ll, mid).mx < 0ll){
LL = mid + 1, best = mid;
}else{
RR = mid - 1;
}
}
int rr = best;
update1(1, n, 1, ll, rr, 0ll);
cur = get_ans(1, n, 1, x, y);
}
}
for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
return a;
}
assert(0);
}
Compilation message
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:149:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
149 | for(register int q = 0; q < Q; ++q){
| ^
candies.cpp:250:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
250 | for(register int i = 1; i <= n; ++i) a[i-1] = min((long long)c[i-1], get_ans(1, n, 1, i, i).sm);
| ^
candies.cpp:114:6: warning: unused variable 'mn' [-Wunused-variable]
114 | int mn = *min_element(all(v));
| ^~
candies.cpp:230:33: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
230 | if(get_ans(1, n, 1, ll, mid).mx < 0ll){
| ^
candies.cpp:192:33: warning: 'best' may be used uninitialized in this function [-Wmaybe-uninitialized]
192 | if(get_ans(1, n, 1, ll, mid).mn > c[0]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8804 KB |
Output is correct |
2 |
Runtime error |
7 ms |
13404 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
74 ms |
27628 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
8796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
9 ms |
13404 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8804 KB |
Output is correct |
2 |
Runtime error |
7 ms |
13404 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |