#include "candies.h"
#include <bits/stdc++.h>
#define SIZE 200001
using namespace std;
typedef long long ll;
ll lazy[4*SIZE], max_tree[4*SIZE], min_tree[4*SIZE];
vector<pair<int, int> > query[SIZE];
void pro(int node)
{
lazy[node*2]+=lazy[node];
lazy[node*2+1]+=lazy[node];
max_tree[node*2]+=lazy[node];
max_tree[node*2+1]+=lazy[node];
min_tree[node*2]+=lazy[node];
min_tree[node*2+1]+=lazy[node];
lazy[node]=0;
}
void upd(int s, int e, int w, int l, int r, int node)
{
if (l>r) return;
if (s<=l && r<=e) {
lazy[node]+=w;
max_tree[node]+=w;
min_tree[node]+=w;
return;
}
if (l>e || s>r) return;
pro(node);
int mid=(l+r)/2;
upd(s, e, w, l, mid, node*2);
upd(s, e, w, mid+1, r, node*2+1);
max_tree[node]=max(max_tree[node*2], max_tree[node*2+1]);
min_tree[node]=min(min_tree[node*2], min_tree[node*2+1]);
}
ll get_max(int s, int e, int l, int r, int node)
{
if (l>r) return -1e18;
if (s<=l && r<=e) return max_tree[node];
if (l>e || s>r) return -1e18;
int mid=(l+r)/2;
pro(node);
return max(get_max(s, e, l, mid, node*2), get_max(s, e, mid+1, r, node*2+1));
}
ll get_min(int s, int e, int l, int r, int node)
{
if (l>r) return 1e18;
if (s<=l && r<=e) return min_tree[node];
if (l>e || s>r) return 1e18;
int mid=(l+r)/2;
pro(node);
return min(get_min(s, e, l, mid, node*2), get_min(s, e, mid+1, r, node*2+1));
}
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v)
{
int N=c.size(), M=l.size();
vector<int> ans(N);
for (int i=0; i<M; i++) {
query[l[i]].push_back(make_pair(v[i], i));
query[r[i]+1].push_back(make_pair(-v[i], i));
}
for (int i=0; i<N; i++) {
for (auto j : query[i]) upd(j.second+1, M, j.first, 0, M, 1);
if (get_max(0, M, 0, M, 1)-get_min(0, M, 0, M, 1)<=c[i]) ans[i]=get_max(M, M, 0, M, 1)-get_min(0, M, 0, M, 1);
else {
int s=0, e=M, mid;
while (1) {
if (s>=e) break;
mid=(s+e)/2;
if (get_max(mid, M, 0, M, 1)-get_min(mid, M, 0, M, 1)<=c[i]) e=mid;
else s=mid+1;
}
if (get_max(s-1, s-1, 0, M, 1)<get_min(s, M, 0, M, 1)) ans[i]=c[i]-get_max(s, M, 0, M, 1)+get_max(M, M, 0, M, 1);
else ans[i]=get_max(M, M, 0, M, 1)-get_min(s, M, 0, M, 1);
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
5144 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
13 ms |
5272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2894 ms |
34040 KB |
Output is correct |
2 |
Correct |
2658 ms |
33524 KB |
Output is correct |
3 |
Correct |
2514 ms |
33432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
364 ms |
28540 KB |
Output is correct |
3 |
Correct |
818 ms |
10736 KB |
Output is correct |
4 |
Correct |
2960 ms |
35444 KB |
Output is correct |
5 |
Correct |
2779 ms |
36172 KB |
Output is correct |
6 |
Correct |
2802 ms |
36200 KB |
Output is correct |
7 |
Correct |
2799 ms |
36292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
175 ms |
27224 KB |
Output is correct |
4 |
Correct |
667 ms |
8952 KB |
Output is correct |
5 |
Correct |
1871 ms |
30052 KB |
Output is correct |
6 |
Correct |
2245 ms |
30608 KB |
Output is correct |
7 |
Correct |
2462 ms |
31324 KB |
Output is correct |
8 |
Correct |
1764 ms |
29864 KB |
Output is correct |
9 |
Correct |
2577 ms |
31480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
5 ms |
5144 KB |
Output is correct |
4 |
Correct |
5 ms |
5144 KB |
Output is correct |
5 |
Correct |
13 ms |
5272 KB |
Output is correct |
6 |
Correct |
2894 ms |
34040 KB |
Output is correct |
7 |
Correct |
2658 ms |
33524 KB |
Output is correct |
8 |
Correct |
2514 ms |
33432 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
364 ms |
28540 KB |
Output is correct |
11 |
Correct |
818 ms |
10736 KB |
Output is correct |
12 |
Correct |
2960 ms |
35444 KB |
Output is correct |
13 |
Correct |
2779 ms |
36172 KB |
Output is correct |
14 |
Correct |
2802 ms |
36200 KB |
Output is correct |
15 |
Correct |
2799 ms |
36292 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
175 ms |
27224 KB |
Output is correct |
19 |
Correct |
667 ms |
8952 KB |
Output is correct |
20 |
Correct |
1871 ms |
30052 KB |
Output is correct |
21 |
Correct |
2245 ms |
30608 KB |
Output is correct |
22 |
Correct |
2462 ms |
31324 KB |
Output is correct |
23 |
Correct |
1764 ms |
29864 KB |
Output is correct |
24 |
Correct |
2577 ms |
31480 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
485 ms |
8844 KB |
Output is correct |
27 |
Correct |
345 ms |
28556 KB |
Output is correct |
28 |
Correct |
1886 ms |
34660 KB |
Output is correct |
29 |
Correct |
2541 ms |
35104 KB |
Output is correct |
30 |
Correct |
2671 ms |
35388 KB |
Output is correct |
31 |
Correct |
2857 ms |
35444 KB |
Output is correct |
32 |
Correct |
2814 ms |
35432 KB |
Output is correct |