답안 #619693

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
619693 2022-08-02T14:36:52 Z Koosha_mv 사탕 분배 (IOI21_candies) C++17
100 / 100
756 ms 40248 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,int(v.size())) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;
const ll inf=1e16;

int n,q,c[N],a[N];
ll Min[N<<2],Max[N<<2],lz[N<<2];
ll b[N];
vector<int> s,t,val,ad[N],dl[N];

void upd(int id){
    Min[id]=min(Min[id<<1],Min[id<<1|1]);
    Max[id]=max(Max[id<<1],Max[id<<1|1]);
}
void shift(int id){
    lz[id<<1]+=lz[id];
    Min[id<<1]+=lz[id];
    Max[id<<1]+=lz[id];

    lz[id<<1|1]+=lz[id];
    Min[id<<1|1]+=lz[id];
    Max[id<<1|1]+=lz[id];

    lz[id]=0;
}

void add(int L,int R,ll val,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return ;
    if(L<=l && r<=R){
        lz[id]+=val;
        Min[id]+=val;
        Max[id]+=val;
        return ;
    }
    int mid=(l+r)>>1;
    shift(id);
    add(L,R,val,id<<1,l,mid);
    add(L,R,val,id<<1|1,mid,r);
    upd(id);
}
int find(ll d,ll mn=inf,ll mx=-inf,int id=1,int l=0,int r=q+1){
    if(Max[1]-Min[1]<d) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(max(mx,Max[id<<1|1])-min(mn,Min[id<<1|1])<d){
        return find(d,min(mn,Min[id<<1|1]),max(mx,Max[id<<1|1]),id<<1,l,mid);
    }
    else{
        return find(d,mn,mx,id<<1|1,mid,r);
    }
}
ll get0(int L,int R,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return inf;
    if(L<=l && r<=R){
        return Min[id];
    }
    int mid=(l+r)>>1;
    shift(id);
    return min(get0(L,R,id<<1,l,mid),get0(L,R,id<<1|1,mid,r));
}
ll get1(int L,int R,int id=1,int l=0,int r=q+1){
    if(r<=L || R<=l) return -inf;
    if(L<=l && r<=R){
        return Max[id];
    }
    int mid=(l+r)>>1;
    shift(id);
    return max(get1(L,R,id<<1,l,mid),get1(L,R,id<<1|1,mid,r));
}
int find0(ll d,int id=1,int l=0,int r=q+1){
    if(d<=Min[1]) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(Min[id<<1|1]>=d){
        return find0(d,id<<1,l,mid);
    }
    else{
        return find0(d,id<<1|1,mid,r);
    }
}
int find1(ll d,int id=1,int l=0,int r=q+1){
    if(d>=Max[1]) return 0;
    if(l+1==r){
        return r;
    }
    int mid=(l+r)>>1;
    shift(id);
    if(Max[id<<1|1]<=d){
        return find1(d,id<<1,l,mid);
    }
    else{
        return find1(d,id<<1|1,mid,r);
    }
}

vector<int> distribute_candies(vector<int> _c,vector<int> _s,vector<int> _t,vector<int> _val){
    s=_s,t=_t,val=_val;
    n=_c.size(),q=s.size();
    vector<int> ans(n);
    f(i,0,n) c[i]=_c[i];
    f(i,0,q){
        ad[s[i]].pb(i);
        dl[t[i]].pb(i);
    }
    f(i,0,n){
        for(auto x : ad[i]) a[x+1]+=val[x];
        for(auto x : ad[i]) add(x+1,q+1,val[x]);
        int p=find(c[i]);
        int b0=find0(get0(p,q+1));
        int b1=find1(get1(p,q+1));
        ll mn=get0(p,q+1);
        ll mx=get1(p,q+1);

        ans[i]=get0(q,q+1)-get0(p,q+1);
        if(b1<b0){
            ans[i]=c[i]-(get1(p,q+1)-get1(q,q+1));
        }

        for(auto x : dl[i]) add(x+1,q+1,-val[x]);
        for(auto x : dl[i]) a[x+1]-=val[x];
    }
    return ans;
}
/*
3
10 15 13
2
0 2 20
0 1 -11

*/

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:138:12: warning: unused variable 'mn' [-Wunused-variable]
  138 |         ll mn=get0(p,q+1);
      |            ^~
candies.cpp:139:12: warning: unused variable 'mx' [-Wunused-variable]
  139 |         ll mx=get1(p,q+1);
      |            ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9940 KB Output is correct
4 Correct 7 ms 9940 KB Output is correct
5 Correct 9 ms 9940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 749 ms 40220 KB Output is correct
2 Correct 737 ms 40248 KB Output is correct
3 Correct 627 ms 40108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 237 ms 32764 KB Output is correct
3 Correct 201 ms 14308 KB Output is correct
4 Correct 676 ms 40104 KB Output is correct
5 Correct 653 ms 40208 KB Output is correct
6 Correct 723 ms 40212 KB Output is correct
7 Correct 632 ms 40204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 191 ms 31544 KB Output is correct
4 Correct 192 ms 13140 KB Output is correct
5 Correct 442 ms 34796 KB Output is correct
6 Correct 467 ms 34720 KB Output is correct
7 Correct 566 ms 34948 KB Output is correct
8 Correct 428 ms 34816 KB Output is correct
9 Correct 491 ms 34820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9940 KB Output is correct
4 Correct 7 ms 9940 KB Output is correct
5 Correct 9 ms 9940 KB Output is correct
6 Correct 749 ms 40220 KB Output is correct
7 Correct 737 ms 40248 KB Output is correct
8 Correct 627 ms 40108 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 237 ms 32764 KB Output is correct
11 Correct 201 ms 14308 KB Output is correct
12 Correct 676 ms 40104 KB Output is correct
13 Correct 653 ms 40208 KB Output is correct
14 Correct 723 ms 40212 KB Output is correct
15 Correct 632 ms 40204 KB Output is correct
16 Correct 6 ms 9684 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 191 ms 31544 KB Output is correct
19 Correct 192 ms 13140 KB Output is correct
20 Correct 442 ms 34796 KB Output is correct
21 Correct 467 ms 34720 KB Output is correct
22 Correct 566 ms 34948 KB Output is correct
23 Correct 428 ms 34816 KB Output is correct
24 Correct 491 ms 34820 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 179 ms 13172 KB Output is correct
27 Correct 223 ms 32684 KB Output is correct
28 Correct 575 ms 40208 KB Output is correct
29 Correct 722 ms 40212 KB Output is correct
30 Correct 649 ms 40184 KB Output is correct
31 Correct 731 ms 40208 KB Output is correct
32 Correct 756 ms 40228 KB Output is correct