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;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
using vi = vector<int>;
using ll = long long;
const int mxN = (int)2e5+10;
const ll LINF = (ll)1e18;
int n, q;
ll mxsuf[mxN], mnsuf[mxN], pref[mxN];
array<ll,3> seg[mxN*2], Init={LINF,-LINF, 0ll};
void apply(int p, ll a, ll b, ll c){
seg[p][0] = min(a, max(seg[p][0]+c, b));
seg[p][1] = max(seg[p][1]+c, b);
seg[p][2] += c;
}
void prop(int p, int l, int r){
if(l==r or seg[p]==Init) return;
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
auto [a,b,c] = seg[p]; // push down
apply(p+1,a,b,c), apply(rp,a,b,c); seg[p] = Init;
}
void upd(int i, int j, ll a, ll b, ll c, int p=0, int l=0, int r=n-1){
if(i>j or i>r or j<l) return;
if(i<=l and r<=j){ apply(p,a,b,c); return; }
prop(p,l,r); // propagate to preserve order
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
upd(i,j,a,b,c,p+1,l,mid); upd(i,j,a,b,c,rp,mid+1,r);
}
ll query(int i, int p=0, int l=0, int r=n-1){
if(l==r) return min(seg[p][0],max(seg[p][2],seg[p][1]));
prop(p,l,r); // propagate to preserve order
int mid = (l+r)/2; int rp = p+2*(mid-l+1);
if(i<=mid) return query(i,p+1,l,mid);
return query(i,rp,mid+1,r);
}
vi distribute_candies(vi c, vi l, vi r, vi v){
n = sz(c), q = sz(l);
vector<int> ans(n,0);
fill(seg, seg+n*2,Init);
for(int i = 0; i < q; i++){
upd(l[i],r[i],LINF,-LINF,v[i]);
upd(l[i],r[i],c[l[i]],-LINF,0);
upd(l[i],r[i],LINF,0,0);
}
for(int i = 0; i < n; i++) ans[i]=query(i);
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... |