답안 #814975

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
814975 2023-08-08T11:29:58 Z blackyuki 사탕 분배 (IOI21_candies) C++17
100 / 100
1661 ms 41528 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define fi first
#define se second
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
const ll inf=1001001001001001001;
#include "candies.h"

struct segtree{
    ll N=1;
    vp seg;
    vi lazy;
    P f(P a,P b){
        return P(max(a.fi,b.fi),min(a.se,b.se));
    }
    segtree(ll n){
        while(N<n)N<<=1;
        seg=vp(N*2,P(-inf,inf));lazy=vi(N*2);
        rep(i,n)seg[i+N]=P(0,0);
        for(ll i=N-1;i>0;i--)seg[i]=f(seg[i*2],seg[i*2+1]);
    }
    void eval(ll k,ll l,ll r){
        seg[k].fi+=lazy[k];seg[k].se+=lazy[k];
        if(r-l>1){
            lazy[k*2]+=lazy[k];
            lazy[k*2+1]+=lazy[k];
        }
        lazy[k]=0;
    }
    void add(ll x,ll a,ll b,ll k=1,ll l=0,ll r=-1){
        if(r==-1)r=N;
        if(a<=l&&r<=b){
            lazy[k]+=x;
            eval(k,l,r);
            return;
        }
        if(r<=a||b<=l){
            eval(k,l,r);return;
        }
        eval(k,l,r);
        add(x,a,b,k*2,l,(l+r)/2);
        add(x,a,b,k*2+1,(l+r)/2,r);
        seg[k]=f(seg[k*2],seg[k*2+1]);
    }
    P get(ll a,ll b,ll k,ll l,ll r){
        if(a<=l&&r<=b){
            eval(k,l,r);
            return seg[k];
        }
        if(r<=a||b<=l)return P(-inf,inf);
        eval(k,l,r);
        return f(get(a,b,k*2,l,(l+r)/2),get(a,b,k*2+1,(l+r)/2,r));
    }
    ll getma(ll a,ll b){
        return get(a,b,1,0,N).fi;
    }
    ll getmi(ll a,ll b){
        return get(a,b,1,0,N).se;
    }
};
std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l, std::vector<int> r, std::vector<int> v) {
    ll n=c.size(),q=v.size();
    vvp event(n+1);
    rep(i,q){
        event[l[i]].pb(q-i,-v[i]);
        event[r[i]+1].pb(q-i,v[i]);
    }
    segtree seg(q+1);
    vector<int> res(n);
    rep(i,n){
        for(P x:event[i])seg.add(x.se,x.fi,q+1);
        ll ok=q+1,ng=-1;
        while(ok-ng>1){
            ll md=(ok+ng)/2;
            if(seg.getma(0,md+1)-seg.getmi(0,md+1)>=c[i])ok=md;
            else ng=md;
        }
        if(ok==q+1||seg.getma(0,ok+1)==seg.getma(ok,ok+1))res[i]=-seg.getmi(0,ok+1);
        else res[i]=c[i]-seg.getma(0,ok+1);
    }
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 8 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1557 ms 34816 KB Output is correct
2 Correct 1595 ms 38904 KB Output is correct
3 Correct 1622 ms 38828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 303 ms 26224 KB Output is correct
3 Correct 516 ms 9776 KB Output is correct
4 Correct 1655 ms 40748 KB Output is correct
5 Correct 1611 ms 41140 KB Output is correct
6 Correct 1513 ms 41528 KB Output is correct
7 Correct 1544 ms 40872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 157 ms 23968 KB Output is correct
4 Correct 475 ms 8776 KB Output is correct
5 Correct 1409 ms 34160 KB Output is correct
6 Correct 1414 ms 34836 KB Output is correct
7 Correct 1537 ms 35412 KB Output is correct
8 Correct 1349 ms 34052 KB Output is correct
9 Correct 1414 ms 35620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 8 ms 596 KB Output is correct
6 Correct 1557 ms 34816 KB Output is correct
7 Correct 1595 ms 38904 KB Output is correct
8 Correct 1622 ms 38828 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 303 ms 26224 KB Output is correct
11 Correct 516 ms 9776 KB Output is correct
12 Correct 1655 ms 40748 KB Output is correct
13 Correct 1611 ms 41140 KB Output is correct
14 Correct 1513 ms 41528 KB Output is correct
15 Correct 1544 ms 40872 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 157 ms 23968 KB Output is correct
19 Correct 475 ms 8776 KB Output is correct
20 Correct 1409 ms 34160 KB Output is correct
21 Correct 1414 ms 34836 KB Output is correct
22 Correct 1537 ms 35412 KB Output is correct
23 Correct 1349 ms 34052 KB Output is correct
24 Correct 1414 ms 35620 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 487 ms 8820 KB Output is correct
27 Correct 282 ms 28752 KB Output is correct
28 Correct 1587 ms 39396 KB Output is correct
29 Correct 1652 ms 39768 KB Output is correct
30 Correct 1659 ms 39940 KB Output is correct
31 Correct 1661 ms 40148 KB Output is correct
32 Correct 1628 ms 40348 KB Output is correct