답안 #853159

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
853159 2023-09-23T14:48:22 Z abcvuitunggio 사탕 분배 (IOI21_candies) C++17
8 / 100
902 ms 33712 KB
#include "candies.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair <long long, long long> pll;
int n,q;
vector <int> ve[200001],res;
long long lazy[900001],pos;
pll st[900001];
pll combine(pll a, pll b){
    return {min(a.first,b.first),max(a.second,b.second)};
}
void down(int node, int l, int r){
    if (!lazy[node]||l==r)
        return;
    st[node<<1].first+=lazy[node];
    st[node<<1].second+=lazy[node];
    lazy[node<<1]+=lazy[node];
    st[node<<1|1].first+=lazy[node];
    st[node<<1|1].second+=lazy[node];
    lazy[node<<1|1]+=lazy[node];
    lazy[node]=0;
}
void update(int node, int l, int r, int u, int v, long long val){
    if (l>v||r<u||l>r||u>v)
        return;
    if (l>=u&&r<=v){
        st[node].first+=val;
        st[node].second+=val;
        lazy[node]+=val;
        return;
    }
    down(node,l,r);
    int mid=(l+r)>>1;
    update(node<<1,l,mid,u,v,val);
    update(node<<1|1,mid+1,r,u,v,val);
    st[node]=combine(st[node<<1],st[node<<1|1]);
}
pll get(int node, int l, int r, int u, int v){
    if (l>v||r<u|l>r||u>v)
        return {1e18,-1e18};
    if (l>=u&&r<=v)
        return st[node];
    down(node,l,r);
    int mid=(l+r)>>1;
    return combine(get(node<<1,l,mid,u,v),get(node<<1|1,mid+1,r,u,v));
}
vector <int> distribute_candies(vector <int> c, vector <int> l, vector <int> r, vector <int> v){
    n=c.size();
    q=l.size();
    update(1,0,q+1,0,q+1,1e18);
    update(1,0,q+1,1,q+1,-1e18);
    for (int i=0;i<q;i++){
        ve[l[i]].push_back(i+1);
        ve[r[i]+1].push_back(-i-1);
    }
    for (int i=0;i<n;i++){
        for (int j:ve[i]){
            pos+=(j>0?v[j-1]:-v[-j-1]);
            update(1,0,q+1,abs(j)+1,q+1,(j>0?v[j-1]:-v[-j-1]));
        }
        int l=0,r=q+1;
        long long kq=-1;
        while (l<=r){
            int mid=(l+r)>>1;
            auto [mn,mx]=get(1,0,q+1,mid,q+1);
            if (mx-mn>=c[i]){
                kq=(mid==0?pos:(v[mid-1]>0?c[i]-mx+pos:pos-mn));
                l=mid+1;
            }
            else
                r=mid-1;
        }
        res.push_back(kq);
    }
    return res;
}

Compilation message

candies.cpp: In function 'pll get(int, int, int, int, int)':
candies.cpp:39:15: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
   39 |     if (l>v||r<u|l>r||u>v)
      |              ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7512 KB Output is correct
2 Incorrect 2 ms 7516 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 887 ms 33712 KB Output is correct
2 Correct 882 ms 33688 KB Output is correct
3 Correct 902 ms 33560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 7516 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7516 KB Output is correct
2 Correct 2 ms 7516 KB Output is correct
3 Incorrect 90 ms 26428 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7512 KB Output is correct
2 Incorrect 2 ms 7516 KB Output isn't correct
3 Halted 0 ms 0 KB -