#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=2e5+5;
ll sm;
vector<vector<pair<int, int>>> d(nx);
struct node
{
ll mx, mn, mxidx, mnidx, lz;
node(int idx): mx(0), mn(0), mxidx(idx), mnidx(idx), lz(0){}
node(): lz(0){}
};
struct segtree
{
node d[4*nx];
node merge(node &a, node &b)
{
node res=node(0);
res.mx=max(a.mx, b.mx);
res.mn=min(a.mn, b.mn);
res.mxidx=a.mx>=b.mx?a.mxidx:b.mxidx;
res.mnidx=a.mn<=b.mn?a.mnidx:b.mnidx;
res.lz=0;
return res;
}
void build(int l, int r, int i)
{
if (l==r) return void(d[i]=node(l));
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
d[i]=merge(d[2*i], d[2*i+1]);
//cout<<d[i].mxidx<<' '<<d[i].mnidx<<'\n';
}
void pushlz(int l, int r, int i)
{
d[i].mx+=d[i].lz; d[i].mn+=d[i].lz;
if (l==r) return void(d[i].lz=0);
d[2*i].lz+=d[i].lz;
d[2*i+1].lz+=d[i].lz;
d[i].lz=0;
}
void update(int l, int r, int i, int ql, int v)
{
pushlz(l, r, i);
if (r<ql) return;
if (ql<=l) return d[i].lz+=v, pushlz(l, r, i), void();
int md=(l+r)/2;
update(l, md, 2*i, ql, v);
update(md+1, r, 2*i+1, ql, v);
d[i]=merge(d[2*i], d[2*i+1]);
}
node query(int l, int r, int i, ll c, node ans)
{
pushlz(l, r, i);
if (l==r) return merge(d[i], ans);
int md=(l+r)/2;
pushlz(l, md, 2*i); pushlz(md+1, r, 2*i+1);
node tmp=merge(ans, d[2*i+1]);
if (tmp.mx-tmp.mn>=c) return query(md+1, r, 2*i+1, c, ans);
return query(l, md, 2*i, c, tmp);
}
} s;
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
int n=c.size(), t=l.size();
d[0].push_back({1, 1e9+5});
d[0].push_back({2, -1e9-5});
for (int i=1; i<=t; i++)
{
d[l[i-1]].push_back({i+2, v[i-1]});
d[r[i-1]+1].push_back({i+2, -v[i-1]});
}
vector<int> ans(n);
s.build(1, t+2, 1);
for (int i=0; i<n; i++)
{
for (auto [x, y]:d[i]) sm+=y, s.update(1, t+2, 1, x, y);
node tmp(-1);
tmp.mx=LLONG_MIN; tmp.mn=LLONG_MAX;
node res=s.query(1, t+2, 1, c[i], tmp);
//cout<<t+2<<' '<<i<<' '<<c[i]<<' '<<res.mx<<' '<<res.mn<<' '<<res.mxidx<<' '<<res.mnidx<<'\n';
if (res.mnidx<=res.mxidx) ans[i]=max(0ll, c[i]-(res.mx-sm));
else ans[i]=max(0ll, sm-res.mn);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
36444 KB |
Output is correct |
2 |
Correct |
5 ms |
36364 KB |
Output is correct |
3 |
Correct |
7 ms |
36440 KB |
Output is correct |
4 |
Correct |
7 ms |
36444 KB |
Output is correct |
5 |
Correct |
7 ms |
36444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
50528 KB |
Output is correct |
2 |
Correct |
338 ms |
54608 KB |
Output is correct |
3 |
Correct |
346 ms |
54608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
36444 KB |
Output is correct |
2 |
Correct |
215 ms |
48980 KB |
Output is correct |
3 |
Correct |
75 ms |
42180 KB |
Output is correct |
4 |
Correct |
363 ms |
56208 KB |
Output is correct |
5 |
Correct |
378 ms |
56844 KB |
Output is correct |
6 |
Correct |
349 ms |
57172 KB |
Output is correct |
7 |
Correct |
333 ms |
56568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
36440 KB |
Output is correct |
2 |
Correct |
6 ms |
36444 KB |
Output is correct |
3 |
Correct |
101 ms |
47256 KB |
Output is correct |
4 |
Correct |
69 ms |
40196 KB |
Output is correct |
5 |
Correct |
184 ms |
50156 KB |
Output is correct |
6 |
Correct |
177 ms |
50816 KB |
Output is correct |
7 |
Correct |
176 ms |
51196 KB |
Output is correct |
8 |
Correct |
178 ms |
50108 KB |
Output is correct |
9 |
Correct |
185 ms |
51380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
36444 KB |
Output is correct |
2 |
Correct |
5 ms |
36364 KB |
Output is correct |
3 |
Correct |
7 ms |
36440 KB |
Output is correct |
4 |
Correct |
7 ms |
36444 KB |
Output is correct |
5 |
Correct |
7 ms |
36444 KB |
Output is correct |
6 |
Correct |
346 ms |
50528 KB |
Output is correct |
7 |
Correct |
338 ms |
54608 KB |
Output is correct |
8 |
Correct |
346 ms |
54608 KB |
Output is correct |
9 |
Correct |
6 ms |
36444 KB |
Output is correct |
10 |
Correct |
215 ms |
48980 KB |
Output is correct |
11 |
Correct |
75 ms |
42180 KB |
Output is correct |
12 |
Correct |
363 ms |
56208 KB |
Output is correct |
13 |
Correct |
378 ms |
56844 KB |
Output is correct |
14 |
Correct |
349 ms |
57172 KB |
Output is correct |
15 |
Correct |
333 ms |
56568 KB |
Output is correct |
16 |
Correct |
6 ms |
36440 KB |
Output is correct |
17 |
Correct |
6 ms |
36444 KB |
Output is correct |
18 |
Correct |
101 ms |
47256 KB |
Output is correct |
19 |
Correct |
69 ms |
40196 KB |
Output is correct |
20 |
Correct |
184 ms |
50156 KB |
Output is correct |
21 |
Correct |
177 ms |
50816 KB |
Output is correct |
22 |
Correct |
176 ms |
51196 KB |
Output is correct |
23 |
Correct |
178 ms |
50108 KB |
Output is correct |
24 |
Correct |
185 ms |
51380 KB |
Output is correct |
25 |
Correct |
6 ms |
36440 KB |
Output is correct |
26 |
Correct |
77 ms |
40168 KB |
Output is correct |
27 |
Correct |
207 ms |
48212 KB |
Output is correct |
28 |
Correct |
341 ms |
54864 KB |
Output is correct |
29 |
Correct |
359 ms |
55468 KB |
Output is correct |
30 |
Correct |
366 ms |
55848 KB |
Output is correct |
31 |
Correct |
358 ms |
55820 KB |
Output is correct |
32 |
Correct |
355 ms |
56144 KB |
Output is correct |