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 <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a, b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a, b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
refs:
https://codeforces.com/blog/entry/92083?#comment-807860
*/
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
#include "candies.h"
template<typename T>
struct lazysegtree {
/*=======================================================*/
struct data {
ll mn,mnp,mx,mxp;
};
struct lazy {
ll a;
};
data d_neutral = {inf2,-1,-inf2,-1};
lazy l_neutral = {0};
void merge(data &curr, data &left, data &right) {
curr.mn = min(left.mn,right.mn);
curr.mx = max(left.mx,right.mx);
if(left.mn < right.mn) curr.mnp = left.mnp;
else curr.mnp = right.mnp;
if(left.mx > right.mx) curr.mxp = left.mxp;
else curr.mxp = right.mxp;
}
void create(int x, int lx, int rx, T v) {
tr[x].mn = tr[x].mx = 0;
tr[x].mnp = tr[x].mxp = lx;
}
void modify(int x, int lx, int rx, T v) {
lz[x].a = v;
}
void propagate(int x, int lx, int rx) {
ll v = lz[x].a;
if(!v) return;
tr[x].mn += v, tr[x].mx += v;
if(rx-lx > 1){
lz[2*x+1].a += v;
lz[2*x+2].a += v;
}
lz[x] = l_neutral;
}
/*=======================================================*/
int siz = 1;
vector<data> tr;
vector<lazy> lz;
lazysegtree() {
}
lazysegtree(int n) {
while (siz < n) siz *= 2;
tr.assign(2 * siz, d_neutral);
lz.assign(2 * siz, l_neutral);
}
void build(int n, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < n) {
create(x, lx, rx, 0);
}
return;
}
int mid = (lx + rx) / 2;
build(n, 2 * x + 1, lx, mid);
build(n, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void build(int n) {
build(n, 0, 0, siz);
}
void rupd(int l, int r, T v, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r or rx <= l) return;
if (lx >= l and rx <= r) {
modify(x, lx, rx, v);
propagate(x, lx, rx);
return;
}
int mid = (lx + rx) / 2;
rupd(l, r, v, 2 * x + 1, lx, mid);
rupd(l, r, v, 2 * x + 2, mid, rx);
merge(tr[x], tr[2 * x + 1], tr[2 * x + 2]);
}
void rupd(int l, int r, T v) {
rupd(l, r + 1, v, 0, 0, siz);
}
data query(int l, int r, int x, int lx, int rx) {
propagate(x, lx, rx);
if (lx >= r or rx <= l) return d_neutral;
if (lx >= l and rx <= r) return tr[x];
int mid = (lx + rx) / 2;
data curr;
data left = query(l, r, 2 * x + 1, lx, mid);
data right = query(l, r, 2 * x + 2, mid, rx);
merge(curr, left, right);
return curr;
}
data query(int l, int r) {
return query(l, r + 1, 0, 0, siz);
}
};
std::vector<int> distribute_candies(std::vector<int> b, std::vector<int> L,
std::vector<int> R, std::vector<int> V) {
ll n = sz(b), q = sz(L);
vector<pll> enter[n+5], leave[n+5];
rep(i,q){
ll l = L[i], r = R[i], v = V[i];
enter[l].pb({v,i+2});
leave[r+1].pb({v,i+2});
}
enter[0].pb({inf2,0});
enter[0].pb({-inf2,1});
lazysegtree<ll> st(q+5);
st.build(q+2);
vector<int> ans(n);
rep(i,n){
for(auto [v,id] : enter[i]){
st.rupd(id,q+1,v);
}
for(auto [v,id] : leave[i]){
st.rupd(id,q+1,-v);
}
ll l = 0, r = q+1;
ll upper = -1;
while(l <= r){
ll mid = (l+r) >> 1;
auto [mn,mnp,mx,mxp] = st.query(mid,q+1);
if(mx-st.query(mxp+1,q+1).mn <= b[i]){
upper = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
l = 0, r = q+1;
ll lower = -1;
while(l <= r){
ll mid = (l+r) >> 1;
auto [mn,mnp,mx,mxp] = st.query(mid,q+1);
if(st.query(mnp+1,q+1).mx-mn <= b[i]){
lower = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
ll pos = min(upper,lower);
auto [suffmn,mnp,suffmx,mxp] = st.query(pos+1,q+1);
ll lower_wall = -1;
if(st.query(pos,pos).mn > suffmx){
// pos = upper wall touch
lower_wall = suffmn;
}
else{
// pos = lower wall touch
lower_wall = suffmx-b[i];
}
ans[i] = st.query(q+1,q+1).mn-lower_wall;
}
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... |