#include <iostream>
#include "candies.h"
#include <cassert>
#include <cstdio>
#include <climits>
#include <vector>
using namespace std;
class segtree{
public:
struct node{
long long mini;
long long maxi;
long long lazy;
};
vector<node> tree;
int N;
void init(int n){
N = 1;
while(N <= n){
N*=2;
}
tree.assign(2 * N, {0, 0, 0});
}
void lazy_upd(int head){
tree[head].mini += tree[head].lazy;
tree[head].maxi += tree[head].lazy;
if(head < N){
tree[head * 2 + 1].lazy += tree[head].lazy;
tree[head * 2 + 2].lazy += tree[head].lazy;
}
tree[head].lazy = 0;
}
void update(int head, int L, int R, int l, int r, int val){
lazy_upd(head);
if(l <= L && R <= r){
tree[head].lazy = val;
lazy_upd(head);
return;
}
if(R <= l || r <= L){
return;
}
int mid = (L + R) / 2;
update(head * 2 + 1, L, mid, l, r, val);
update(head * 2 + 2, mid, R, l, r, val);
tree[head].mini = min(tree[head * 2 + 1].mini, tree[head * 2 + 2].mini);
tree[head].maxi = max(tree[head * 2 + 1].maxi, tree[head * 2 + 2].maxi);
}
void update(int l, int val){
update(0, 0, N, l, N, val);
}
long long getmin(int head, int L, int R, int l, int r){
lazy_upd(head);
if(l <= L && R <= r){
return tree[head].mini;
}
if(R <= l || r <= L){
return LLONG_MAX;
}
int mid = (L + R)/ 2;
return min(getmin(head * 2 + 1, L, mid, l, r), getmin(head * 2 + 2, mid, R, l, r));
}
long long getmin(int l){
return getmin(0, 0, N, l, N);
}
long long getmax(int head, int L, int R, int l, int r){
lazy_upd(head);
if(l <= L && R <= r){
return tree[head].maxi;
}
if(R <= l || r <= L){
return LLONG_MIN;
}
int mid = (L + R)/ 2;
return max(getmax(head * 2 + 1, L, mid, l, r), getmax(head * 2 + 2, mid, R, l, r));
}
long long getmax(int l){
return getmax(0, 0, N, l, N);
}
long long getpoint(int head, int L, int R, int x){
lazy_upd(head);
if(R - L == 1){
return tree[head].mini;
}
int mid = (L + R) / 2;
if(x < mid){
return getpoint(head * 2 + 1, L, mid, x);
}
else{
return getpoint(head * 2 + 2, mid, R, x);
}
}
long long getpoint(int x){
return getpoint(0, 0, N, x);
}
};
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> val){
int N = c.size();
int Q = l.size();
vector<int> ans(N);
segtree tree;
tree.init(Q + 5);
vector<vector<pair<int, int>>> op(N + 5);
for(int i = 0; i < Q; i ++){
op[l[i]].push_back({i + 2, val[i]});
op[r[i]+1].push_back({i + 2, -val[i]});
}
for(int i = 0; i < N; i ++){
for(auto[ll, rr] : op[i]){
tree.update(ll, rr);
}
int l = 0, r = Q + 1;
if(tree.getmax(0)-tree.getmin(0) < c[i]) { // easy case: range is small
ans[i] = tree.getpoint(Q+1) - tree.getmin(0);
continue;
}
while(l + 1 < r){
int mid = (l + r) / 2;
if(tree.getmax(mid)-tree.getmin(mid) > c[i]){
l = mid;
}
else{
r = mid;
}
}
//cout << tree.getpoint(l) << " " << tree.getpoint(Q + 1) << " " << l << endl;
long long a = tree.getpoint(l), b = tree.getpoint(Q + 1);
if(a < b){
int d = tree.getmax(l);
ans[i] = c[i] - (d - b);
}
else{
int d = tree.getmin(l);
ans[i] = b - d;
}
}
return ans;
}
# |
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 |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1346 ms |
31392 KB |
Output is correct |
2 |
Correct |
1283 ms |
31396 KB |
Output is correct |
3 |
Correct |
1125 ms |
31392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
188 ms |
21836 KB |
Output is correct |
3 |
Correct |
353 ms |
7616 KB |
Output is correct |
4 |
Correct |
1243 ms |
31396 KB |
Output is correct |
5 |
Correct |
1235 ms |
31400 KB |
Output is correct |
6 |
Correct |
1274 ms |
31388 KB |
Output is correct |
7 |
Correct |
1196 ms |
31396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
107 ms |
20696 KB |
Output is correct |
4 |
Correct |
291 ms |
7600 KB |
Output is correct |
5 |
Correct |
820 ms |
27724 KB |
Output is correct |
6 |
Correct |
942 ms |
27720 KB |
Output is correct |
7 |
Correct |
1045 ms |
27732 KB |
Output is correct |
8 |
Correct |
765 ms |
27728 KB |
Output is correct |
9 |
Correct |
1081 ms |
27732 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 |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
5 ms |
468 KB |
Output is correct |
6 |
Correct |
1346 ms |
31392 KB |
Output is correct |
7 |
Correct |
1283 ms |
31396 KB |
Output is correct |
8 |
Correct |
1125 ms |
31392 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
188 ms |
21836 KB |
Output is correct |
11 |
Correct |
353 ms |
7616 KB |
Output is correct |
12 |
Correct |
1243 ms |
31396 KB |
Output is correct |
13 |
Correct |
1235 ms |
31400 KB |
Output is correct |
14 |
Correct |
1274 ms |
31388 KB |
Output is correct |
15 |
Correct |
1196 ms |
31396 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
107 ms |
20696 KB |
Output is correct |
19 |
Correct |
291 ms |
7600 KB |
Output is correct |
20 |
Correct |
820 ms |
27724 KB |
Output is correct |
21 |
Correct |
942 ms |
27720 KB |
Output is correct |
22 |
Correct |
1045 ms |
27732 KB |
Output is correct |
23 |
Correct |
765 ms |
27728 KB |
Output is correct |
24 |
Correct |
1081 ms |
27732 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
211 ms |
7684 KB |
Output is correct |
27 |
Correct |
181 ms |
21744 KB |
Output is correct |
28 |
Correct |
884 ms |
31416 KB |
Output is correct |
29 |
Correct |
1058 ms |
31392 KB |
Output is correct |
30 |
Correct |
1173 ms |
31384 KB |
Output is correct |
31 |
Correct |
1260 ms |
31452 KB |
Output is correct |
32 |
Correct |
1243 ms |
31384 KB |
Output is correct |