#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
const int mxN = 2e5 + 7;
const int SZ = exp2(ceil(log2(mxN)));
const ll INF = 1e18;
int n, q;
ll lazy[4*mxN];
pair<ll,int> seg[4*mxN][2];
vector<pair<int,int>> que[mxN][2];
pair<ll,int> minx(pair<ll,int> a, pair<ll,int> b) {
if (a.f == b.f) {
return (a.s > b.s ? a : b);
}
return (a.f < b.f ? a : b);
}
pair<ll,int> maxx(pair<ll,int> a, pair<ll,int> b) {
if (a.f == b.f) {
return (a.s > b.s ? a : b);
}
return (a.f > b.f ? a : b);
}
void push(int ind, int l, int r, int cnt = 1) {
seg[ind][0].f += lazy[ind];
seg[ind][1].f += lazy[ind];
if (l != r) {
lazy[2*ind] += lazy[ind];
lazy[2*ind+1] += lazy[ind];
if (cnt) {
int mid = (l+r)/2;
push(2*ind,l,mid,cnt-1);
push(2*ind+1,mid+1,r,cnt-1);
}
}
lazy[ind] = 0;
}
void update(int lo, int hi, ll inc, int ind=1, int l=0, int r=SZ-1) {
push(ind,l,r);
if (lo > r || l > hi) {
return;
}
if (lo <= l && r <= hi) {
lazy[ind] += inc;
push(ind,l,r);
return;
}
int mid = (l+r)/2;
update(lo,hi,inc,2*ind,l,mid);
update(lo,hi,inc,2*ind+1,mid+1,r);
seg[ind][0] = minx(seg[2*ind][0],seg[2*ind+1][0]);
seg[ind][1] = maxx(seg[2*ind][1],seg[2*ind+1][1]);
}
// {min, max}
pair<pair<ll,int>,pair<ll,int>>
walk(int c, pair<ll,int> mn, pair<ll,int> mx, int ind=1, int l=0, int r=SZ-1) {
push(ind,l,r);
if (l == r) {
return {minx(mn,seg[ind][0]),maxx(mx,seg[ind][1])};
}
int mid = (l+r)/2;
if (max(mx.f,seg[2*ind+1][1].f) - min(mn.f,seg[2*ind+1][0].f) >= c) {
return walk(c,mn,mx,2*ind+1,mid+1,r);
}
else {
return walk(c,min(mn,seg[2*ind+1][0]),max(mx,seg[2*ind+1][1]),
2*ind,l,mid);
}
}
ll query(int pos, int ind=1, int l=0, int r=SZ-1) {
push(ind,l,r);
if (l == r) {
return seg[ind][0].f;
}
int mid = (l+r)/2;
if (pos <= mid) {
return query(pos,2*ind,l,mid);
}
else {
return query(pos,2*ind+1,mid+1,r);
}
}
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
n = c.size(), q = v.size()+1;
seg[1+SZ][0] = seg[1+SZ][1] = {0, 1};
for (int i = 2; i <= q; ++i) {
que[l[i-2]][0].push_back({v[i-2], i});
que[r[i-2]][1].push_back({v[i-2], i});
seg[i+SZ][0] = seg[i+SZ][1] = {0, i};
}
for (int i = q+1; i < SZ; ++i) {
seg[i+SZ][0] = {INF, i};
seg[i+SZ][1] = {-INF, i};
}
for (int ind = SZ-1; ind >= 0; --ind) {
seg[ind][0] = minx(seg[2*ind][0],seg[2*ind+1][0]);
seg[ind][1] = maxx(seg[2*ind][1],seg[2*ind+1][1]);
}
vector<int> ans(n);
for (int i = 0; i < n; ++i) {
for (auto [val, t] : que[i][0]) {
update(t, q, val);
}
update(1,q,-c[i]);
/*
for (int j = 0; j <= q; ++j) {
cout << query(j) << ' ';
}
cout << "\n";
*/
auto cur = walk(c[i],{INF,0},{-INF,0});
//cout << cur.f.f << ' ' << cur.f.s << "\n";
//cout << cur.s.f << ' ' << cur.s.s << "\n----\n";
if (cur.s.s > cur.f.s) {
ans[i] = c[i] + query(q) - query(cur.s.s);
}
else {
ans[i] = query(q) - query(cur.f.s);
}
/*
int lb = 0, rb = q;
ll res = 0;
while (lb <= rb) {
int mb = (lb+rb)/2;
auto cur = query(mb,q);
if (cur.s.f - cur.f.f >= (ll) c[i]) {
lb = mb + 1;
if (cur.s.s > cur.f.s) {
res = c[i] + query(q,q).f.f - query(cur.s.s,cur.s.s).f.f;
}
else {
res = query(q,q).f.f - query(cur.f.s,cur.f.s).f.f;
}
}
else {
rb = mb - 1;
}
}
ans[i] = res;
*/
update(1,q,c[i]);
for (auto [val, t] : que[i][1]) {
update(t, q, -val);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29476 KB |
Output is correct |
5 |
Correct |
21 ms |
29528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1549 ms |
46908 KB |
Output is correct |
2 |
Correct |
1521 ms |
49220 KB |
Output is correct |
3 |
Correct |
1600 ms |
49212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
675 ms |
44000 KB |
Output is correct |
3 |
Correct |
637 ms |
34368 KB |
Output is correct |
4 |
Correct |
1546 ms |
50236 KB |
Output is correct |
5 |
Correct |
1542 ms |
50256 KB |
Output is correct |
6 |
Correct |
1512 ms |
50664 KB |
Output is correct |
7 |
Correct |
1465 ms |
50240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
8 ms |
29400 KB |
Output is correct |
3 |
Correct |
568 ms |
41444 KB |
Output is correct |
4 |
Correct |
621 ms |
32784 KB |
Output is correct |
5 |
Correct |
1363 ms |
44468 KB |
Output is correct |
6 |
Correct |
1293 ms |
44416 KB |
Output is correct |
7 |
Correct |
1306 ms |
44848 KB |
Output is correct |
8 |
Correct |
1304 ms |
44212 KB |
Output is correct |
9 |
Correct |
1315 ms |
44992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
13 ms |
29476 KB |
Output is correct |
5 |
Correct |
21 ms |
29528 KB |
Output is correct |
6 |
Correct |
1549 ms |
46908 KB |
Output is correct |
7 |
Correct |
1521 ms |
49220 KB |
Output is correct |
8 |
Correct |
1600 ms |
49212 KB |
Output is correct |
9 |
Correct |
8 ms |
29276 KB |
Output is correct |
10 |
Correct |
675 ms |
44000 KB |
Output is correct |
11 |
Correct |
637 ms |
34368 KB |
Output is correct |
12 |
Correct |
1546 ms |
50236 KB |
Output is correct |
13 |
Correct |
1542 ms |
50256 KB |
Output is correct |
14 |
Correct |
1512 ms |
50664 KB |
Output is correct |
15 |
Correct |
1465 ms |
50240 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
8 ms |
29400 KB |
Output is correct |
18 |
Correct |
568 ms |
41444 KB |
Output is correct |
19 |
Correct |
621 ms |
32784 KB |
Output is correct |
20 |
Correct |
1363 ms |
44468 KB |
Output is correct |
21 |
Correct |
1293 ms |
44416 KB |
Output is correct |
22 |
Correct |
1306 ms |
44848 KB |
Output is correct |
23 |
Correct |
1304 ms |
44212 KB |
Output is correct |
24 |
Correct |
1315 ms |
44992 KB |
Output is correct |
25 |
Correct |
6 ms |
29272 KB |
Output is correct |
26 |
Correct |
637 ms |
32792 KB |
Output is correct |
27 |
Correct |
676 ms |
43600 KB |
Output is correct |
28 |
Correct |
1531 ms |
49664 KB |
Output is correct |
29 |
Correct |
1499 ms |
49728 KB |
Output is correct |
30 |
Correct |
1515 ms |
49744 KB |
Output is correct |
31 |
Correct |
1504 ms |
49588 KB |
Output is correct |
32 |
Correct |
1548 ms |
50248 KB |
Output is correct |