求卡常
  • 板块灌水区
  • 楼主Rolen
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/11/3 20:53
  • 上次更新2024/11/4 10:21:50
查看原帖
求卡常
730113
Rolen楼主2024/11/3 20:53
#include<iostream>
#include<algorithm>
#include<limits.h>
using namespace std;
#define ll long long
#define ls(x) x<<1
#define rs(x) x<<1|1
ll n,k,l,r,mid,a[1000006],c1,c2,res,cnt,mx=1,it,pt,yes,sum,yuan,ss;
pair<ll,ll>p[300005];
ll tr[4000006],q1[300005],q2[300005];
namespace fastio{
	#define FILE_SIZE 1<<23
	char rbuf[FILE_SIZE],*p1=rbuf,*p2=rbuf,wbuf[FILE_SIZE],*p3=wbuf;
	inline char getc(){return p1==p2&&(p2=(p1=rbuf)+fread(rbuf,1,FILE_SIZE,stdin),p1==p2)?-1:*p1++;}
	inline void putc(char x){(*p3++=x);}
	template<typename T> void read(T &x){
		x=0;char c=getchar();T neg=0;
		while(!isdigit(c)) neg|=!(c^'-'),c=getchar();
		while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
		if(neg) x=(~x)+1;
	}
	template<typename T> void recursive_print(T x){if(!x) return;recursive_print(x/10);putc(x%10^48);}
	template<typename T> void print(T x){if(!x) putc('0');if(x<0) putc('-'),x=~x+1;recursive_print(x);}
	void print_final(){fwrite(wbuf,1,p3-wbuf,stdout);}
}
void init(ll lp,ll rp,ll x){
	tr[x]=0;
	if(lp==rp){
		return;
	}
	ll m=(lp+rp)>>1;
	init(lp,m,ls(x)),init(m+1,rp,rs(x));
}
void updata(ll lp,ll rp,ll x,ll L,ll R){
	if(lp==rp){
		tr[x]++;
		return;
	}
	ll m=(lp+rp)>>1;
	if(m<R&&m>=L){
		updata(lp,m,ls(x),L,m),updata(m+1,rp,rs(x),m+1,R);
	}
	else if(m>=R) updata(lp,m,ls(x),L,R);
	else updata(m+1,rp,rs(x),L,R);
	tr[x]=tr[ls(x)]+tr[rs(x)];
}
ll query(ll lp,ll rp,ll x,ll L,ll R){
	if(L<=lp&&R>=rp){
		return tr[x];
	}
	ll m=(lp+rp)>>1;
	if(m<R&&m>=L){
		return query(lp,m,ls(x),L,m)+query(m+1,rp,rs(x),m+1,R);
	}
	if(m>=R) return query(lp,m,ls(x),L,R);
	return query(m+1,rp,rs(x),L,R);
}
ll lowerbound(ll x){
	ll lp=1,rp=mx,m;
	while(lp<=rp){
		m=(lp+rp)>>1;
		if(a[m]>=x) rp=m-1;
		else lp=m+1;
	}
	return rp+1;
}
void uni(){
	ll pre=-LONG_LONG_MAX;
	mx=0;
	for(int i=1;i<=cnt;++i){
		if(pre!=a[i]){
			a[++mx]=a[i],pre=a[i];
		}
	}
}
int check1(ll x){
	init(1,mx,1),mx=-LONG_LONG_MAX,c1=c2=res=0,cnt=0;
	for(int i=1;i<=n;++i) if(p[i].second>0)	q1[++c1]=p[i].first+x*p[i].second,a[++cnt]=q1[c1];	else	q2[++c2]=p[i].first+x*p[i].second,a[++cnt]=q2[c2];
	sort(a+1,a+1+cnt),uni();
	for(int i=1;i<=c2;++i) pt=lowerbound(q2[i]),updata(1,mx,1,pt,pt);
	for(int i=1;i<=c1;++i) pt=lowerbound(q1[i]),res+=query(1,mx,1,1,pt);
	if(res-yuan>=k) return 1;
	return 0;
}
int check2(ll x){
	init(1,mx,1);
	mx=-LONG_LONG_MAX;
	c1=c2=res=0;
	cnt=0;
	for(int i=1;i<=n;++i){
		if(p[i].second>0){
			q1[++c1]=p[i].first+x*p[i].second;
			a[++cnt]=q1[c1];
		}
		else{
			q2[++c2]=p[i].first+x*p[i].second;
			a[++cnt]=q2[c2];
		}
	}
	sort(a+1,a+1+cnt);
	uni();
	for(int i=1;i<=c2;++i){
		pt=lowerbound(q2[i]);
		res+=query(1,mx,1,1,pt);
		updata(1,mx,1,pt,pt);
	}
	init(1,mx,1);
	for(int i=c1;i>=1;--i){
		pt=lowerbound(q1[i]);
		res+=query(1,mx,1,pt,mx);
		updata(1,mx,1,pt,pt);
	}
	if(res-yuan>=k) return 1;
	return 0;
}
int cmp(pair<ll,ll>xx,pair<ll,ll>yy){
	return xx.second<yy.second;
}
int main(){
	freopen("war5.in","r",stdin);
	freopen("war.out","w",stdout);
	ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>p[i].first>>p[i].second;
	}
	check1(0);
	yuan=res;
	l=1,r=290009;
	while(l<=r){
		mid=(l+r)>>1;
		if(check1(mid)){
			yes=1;
			r=mid-1;
		} else l=mid+1;
	}
	if(yes)
	cout<<r+1<<"\n";
	else cout<<-1<<"\n";
	sort(p+1,p+1+n,cmp);
	check2(0);
	yuan=res;
	l=1,r=880009;
	while(l<=r){
		mid=(l+r)>>1;
		if(check2(mid)){
			yes=-1;
			r=mid-1;
		} else l=mid+1;
	}
	cout<<ss<<"\n";
	if(yes==-1)
	cout<<r+1;
	else 
	cout<<-1;
}
2024/11/3 20:53
加载中...