This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
struct dataa{
long long Min = 0,Max = 0,v = 0,lazy = 0;
void add(long long left,long long right,long long val){
v += val;
lazy += val;
Min +=val;
Max +=val;
}
};
struct Segment_Tree{
vector<dataa>tree;
void init(long long n){
tree.resize(2*n - 1);
}
dataa combine(dataa left,dataa right){
dataa res;
res.Min = min(left.Min,right.Min);
res.v = left.v + right.v;
res.Max = max(left.Max,right.Max);
return res;
}
void push(long long node,long long left,long long right){
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (tree[node].lazy!=0){
tree[node + 1].add(left,mid,tree[node].lazy);
tree[z].add(mid + 1,right,tree[node].lazy);
tree[node].lazy = 0;
}
}
dataa query(long long node,long long left,long long right,long long qleft,long long qright){
if (qright<left|| qleft > right)return {LLONG_MAX,LLONG_MIN,0};
if (qleft<=left && qright>=right){
return tree[node];
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
return combine(query(node + 1,left,mid,qleft,qright),query(z,mid + 1,right,qleft,qright));
}
void update(long long node,long long left,long long right,long long uleft,long long uright,long long v){
if (left > uright || right < uleft) return;
if (uleft <= left && right <=uright){
tree[node].add(left,right,v);
return;
}
push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (uleft<=mid){
update(node + 1,left,mid,uleft,uright,v);
}
if (uright > mid){
update(z,mid + 1,right,uleft,uright,v);
}
tree[node] = combine(tree[node + 1],tree[z]);
}
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
std::vector<int> r, std::vector<int> v) {
int n = (int)c.size();
vector<int>ans(n);
int m = (int)l.size();
vector<vector<pair<long long,long long>>>segment(n + 1);
for (int i = 0;i<m;++i){
segment[l[i]].push_back({v[i],i});
segment[r[i] + 1].push_back({-v[i],i});
}
Segment_Tree st;
st.init(m + 1);
for (int i = 0;i<n;++i){
for (auto x:segment[i]){
st.update(0,0,m,x.second + 1,m,x.first);
}
int left = 0,right = m;
int p = -1;
dataa pos;
while(left<=right){
int mid = (left + right)>>1;
auto temp = st.query(0,0,m,mid,m);
if (temp.Max - temp.Min >= c[i]){
left = mid + 1;
p = mid;
pos = temp;
}
else right = mid - 1;
}
long long sum = st.query(0,0,m,m,m).v;
// cout<<sum<<" "<<pos.Min<<" "<<pos.Max<<" "<<pos.v<<'\n';
if (p == -1){
ans[i] = sum - st.query(0,0,m,0,m).Min;
}
else if (pos.Min == st.query(0,0,m,p,p).v){
ans[i] = max(0LL,sum - pos.Max + c[i]);
}
else{
ans[i] = max(0LL,sum - pos.Min);
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |