zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    ABP 往前端返回详细的错误信息
    ABP 报错1
    three.js 测试1
    three.js 添加 图形控制界面 gui
    three.js 设置透明度
    three.js 基础使用1
    three.js 添加环境光
    three.js 添加三维坐标系
    P2690 接苹果
    [USACO08FEB]修路Making the Grade
  • 原文地址:https://www.cnblogs.com/ccd2333/p/6375116.html
Copyright © 2011-2022 走看看