제출 #1332147

#제출 시각아이디문제언어결과실행 시간메모리
1332147danglayloi1Tricks of the Trade (CEOI23_trade)C++20
100 / 100
1026 ms34032 KiB
#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const int nx=250005;
const int LG=19;
struct BIT
{
    vector<ll> bit;
    int n;
    void init(int _n)
    {
        n=_n;
        bit.assign(n+1, 0);
    }
    void add(int x, int val)
    {
        for(; x <= n; x+=x&-x)
            bit[x]+=val;
    }
    ll get(int x)
    {
        ll res=0;
        for(; x > 0; x-=x&-x)
            res+=bit[x];
        return res;
    }
    int walk(ll k)
    {
        int pos=0;
        ll cur=0;
        for(int i = LG-1; i >= 0; i--)
            if(pos+(1<<i)<=n && cur+bit[pos+(1<<i)]<k)
                pos+=(1<<i), cur+=bit[pos];
        return pos+1;
    }
} S, B;
int n, K, id[nx], L=1, R=0, st[LG][nx];
ll pre[nx], res=-inf;
ii a[nx];
pair<ll, int> best[nx];
vector<ii> ve;
void add(int i, int e)
{
    int pos=id[i];
    B.add(pos, e);
    S.add(pos, e*a[i].se);
}
ll cost(int l, int r)
{
    if(r-l+1<K) return -inf-1;
    while(L>l) add(--L, 1);
    while(R<r) add(++R, 1);
    while(L<l) add(L++, -1);
    while(R>r) add(R--, -1);
    int pos=B.walk(K);
    return S.get(pos)-pre[r]+pre[l-1];
}
void solve(int l, int r, int optl, int optr)
{
    if(l>r) return;
    int mid=(l+r)>>1;
    best[mid]={-inf, 1};
    for(int i = optl; i <= min(optr, mid); i++)
    {
        ll tmp=cost(i, mid);
        if(tmp>=best[mid].fi)
            best[mid]={tmp, i};
    }
    res=max(res, best[mid].fi);
    solve(l, mid-1, optl, best[mid].se);
    solve(mid+1, r, best[mid].se, optr);
}
void update(int l, int r, int val)
{
    int k=__lg(r-l+1);
    st[k][l]=min(st[k][l], val);
    st[k][r-(1<<k)+1]=min(st[k][r-(1<<k)+1], val);
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>K;
    for(int i = 1; i <= n; i++)
        cin>>a[i].fi, pre[i]=pre[i-1]+a[i].fi;
    for(int i = 1; i <= n; i++)
        cin>>a[i].se, ve.emplace_back(a[i].se, i);
    sort(ve.begin(), ve.end(), greater<ii>());
    for(int i = 0; i < n; i++)
        id[ve[i].se]=i+1;
    S.init(n);
    B.init(n);
    solve(1, n, 1, n);
    cout<<res<<'\n';
    int j=1;
    memset(st, 0x3f, sizeof(st));
    for(int i = 1; i <= n; i++)
    {
        if(best[i].fi!=res) continue;
        while(j<=best[i].se)
        {
            ll x=cost(j, i);
            if(x==res)
            {
                int pos=B.walk(K);
                update(j, i, ve[pos-1].fi);
            }
            j++;
        }
        j--;
    }
    for(j = LG-2; j >= 0; j--)
    {
        for(int i = 1; i <= n; i++)
        {
            st[j][i]=min(st[j][i], st[j+1][i]);
            if(i+(1<<j)<=n) st[j][i+(1<<j)]=min(st[j][i+(1<<j)], st[j+1][i]);
        }
    }
    for(int i = 1; i <= n; i++)
        cout<<(st[0][i]<=a[i].se);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...