#include<bits/stdc++.h>
#include"candies.h"
using namespace std;
using ll = long long;
struct segtree {
struct node {
ll s{}, mx{}, mn{};
node operator+(const node& o) {
return node{s + o.s, max(o.mx, o.s + mx), min(o.mn, o.s + mn)};
}
};
int n;
vector<node> t;
segtree(int m) : n(m), t(m*4) {}
void upd(int i, int x) {
t[i += n] = node {x, max(0, x), min(0, x)};
for (i /= 2; i >= 1; i /= 2)
t[i] = t[i*2] + t[i*2+1];
}
node query(int l, int r) {
node resl, resr;
l += n; r += n;
while (l <=r) {
if (l%2==1) resl = resl + t[l++];
if (r%2==0) resr = t[r--] + resr;
l /= 2; r /= 2;
}
return resl + resr;
}
};
vector<int> distribute_candies(vector<int> c, vector<int> l,
vector<int> r, vector<int> v) {
int n = c.size();
int m = (int)v.size();
vector<vector<int>> in(n), out(n);
for (int i = 0; i < m; i++) {
in[l[i]].push_back(i);
out[r[i]].push_back(i);
}
segtree vcur(m);
for(int i = 0; i < n; i ++){
for (int j : in[i]) {
vcur.upd(j, -v[j]);
}
int l = 0, r = m;
while(l <= r){
int mid = (l + r) >> 1;
auto [s, mx, mn] = vcur.query(mid, m-1);
if(mx - mn >= c[i]) {
l = mid + 1;
}else r = mid - 1;
}
r ++;
if(r == 0 || v[r - 1] < 0){
auto [s, mx, mn] = vcur.query(r, m-1);
c[i] = -mn;
}else{
auto [s, mx, mn] = vcur.query(r, m-1);
c[i] = c[i] - mx;
}
for (int j : out[i]) {
vcur.upd(j, 0);
}
}
//for(auto u : suf) cout << u << ' ';cout << endl;
return c;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
452 ms |
41956 KB |
Output is correct |
2 |
Correct |
488 ms |
45964 KB |
Output is correct |
3 |
Correct |
472 ms |
45844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
142 ms |
26800 KB |
Output is correct |
3 |
Correct |
111 ms |
11684 KB |
Output is correct |
4 |
Correct |
440 ms |
41932 KB |
Output is correct |
5 |
Correct |
492 ms |
48244 KB |
Output is correct |
6 |
Correct |
450 ms |
48652 KB |
Output is correct |
7 |
Correct |
361 ms |
47972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
88 ms |
25672 KB |
Output is correct |
4 |
Correct |
96 ms |
11516 KB |
Output is correct |
5 |
Correct |
256 ms |
36548 KB |
Output is correct |
6 |
Correct |
251 ms |
37036 KB |
Output is correct |
7 |
Correct |
210 ms |
36924 KB |
Output is correct |
8 |
Correct |
271 ms |
37020 KB |
Output is correct |
9 |
Correct |
299 ms |
37040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
724 KB |
Output is correct |
6 |
Correct |
452 ms |
41956 KB |
Output is correct |
7 |
Correct |
488 ms |
45964 KB |
Output is correct |
8 |
Correct |
472 ms |
45844 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
142 ms |
26800 KB |
Output is correct |
11 |
Correct |
111 ms |
11684 KB |
Output is correct |
12 |
Correct |
440 ms |
41932 KB |
Output is correct |
13 |
Correct |
492 ms |
48244 KB |
Output is correct |
14 |
Correct |
450 ms |
48652 KB |
Output is correct |
15 |
Correct |
361 ms |
47972 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
88 ms |
25672 KB |
Output is correct |
19 |
Correct |
96 ms |
11516 KB |
Output is correct |
20 |
Correct |
256 ms |
36548 KB |
Output is correct |
21 |
Correct |
251 ms |
37036 KB |
Output is correct |
22 |
Correct |
210 ms |
36924 KB |
Output is correct |
23 |
Correct |
271 ms |
37020 KB |
Output is correct |
24 |
Correct |
299 ms |
37040 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
104 ms |
12716 KB |
Output is correct |
27 |
Correct |
140 ms |
29344 KB |
Output is correct |
28 |
Correct |
409 ms |
46492 KB |
Output is correct |
29 |
Correct |
421 ms |
46880 KB |
Output is correct |
30 |
Correct |
447 ms |
47148 KB |
Output is correct |
31 |
Correct |
370 ms |
47264 KB |
Output is correct |
32 |
Correct |
389 ms |
47564 KB |
Output is correct |