#include "candies.h"
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define MASK(i) (1LL<<(i))
#define BIT(mask,i) (((mask) >> (i))&1)
namespace trung{
const ll block = 450;
const ll MAXN = 2e5+100;
const ll INF = 1e18;
pll nxt[MAXN][2];
struct event{
ll x,i,v;
};
ll m,n;
namespace segtree{
struct node{
pll min1,max1;
};
const node worst = {MP(INF,-1),MP(-INF,-1)};
node tree[MAXN*4];
ll lazy[MAXN*4];
void push(ll id,ll v){
lazy[id]+=v;
tree[id].min1.fi += v;
tree[id].max1.fi += v;
}
void lz(ll id){
push(id<<1,lazy[id]);
push(id<<1|1,lazy[id]);
lazy[id] = 0;
}
node combine(node x,node y){
node res;
if (y.min1.fi < x.min1.fi)res.min1=y.min1;
else res.min1 = x.min1;
if (y.max1.fi > x.max1.fi)res.max1=y.max1;
else res.max1 = x.max1;
return res;
}
void build(ll id = 1,ll l = 0,ll r = m){
tree[id] = {MP(0,l),MP(0,l)};
if (l < r){
ll mid = (l + r) >> 1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
}
}
void update(ll i,ll v,ll id=1,ll l = 0,ll r = m){
// cout<<id<<' '<<l<<' '<<r<<endl;
if (r < i)return;
if (i <= l){
push(id,v);
return;
}
lz(id);
ll mid = (l + r) >> 1;
update(i,v,id<<1,l,mid);
update(i,v,id<<1|1,mid+1,r);
tree[id] = combine(tree[id<<1],tree[id<<1|1]);
}
node get(ll l1,ll r1,ll id = 1,ll l = 0,ll r = m){
if (l > r1 || l1 > r)return worst;
if (l1 <= l && r <= r1){
return tree[id];
}
lz(id);
ll mid = (l + r) >> 1;
return combine(get(l1,r1,id<<1,l,mid),get(l1,r1,id<<1|1,mid+1,r));
}
}
}
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
using namespace trung;
n = c.size();
m = sz(l);
vector <event> all;
for (ll i = 0;i < m;i ++){
all.push_back({l[i],i+1,v[i]});
all.push_back({r[i]+1,i+1,-v[i]});
}
sort(all.begin(),all.end(),[](event a1,event a2){return a1.x < a2.x;});
using namespace segtree;
build();
vector <int> res(n);
for (ll i = 0,ptr = 0;i < n;i ++){
while (ptr < sz(all) && all[ptr].x == i){
update(all[ptr].i,all[ptr].v);
ptr++;
}
pll cur = get(0,m).min1;
ll state = 0;
while (cur.se < m){
// cout<<cur.fi<<' '<<cur.se<<endl;
pll tmp;
if (state == 0){
tmp = get(cur.se+1,m).max1;
}
else tmp = get(cur.se+1,m).min1;
if (abs(cur.fi-tmp.fi) <= c[i])break;
cur = tmp;
state = 1-state;
}
res[i] = get(m,m).min1.fi - cur.fi + (state ? c[i] : 0);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6656 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
343 ms |
47188 KB |
Output is correct |
2 |
Correct |
332 ms |
47512 KB |
Output is correct |
3 |
Correct |
309 ms |
45744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
199 ms |
43988 KB |
Output is correct |
3 |
Correct |
111 ms |
12292 KB |
Output is correct |
4 |
Correct |
438 ms |
47276 KB |
Output is correct |
5 |
Correct |
378 ms |
48060 KB |
Output is correct |
6 |
Correct |
463 ms |
48728 KB |
Output is correct |
7 |
Correct |
251 ms |
48108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4528 KB |
Output is correct |
3 |
Correct |
98 ms |
42416 KB |
Output is correct |
4 |
Correct |
88 ms |
10328 KB |
Output is correct |
5 |
Correct |
318 ms |
46264 KB |
Output is correct |
6 |
Correct |
306 ms |
47440 KB |
Output is correct |
7 |
Correct |
295 ms |
46512 KB |
Output is correct |
8 |
Correct |
332 ms |
46416 KB |
Output is correct |
9 |
Correct |
200 ms |
46560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
6656 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6744 KB |
Output is correct |
6 |
Correct |
343 ms |
47188 KB |
Output is correct |
7 |
Correct |
332 ms |
47512 KB |
Output is correct |
8 |
Correct |
309 ms |
45744 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
199 ms |
43988 KB |
Output is correct |
11 |
Correct |
111 ms |
12292 KB |
Output is correct |
12 |
Correct |
438 ms |
47276 KB |
Output is correct |
13 |
Correct |
378 ms |
48060 KB |
Output is correct |
14 |
Correct |
463 ms |
48728 KB |
Output is correct |
15 |
Correct |
251 ms |
48108 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4528 KB |
Output is correct |
18 |
Correct |
98 ms |
42416 KB |
Output is correct |
19 |
Correct |
88 ms |
10328 KB |
Output is correct |
20 |
Correct |
318 ms |
46264 KB |
Output is correct |
21 |
Correct |
306 ms |
47440 KB |
Output is correct |
22 |
Correct |
295 ms |
46512 KB |
Output is correct |
23 |
Correct |
332 ms |
46416 KB |
Output is correct |
24 |
Correct |
200 ms |
46560 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
91 ms |
10432 KB |
Output is correct |
27 |
Correct |
186 ms |
39864 KB |
Output is correct |
28 |
Correct |
420 ms |
45488 KB |
Output is correct |
29 |
Correct |
426 ms |
45144 KB |
Output is correct |
30 |
Correct |
431 ms |
46356 KB |
Output is correct |
31 |
Correct |
508 ms |
46516 KB |
Output is correct |
32 |
Correct |
447 ms |
47532 KB |
Output is correct |