这题做得我想哭QAQ
完全不知所措
srorz
SROTL
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; #define _(x) static_cast<double>(x) const int N=500000+10; int n,m,s[N],cnt[N],a[N],tot; struct node{int l,r,x;}t[N<<1]; int newnode(int l,int r,int x){ t[++tot]=(node){l,r,x}; return tot; } struct My_queue{ int st,ed,len; void push_back(int x){ if(!len)st=ed=newnode(0,0,x); else t[ed].r=newnode(ed,0,x),ed=t[ed].r; len++; } bool empty(){return !len;} int front(){return t[st].x;} int back(){return t[ed].x;} void pop_front(){st=t[st].r;len--;} void pop_back(){ed=t[ed].l;len--;} void push(int x){while(!empty()&&a[back()]>a[x])pop_back();push_back(x);} }Q[N<<1],Qu[N<<1],*q=Q+N,*qu=Qu+N; bool cmp(int x,int y){ return a[x]<a[y]; } int iabs(int x){ return x<0?-x:x; } int main(){ //freopen("a.in","r",stdin); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("%d%d",a+i,s+i); if(!s[i])s[i]--; } for(int i=n-1;i>=1;i--)s[i]+=s[i+1]; for(int i=n;i>=1;cnt[i]+=cnt[i+1],i--)cnt[i]+=!s[i]; int d=s[1]?ceil(iabs(s[1])/_(m)):cnt[1]<m; cnt[n+1]=-1; if(d){ a[n+1]=n+1; int last=0; for(int i=2;i<=n;i++) qu[s[i]].push_back(i-1); for(int i=1;i<m;i++){ int ans=n+1; for(int j=s[last+1]-d;j<=s[last+1]+d;j++){ if(ceil(iabs(j)/_(m-i))>d)continue; for(;!qu[j].empty()&&n-qu[j].front()>=m-i;qu[j].pop_front()) if(qu[j].front()>last)q[j].push(qu[j].front()); for(;!q[j].empty()&&q[j].front()<=last;q[j].pop_front()); if(!q[j].empty())ans=min(ans,q[j].front(),cmp); } last=ans; printf("%d ",a[ans]); } }else{ for(int i=1,j=2;i<m;i++){ for(;cnt[j+1]>=m-i;j++) if(!s[j+1])q[0].push(j); printf("%d ",a[q[0].front()]); q[0].pop_front(); } } printf("%d",a[n]); return 0; }