锰锌刚学oi,全re惨遭暴〇

P4064 [JXOI2017] 加法

Miraii @ 2021-08-03 19:42:57

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5,inf=2e9;
struct node{
    int l,r;
}qk[N];
int t,n,m,k,a,A[N],b[N],ans;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return x*f;
}
bool cmp(node a,node b){
    return a.l<b.l;
}
bool check(int x){
    priority_queue<int> q;
    while(q.size()) q.pop();
    for(int i=1;i<=n;i++) b[i]=A[i]-A[i-1];
    int now=1,res=0;
    for(int i=1;i<=n;i++){
    b[i]+=b[i-1];
    while(qk[now].l==i) q.push(qk[now++].r);
    while(q.top()>=i&&b[i]<x&&q.size()){
        b[i]+=a;
        b[q.top()+1]-=a;q.pop();
        res++;
    }
    if(res>k||b[i]<x) return 0;
    }
    return 1;
}
int main(){
    t=read();
    while(t--){
    int l=inf,r=-inf;
    ans=-1;
    n=read();m=read();k=read();a=read();
    for(int i=1;i<=n;i++) A[i]=read(),l=min(l,A[i]),r=max(r,A[i]);
    for(int i=1;i<=m;i++){
        qk[i].l=read();qk[i].r=read();
    }
    sort(qk+1,qk+1+m,cmp);
    r=l+a*m;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)){
        l=mid+1;
        ans=mid;
        }
        else r=mid-1;
    }
    cout<<ans<<endl;
    }
    return 0;
}

by StarLbright40 @ 2021-08-03 19:50:45

while(q.top()>=i&&b[i]<x&&q.size())

中,先判q.size,不然可能会从空队列中取top造成错误

不确定有没有别的问题


by Miraii @ 2021-08-03 19:55:59

@星光0000 AC了,非常感谢!


|