bzoj 4237稻草人

神奇的分治??!!!

%%%http://blog.csdn.net/lych_cys/article/details/50923926

#include<bits/stdc++.h>
#define INF 0x7fffffff
#define LL long long
#define N 100005
using namespace std;
inline int ra()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int n,m,p[N<<1],q[N<<1]; struct node{ int x,y; }a[N<<1],b[N<<1];
LL ans;
bool cmpy(node a, node b)
{
	return a.y<b.y;
}
int find(int x, int l, int r)
{
	while (l+1<r)
	{
		int mid=l+r>>1;
		if (a[q[mid]].x<x) l=mid; else r=mid;
	}
	return l;
}
void solve(int l, int r)
{
	if (l==r) return;
	int mid=l+r>>1;
	solve(l,mid); solve(mid+1,r);
	int i,k,tp1=0,tp2=0,j=l;
	for (i=mid+1; i<=r; i++)
	{
		while (tp1 && a[i].y<a[p[tp1]].y) tp1--;
		p[++tp1]=i;
		for (; a[j].x<a[i].x && j<=mid; j++)
		{
			while (tp2 && a[j].y>a[q[tp2]].y) tp2--;
			q[++tp2]=j;
		}
		ans+=tp2-find(a[p[tp1-1]].x,0,tp2+1);
	}
	j=l; k=mid+1;
	for (i=l; i<=r; i++)
		b[i]=(j<=mid && a[j].x<a[k].x || k>r)?a[j++]:a[k++];
	for (i=l; i<=r; i++) a[i]=b[i];
}
int main()
{
	n=ra();
	for (int i=1; i<=n; i++)
		a[i].x=ra(),a[i].y=ra();
	sort(a+1,a+n+1,cmpy);
	solve(1,n);
	cout<<ans;
	return 0;
}

  

posted @ 2017-02-07 17:25  ws_ccd  阅读(124)  评论(0编辑  收藏  举报