제출 #1333977

#제출 시각아이디문제언어결과실행 시간메모리
1333977KhoaDuyTricks of the Trade (CEOI23_trade)C++20
55 / 100
7142 ms36224 KiB
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
const ll limit=-1e18;
#define int long long
struct node{
    int delidx=-1,addidx=-1;
};
ll ans=limit;
set<pair<int,int>> se;
ll curr=0;
const int MAXN=3*1e5;
ll pre[MAXN+1];
int c[MAXN+1],s[MAXN+1];
int k;
stack<node> st;
stack<bool> rb;
int L,R;
node breh;
int opt[MAXN+2];
ll calc(){
    return (curr-(pre[R]-pre[L-1]));
}
void reset(){
    curr=0;
    while(!st.empty()){
        st.pop();
    }
    while(!rb.empty()){
        rb.pop();
    }
    L=0,R=0;
    while(!se.empty()){
        se.erase(se.begin());
    }
}
void add(int idx){
    node nxt;
    curr+=s[idx];
    se.insert({s[idx],idx});
    nxt.addidx=idx;
    if(se.size()>k){
        int idx2=(*se.begin()).second;
        nxt.delidx=idx2;
        se.erase(se.begin());
        curr-=s[idx2];
    }
    st.push(nxt);
}
void rollback(int cnt){
    for(int bruh=0;bruh<cnt;bruh++){
        node temp=st.top();
        st.pop();
        if(!rb.top()){
            L++;
        }
        else{
            R--;
        }
        if(temp.delidx!=-1){
            se.insert({s[temp.delidx],temp.delidx});
            curr+=s[temp.delidx];
        }
        if(temp.addidx!=-1){
            se.erase({s[temp.addidx],temp.addidx});
            curr-=s[temp.addidx];
        }
        rb.pop();
    }
}
void moveL(int cnt){
    for(int bruh=0;bruh<cnt;bruh++){
        L--;
        if(L<=R){
            add(L);
        }
        else{
            st.push(breh);
        }
        rb.push(false);
    }
}
void moveR(int cnt){
    for(int bruh=0;bruh<cnt;bruh++){
        R++;
        if(L<=R){
            add(R);
        }
        else{
            st.push(breh);
        }
        rb.push(true);
    }
}
void dnc(int l,int r,int optl,int optr){
    // cout << "HERE " << l << ' ' << r << ' ' << optl << ' ' << optr << endl;
    // cout << "??? " << L << ' ' << R << endl;
    int mid=((l+r)>>1);
    moveL(r-mid);
    int optm=-1;
    ll val=limit;
    for(int i=optl;i<=optr;i++){
        ll nxt=calc();
        if(i-mid+1>=k&&nxt>val){
            val=nxt;
            optm=i;
        }
        if(i+1<=optr){
            moveR(1);
        }
    }
    opt[mid]=optm;
    ans=max(ans,val);
    rollback(optr-optl+r-mid);
    if(l<=mid-1){
        moveL(r-mid+1);
        dnc(l,mid-1,optl,optm);
        rollback(r-mid+1);
    }
    if(mid+1<=r){
        moveR(optm-optl);
        dnc(mid+1,r,optm,optr);
        rollback(optm-optl);
    }
}
bool del[MAXN+1];
pair<int,int> Tid={0,-1};
pair<int,int> TT(pair<int,int> &le,pair<int,int> &ri){
    return max(le,ri);
}
struct segtree{
    vector<pair<int,int>> seg;
    int n,lg;
    void refresh(int v){
        seg[v]=TT(seg[v<<1],seg[(v<<1)|1]);
    }
    void build(vector<int> &a){
        n=1;
        while(n<a.size()){
            n<<=1;
        }
        lg=__lg(n);
        seg.assign(n<<1,Tid);
        for(int i=0;i<a.size();i++){
            seg[n+i]={a[i],i};
        }
        for(int i=n-1;i>=1;i--){
            refresh(i);
        }
    }
    void update(int l){
        del[l]=true;
        l+=n;
        seg[l]=Tid;
        for(int i=1;i<=lg;i++){
            refresh(l>>i);
        }
    }
    pair<int,int> query(int l,int r){
        l+=n,r+=n;
        pair<int,int> curr=Tid;
        for(;l<r;l>>=1,r>>=1){
            if(l&1){
                curr=TT(curr,seg[l]);
                l++;
            }
            if(r&1){
                r--;
                curr=TT(curr,seg[r]);
            }
        }
        return curr;
    }
};
segtree seg;
void dnc_trace(int l,int r,int optl,int optr){
    // cout << "HERE " << l << ' ' << r << ' ' << optl << ' ' << optr << endl;
    // cout << "??? " << L << ' ' << R << endl;
    int mid=((l+r)>>1);
    moveL(r-mid);
    int optm=opt[mid];
    ll val=limit;
    moveR(optm-optl);
    for(int i=optm;i<=opt[mid+1];i++){
        if(calc()==ans){
            // cout << "GOT " << mid << ' ' << i << ' ' << L << ' ' << R << ' ' << calc() << endl;
            // for(auto it=se.begin();it!=se.end();it++){
            //     cout << (*it).first << ' ';
            // }
            // cout << endl;
            int idx=seg.query(mid,i+1).second;
            while(idx!=-1&&s[idx]>=(*se.begin()).first){
                seg.update(idx);
                idx=seg.query(mid,i+1).second;
            }
        }
        if(i<opt[mid+1]){
            moveR(1);
        }
    }
    rollback(opt[mid+1]-optl);
    rollback(r-mid);
    if(l<=mid-1){
        moveL(r-mid+1);
        dnc_trace(l,mid-1,optl,optm);
        rollback(r-mid+1);
    }
    if(mid+1<=r){
        moveR(optm-optl);
        dnc_trace(mid+1,r,optm,optr);
        rollback(optm-optl);
    }
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if(fopen("input.txt","r")){
        freopen("input.txt","r",stdin);
    }
    int n;
    cin >> n >> k;
    for(int i=1;i<=n;i++){
        cin >> c[i];
        pre[i]=pre[i-1]+c[i];
    }
    for(int i=1;i<=n;i++){
        cin >> s[i];
    }
    L=n-k+1,R=k;
    for(int i=L;i<=R;i++){
        add(i);
    }
    vector<int> a(n+1);
    a[0]=0;
    for(int i=1;i<=n;i++){
        a[i]=s[i];
    }
    seg.build(a);
    dnc(1,n-k+1,k,n);
    opt[n-k+2]=n;
    reset();
    L=n-k+1,R=k;
    for(int i=L;i<=R;i++){
        add(i);
    }
    dnc_trace(1,n-k+1,k,n);
    cout << ans << endl;
    for(int i=1;i<=n;i++){
        if(del[i]){
            cout << '1';
        }
        else{
            cout << '0';
        }
    }
}

컴파일 시 표준 에러 (stderr) 메시지

trade.cpp: In function 'int main()':
trade.cpp:219:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  219 |         freopen("input.txt","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...