답안 #721378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
721378 2023-04-10T19:10:59 Z urosk 사탕 분배 (IOI21_candies) C++17
8 / 100
1233 ms 50060 KB
#include "candies.h"
#define here cerr<<"======================================\n"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define fi first
#define sc second
#define llinf 1000000000000000000LL
#define dbg(x) cerr<<#x<<": "<<x<<endl

using namespace std;
#define maxn 200005
ll n,q;
ll c[maxn];
vector<pll> v[maxn];
ll lazy[2*maxn];
ll ls[2*maxn],rs[2*maxn],root,tsz = 0;
struct nod{
    ll mx,mn,mxid,mnid,val;
} t[2*maxn];
nod def,def0;
nod f(nod l,nod r){
    nod ans;
    ans.mn = min(l.mn,r.mn);
    ans.mx = max(l.mx,r.mx);
    if(l.mn<r.mn) ans.mnid = l.mnid;
    else ans.mnid = r.mnid;
    if(l.mx>r.mx) ans.mxid = l.mxid;
    else ans.mxid = r.mxid;
    ans.val = ans.mx-ans.mn;
    return ans;
}
void init(ll &v,ll tl,ll tr){
    if(!v) v = ++tsz;
    t[v].mn = 0;
    t[v].mx = 0;
    t[v].mxid = tr;
    t[v].mnid = tr;
    t[v].val = 0;
    if(tl==tr) return;
    ll mid = (tl+tr)/2;
    init(ls[v],tl,mid);
    init(rs[v],mid+1,tr);
}
void push(ll v,ll tl,ll tr,ll dub){
    if(dub==2) return;
    if(lazy[v]==0) return;
    t[v].mn+=lazy[v];
    t[v].mx+=lazy[v];
    ll mid = (tl+tr)/2;
    if(tl<tr){
        lazy[ls[v]]+=lazy[v];
        lazy[rs[v]]+=lazy[v];
        push(ls[v],tl,mid,dub+1);
        push(rs[v],mid+1,tr,dub+1);
    }
    lazy[v] = 0;
}
void upd(ll v,ll tl,ll tr,ll l,ll r,ll x){
    push(v,tl,tr,0);
    if(l>r||tl>tr||l>tr||tl>r) return;
    if(tl>=l&&tr<=r){
        lazy[v]+=x;
        push(v,tl,tr,0);
        return;
    }
    ll mid = (tl+tr)/2;
    upd(ls[v],tl,mid,l,r,x);
    upd(rs[v],mid+1,tr,l,r,x);
    t[v] = f(t[ls[v]],t[rs[v]]);
}
ll get(ll v,ll tl,ll tr,ll x,nod cur){
    push(v,tl,tr,0);
    nod cur2 = f(t[v],cur);
    if(cur2.val<x) return -1;
    if(tl==tr) return tl;
    ll mid = (tl+tr)/2;
    push(ls[v],tl,tr,0);
    push(rs[v],mid+1,tr,0);
    ll ans = get(rs[v],mid+1,tr,x,cur);
    if(ans==-1) return get(ls[v],tl,mid,x,f(t[rs[v]],cur));
    return ans;
}
nod get(ll v,ll tl,ll tr,ll l,ll r){
    push(v,tl,tr,0);
    if(l>r||l>tr||tl>tr||tl>r) return def0;
    if(tl>=l&&tr<=r) return t[v];
    ll mid = (tl+tr)/2;
    return f(get(ls[v],tl,mid,l,r),get(rs[v],mid+1,tr,l,r));
}
vector<int> ans;
vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> VAL) {
    n = C.size();
    q = L.size();
    for(ll i = 1;i<=n;i++) c[i] = C[i-1];
    for(ll i = 1;i<=q;i++){
        v[L[i-1]+1].pb({VAL[i-1],i+2});
        v[R[i-1]+2].pb({-VAL[i-1],i+2});
    }
    q+=2;
    init(root,1,q);
    upd(1,1,q,1,q,llinf);
    upd(1,1,q,2,q,-llinf);
    ll sum = 0;
    nod def0;
    def0.mn = llinf;
    def0.mx = -llinf;
    def0.mnid = def0.mxid = q+1;
    def0.val = 0;
    for(ll i = 1;i<=n;i++){
        for(pll p : v[i]){
            ll x = p.fi;
            ll i = p.sc;
            upd(root,1,q,i,q,x);
            sum+=x;
        }
        nod def;
        def.mn = llinf;
        def.mx = -llinf;
        def.mxid = q+1;
        def.mnid = q+1;
        def.val = 0;
        ll j = get(1,1,q,c[i],def);
        nod cur = get(1,1,q,j,q);
        ll minid = cur.mnid;
        ll maxid = cur.mxid;
        if(minid>maxid) ans.pb(min(c[i],sum-cur.mn));
        else ans.pb(max(0LL,c[i]+(sum-cur.mx)));
    }
    return ans;
}
/**
3
10 15 13
2
0 2 20
0 1 -11

3
10 15 13
2
0 2 -5
0 2 7
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 5 ms 5332 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1223 ms 50012 KB Output is correct
2 Correct 1233 ms 50060 KB Output is correct
3 Correct 1227 ms 49912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Incorrect 731 ms 43764 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Incorrect 202 ms 41216 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Incorrect 5 ms 5332 KB Output isn't correct
4 Halted 0 ms 0 KB -