#include "candies.h"
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::vector;
using std::string;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
using std::endl;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define ln "\n"
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
#define all(a) a.begin(),a.end()
struct segment_tree{
ll N;
ll log;
vi sum, max, min;
segment_tree(ll n){
N = 1;
log = 0;
while(N <= n){
N *= 2;
log++;
}
sum.resize(2*N);
max.resize(2*N);
min.resize(2*N);
}
void update(ll idx, ll val){
idx += N;
sum[idx] += val;
max[idx] = min[idx] = sum[idx];
idx >>= 1;
while(idx){
sum[idx] = sum[idx*2]+sum[idx*2+1];
max[idx] = std::max(max[idx*2],sum[idx*2]+max[idx*2+1]);
min[idx] = std::min(min[idx*2],sum[idx*2]+min[idx*2+1]);
idx >>= 1;
}
}
std::tuple<ll,ll,ll,ll> min_right(ll val, std::tuple<ll,ll,ll> state, ll now = 1, ll l = 0, ll r = -1){
if(r == -1) r = N;
ll s,ma,mi;
std::tie(s,ma,mi) = state;
if(std::max(ma,s+max[now])-std::min(mi,s+min[now]) < val){
return mtp(r, s+sum[now], std::max(ma,s+max[now]), std::min(mi,s+min[now]));
}
if(now >= N) return mtp(l, s+sum[now], std::max(ma,s+max[now]), std::min(mi,s+min[now]));
ll left,sl,mal,mil;
std::tie(left,sl,mal,mil) = min_right(val, state, now*2, l, (l+r)/2);
if(left == (l+r)/2) return min_right(val, mtp(sl, mal, mil), now*2+1, (l+r)/2, r);
return mtp(left, sl, mal, mil);
}
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int N = c.size();
ll Q = l.size();
vii add(N+1), dec(N+1);
reverse(all(l));
reverse(all(r));
reverse(all(v));
rep(i,0,Q){
add[l[i]].pb(i);
dec[r[i]+1].pb(i);
}
segment_tree seg(Q);
vector<int> ans(N);
rep(i,0,N){
for(auto j: add[i]) seg.update(j, v[j]);
for(auto j: dec[i]) seg.update(j, -v[j]);
ll idx, sum, max, min;
std::tie(idx, sum, max, min) = seg.min_right(c[i], mtp(0,0,0));
if(idx >= Q) ans[i] = max;
else if(v[idx] > 0){
ans[i] = c[i]+min;
}
else{
ans[i] = max;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
436 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
37008 KB |
Output is correct |
2 |
Correct |
298 ms |
37012 KB |
Output is correct |
3 |
Correct |
273 ms |
36964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
125 ms |
22908 KB |
Output is correct |
3 |
Correct |
69 ms |
14400 KB |
Output is correct |
4 |
Correct |
279 ms |
42804 KB |
Output is correct |
5 |
Correct |
282 ms |
43348 KB |
Output is correct |
6 |
Correct |
273 ms |
43612 KB |
Output is correct |
7 |
Correct |
260 ms |
43048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
67 ms |
20708 KB |
Output is correct |
4 |
Correct |
64 ms |
13364 KB |
Output is correct |
5 |
Correct |
135 ms |
35640 KB |
Output is correct |
6 |
Correct |
147 ms |
36368 KB |
Output is correct |
7 |
Correct |
142 ms |
36980 KB |
Output is correct |
8 |
Correct |
132 ms |
35516 KB |
Output is correct |
9 |
Correct |
148 ms |
37088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
436 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
285 ms |
37008 KB |
Output is correct |
7 |
Correct |
298 ms |
37012 KB |
Output is correct |
8 |
Correct |
273 ms |
36964 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
125 ms |
22908 KB |
Output is correct |
11 |
Correct |
69 ms |
14400 KB |
Output is correct |
12 |
Correct |
279 ms |
42804 KB |
Output is correct |
13 |
Correct |
282 ms |
43348 KB |
Output is correct |
14 |
Correct |
273 ms |
43612 KB |
Output is correct |
15 |
Correct |
260 ms |
43048 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
67 ms |
20708 KB |
Output is correct |
19 |
Correct |
64 ms |
13364 KB |
Output is correct |
20 |
Correct |
135 ms |
35640 KB |
Output is correct |
21 |
Correct |
147 ms |
36368 KB |
Output is correct |
22 |
Correct |
142 ms |
36980 KB |
Output is correct |
23 |
Correct |
132 ms |
35516 KB |
Output is correct |
24 |
Correct |
148 ms |
37088 KB |
Output is correct |
25 |
Correct |
0 ms |
296 KB |
Output is correct |
26 |
Correct |
54 ms |
13496 KB |
Output is correct |
27 |
Correct |
114 ms |
25644 KB |
Output is correct |
28 |
Correct |
271 ms |
41564 KB |
Output is correct |
29 |
Correct |
267 ms |
41956 KB |
Output is correct |
30 |
Correct |
267 ms |
42028 KB |
Output is correct |
31 |
Correct |
279 ms |
42444 KB |
Output is correct |
32 |
Correct |
297 ms |
42544 KB |
Output is correct |