답안 #435333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435333 2021-06-23T08:14:45 Z jangwonyoung 사탕 분배 (IOI21_candies) C++17
100 / 100
608 ms 31500 KB
////////////////////////////////////////
#include "candies.h"

#include <vector>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int N=2e5+5;
#define fi first
#define se second
int n,q;
vector<pair<int,int> >qr[N];
const int ts=1<<19;
ll mx[ts];
ll mn[ts];
ll sum[ts];
void pull(int id){
    sum[id]=sum[id*2]+sum[id*2+1];
    mx[id]=max(mx[id*2],mx[id*2+1]+sum[id*2]);
    mn[id]=min(mn[id*2],mn[id*2+1]+sum[id*2]);
}
void upd(int id,int l,int r,int p,ll v){
    if(l==r){
        mx[id]=0;
        mn[id]=0;
        sum[id]=v;
        if(v>0) mx[id]=v;
        else mn[id]=v;
        return;
    }
    int mid=(l+r)/2;
    if(p<=mid) upd(id*2,l,mid,p,v);
    else upd(id*2+1,mid+1,r,p,v);
    pull(id);
}
ll qry(int id,int l,int r,ll v,ll c){
    if(l==r){
        ll res=v+sum[id];
        res=min(res,c);
        res=max(res,0LL);
        return res;
    }
    int mid=(l+r)/2;
    if(mx[id*2+1]-mn[id*2+1]>=c){
        ll res=qry(id*2+1,mid+1,r,v,c);
        return res;
    }
    v=qry(id*2,l,mid,v,c);
    if(v+mx[id*2+1]>c){
        return c+sum[id*2+1]-mx[id*2+1];
    }
    if(v+mn[id*2+1]<0){
        return sum[id*2+1]-mn[id*2+1];
    }
    return v+sum[id*2+1];
}
vector<int>distribute_candies(vi c,vi l,vi r,vi v){
    n=c.size();q=l.size();
    vi ans(n);
    for(int i=0; i<q ;i++){
        qr[l[i]+1].push_back({i+1,v[i]});
        qr[r[i]+2].push_back({i+1,0});
    }
    for(int i=1; i<=n ;i++){
        for(auto c:qr[i]){
            upd(1,1,q,c.fi,c.se);
        }
        ans[i-1]=qry(1,1,q,0,c[i-1]);
    }
    return ans;
}

//////////////////////////////////////////
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 7 ms 5224 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 608 ms 31388 KB Output is correct
2 Correct 580 ms 31388 KB Output is correct
3 Correct 586 ms 31288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 302 ms 26432 KB Output is correct
3 Correct 80 ms 8756 KB Output is correct
4 Correct 510 ms 31424 KB Output is correct
5 Correct 558 ms 31296 KB Output is correct
6 Correct 539 ms 31500 KB Output is correct
7 Correct 507 ms 31392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 130 ms 25248 KB Output is correct
4 Correct 67 ms 7560 KB Output is correct
5 Correct 199 ms 27684 KB Output is correct
6 Correct 212 ms 27640 KB Output is correct
7 Correct 220 ms 27688 KB Output is correct
8 Correct 225 ms 27676 KB Output is correct
9 Correct 311 ms 27716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 7 ms 5224 KB Output is correct
4 Correct 5 ms 5196 KB Output is correct
5 Correct 6 ms 5196 KB Output is correct
6 Correct 608 ms 31388 KB Output is correct
7 Correct 580 ms 31388 KB Output is correct
8 Correct 586 ms 31288 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 302 ms 26432 KB Output is correct
11 Correct 80 ms 8756 KB Output is correct
12 Correct 510 ms 31424 KB Output is correct
13 Correct 558 ms 31296 KB Output is correct
14 Correct 539 ms 31500 KB Output is correct
15 Correct 507 ms 31392 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 4 ms 4940 KB Output is correct
18 Correct 130 ms 25248 KB Output is correct
19 Correct 67 ms 7560 KB Output is correct
20 Correct 199 ms 27684 KB Output is correct
21 Correct 212 ms 27640 KB Output is correct
22 Correct 220 ms 27688 KB Output is correct
23 Correct 225 ms 27676 KB Output is correct
24 Correct 311 ms 27716 KB Output is correct
25 Correct 4 ms 4940 KB Output is correct
26 Correct 78 ms 7568 KB Output is correct
27 Correct 285 ms 26512 KB Output is correct
28 Correct 525 ms 31412 KB Output is correct
29 Correct 600 ms 31392 KB Output is correct
30 Correct 553 ms 31384 KB Output is correct
31 Correct 575 ms 31264 KB Output is correct
32 Correct 475 ms 31384 KB Output is correct