#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MX = 200005;
struct segtree{
int n;
ll suffmn[MX * 2], suffmx[MX * 2], sm[MX * 2], vals[MX];
segtree(int x) : n(x){
memset(suffmn, 0, sizeof(suffmn));
memset(suffmx, 0, sizeof(suffmx));
memset(sm, 0, sizeof(sm));
memset(vals, 0, sizeof(vals));
}
#define md (cl + cr) / 2
#define lc nd + 1
#define rc nd + 2 * (md - cl + 1)
void update(int nd, int cl, int cr, int ps, ll val){
if(ps < cl || cr < ps) return;
if(cl == cr){
suffmn[nd] = suffmx[nd] = sm[nd] = val;
return;
}
update(lc, cl, md, ps, val);
update(rc, md + 1, cr, ps, val);
suffmn[nd] = min(suffmn[lc] + sm[rc], suffmn[rc]);
suffmx[nd] = max(suffmx[lc] + sm[rc], suffmx[rc]);
sm[nd] = sm[lc] + sm[rc];
}
void upd(int ps, ll val){
vals[ps] = val;
update(1, 0, n - 1, ps, val);
}
// smallest suff s.t. mx - mn > c
pair<pair<ll, ll>, ll> query(int nd, int cl, int cr, ll sfmn, ll sfmx, ll sfsm, int c){
if(cl == cr)
return {{min(sfmn, sfsm + suffmn[nd]), max(sfmx, sfsm + suffmx[nd])}, cl};
ll nwsfmn = min(sfmn, sfsm + suffmn[rc]);
ll nwsfmx = max(sfmx, sfsm + suffmx[rc]);
ll nwsfsm = sfsm + sm[rc];
if(nwsfmx - nwsfmn > c)
return query(rc, md + 1, cr, sfmn, sfmx, sfsm, c);
return query(lc, cl, md, nwsfmn, nwsfmx, nwsfsm, c);
}
pair<pair<ll, ll>, ll> que(int c){
return query(1, 0, n - 1, 0, 0, 0, c);
}
int tp(int x){
return vals[x];
}
};
vector<pair<int, int> > queriesL[MX], queriesR[MX];
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v){
int n = c.size(), q = v.size();
vector<int> ans(n);
segtree st(q);
for(int i = 0; i < q; i ++){
queriesL[l[i]].push_back({i, v[i]});
queriesR[r[i]].push_back({i, v[i]});
}
for(int i = 0; i < n; i ++){
for(pair<int, int> j : queriesL[i])
st.upd(j.first, -j.second);
auto gt = st.que(c[i]);
ll mn = gt.first.first, mx = gt.first.second, id = gt.second;
if(mx - mn <= c[i] || st.tp(id) >= 0) ans[i] = -mn;
else ans[i] = c[i] - mx;
for(pair<int, int> j : queriesR[i])
st.upd(j.first, 0);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
12 ms |
20556 KB |
Output is correct |
3 |
Correct |
14 ms |
20680 KB |
Output is correct |
4 |
Correct |
13 ms |
20684 KB |
Output is correct |
5 |
Correct |
14 ms |
20748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
535 ms |
35640 KB |
Output is correct |
2 |
Correct |
535 ms |
35728 KB |
Output is correct |
3 |
Correct |
496 ms |
35656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
20556 KB |
Output is correct |
2 |
Correct |
279 ms |
30868 KB |
Output is correct |
3 |
Correct |
80 ms |
24280 KB |
Output is correct |
4 |
Correct |
473 ms |
35524 KB |
Output is correct |
5 |
Correct |
495 ms |
36916 KB |
Output is correct |
6 |
Correct |
495 ms |
36932 KB |
Output is correct |
7 |
Correct |
464 ms |
36804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
14 ms |
20556 KB |
Output is correct |
3 |
Correct |
135 ms |
28640 KB |
Output is correct |
4 |
Correct |
77 ms |
23220 KB |
Output is correct |
5 |
Correct |
203 ms |
31056 KB |
Output is correct |
6 |
Correct |
207 ms |
31144 KB |
Output is correct |
7 |
Correct |
209 ms |
31052 KB |
Output is correct |
8 |
Correct |
201 ms |
31048 KB |
Output is correct |
9 |
Correct |
207 ms |
31148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
20556 KB |
Output is correct |
2 |
Correct |
12 ms |
20556 KB |
Output is correct |
3 |
Correct |
14 ms |
20680 KB |
Output is correct |
4 |
Correct |
13 ms |
20684 KB |
Output is correct |
5 |
Correct |
14 ms |
20748 KB |
Output is correct |
6 |
Correct |
535 ms |
35640 KB |
Output is correct |
7 |
Correct |
535 ms |
35728 KB |
Output is correct |
8 |
Correct |
496 ms |
35656 KB |
Output is correct |
9 |
Correct |
13 ms |
20556 KB |
Output is correct |
10 |
Correct |
279 ms |
30868 KB |
Output is correct |
11 |
Correct |
80 ms |
24280 KB |
Output is correct |
12 |
Correct |
473 ms |
35524 KB |
Output is correct |
13 |
Correct |
495 ms |
36916 KB |
Output is correct |
14 |
Correct |
495 ms |
36932 KB |
Output is correct |
15 |
Correct |
464 ms |
36804 KB |
Output is correct |
16 |
Correct |
12 ms |
20556 KB |
Output is correct |
17 |
Correct |
14 ms |
20556 KB |
Output is correct |
18 |
Correct |
135 ms |
28640 KB |
Output is correct |
19 |
Correct |
77 ms |
23220 KB |
Output is correct |
20 |
Correct |
203 ms |
31056 KB |
Output is correct |
21 |
Correct |
207 ms |
31144 KB |
Output is correct |
22 |
Correct |
209 ms |
31052 KB |
Output is correct |
23 |
Correct |
201 ms |
31048 KB |
Output is correct |
24 |
Correct |
207 ms |
31148 KB |
Output is correct |
25 |
Correct |
12 ms |
20556 KB |
Output is correct |
26 |
Correct |
83 ms |
24272 KB |
Output is correct |
27 |
Correct |
283 ms |
32264 KB |
Output is correct |
28 |
Correct |
484 ms |
36912 KB |
Output is correct |
29 |
Correct |
486 ms |
36956 KB |
Output is correct |
30 |
Correct |
492 ms |
37020 KB |
Output is correct |
31 |
Correct |
482 ms |
36924 KB |
Output is correct |
32 |
Correct |
485 ms |
37084 KB |
Output is correct |