[USACO13NOV]Crowded Cows

题目链接-洛谷

#include<cstdio>
#define MAXN 500005
using namespace std;

int x[MAXN];
int h[MAXN];

int dq1[MAXN],dq2[MAXN];

inline void qs(int l,int r){
	int i=l,j=r,s=x[(l+r)>>1],t;
	while(true){
		while(x[i]<s)++i;
		while(x[j]>s)--j;
		if(i<=j){
			t = x[i];x[i] = x[j];x[j] = t;
			t = h[i];h[i] = h[j];h[j] = t;
			++i;--j;
		}
		if(i>j)break;
	}
	if(i<r)qs(i,r);
	if(l<j)qs(l,j);
}

int N,D;
int l1 = 1;
int r1 = 0;
int l2 = 1;
int r2 = 0;

int main(){
	
	scanf("%d%d",&N,&D);
	for(register int i=1;i<=N;++i){
		scanf("%d%d",&x[i],&h[i]);
	}
	qs(1,N);
	
	int ans = 0;
	dq1[1] = h[1];
	r1=1;
	
	int head = 1;
	int tail = 3;
	
	while(tail<=N&&x[2]+D>=x[tail]){
		int y = h[tail++];
		while(l2<=r2&&dq2[r2]<y)r2--;
		dq2[++r2]=y;
	}
	
	for(register int i=2;i<N;++i){
		
		int num1 = dq1[l1]>>1;int num2 = dq2[l2]>>1;
		if(num1>=h[i]&&num2>=h[i])ans++;
		
		int j = i+1;
		if(j==N)continue;
		
		while(head<j&&x[j]-D>x[head]){
			if(h[head++]==dq1[l1])l1++;
		}
		
		if(x[i]>=x[j]-D){
			while(l1<=r1&&dq1[r1]<h[i])r1--;
			dq1[++r1]=h[i];
		}
		
		if(h[j]==dq2[l2])l2++;
		
		while(tail<=N&&x[j]+D>=x[tail]){
			int y = h[tail++];
			while(l2<=r2&&dq2[r2]<y)r2--;
			dq2[++r2]=y;
		}
	}
	
	printf("%d",ans);
	return 0;
}

posted @ 2018-03-03 11:39  Neworld1111  阅读(177)  评论(0编辑  收藏  举报