• BZOJ4237:稻草人


    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4237

    考虑按(y)值从小到大排序之后分治。完全在下半部分和上半部分的田我们递归去统计,问题在于怎么计算跨越了(mid)的田。

    我们把([l,mid])([mid+1,r])(x)排序,然后从(mid+1)(r)枚举右上角。

    对于下半部分我们维护一个(y)值单调递减的栈,每次把(x)值小于等于当前枚举的点的(x)值的点加进来;上半部分维护一个(y)单调上升的栈,所以(i)号点可以与下半部分的栈里面(x)值比(i)在栈里面前一个点的(x)值配对形成一块田。这个二分就好了。

    时间复杂度:(O(nlog^2n))

    空间复杂度:(O(n))

    代码如下:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    
    const int maxn=2e5+5;
    
    ll ans;
    int n,top1,top2;
    int stk1[maxn],stk2[maxn];
    
    int read() {
    	int x=0,f=1;char ch=getchar();
    	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
    	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
    	return x*f;
    }
    
    struct point {
    	int x,y;
    
    	bool operator<(const point &a)const {
    		return y<a.y;
    	}
    }p[maxn];
    
    bool cmp(point a,point b) {
    	return a.x<b.x;
    }
    
    void solve(int l,int r) {
    	if(l==r)return;
    	int mid=(l+r)>>1;
    	solve(l,mid),solve(mid+1,r);
    	sort(p+l,p+mid+1,cmp);
    	sort(p+mid+1,p+r+1,cmp);
    	int pos=l;top1=top2=0;
    	for(int i=mid+1;i<=r;i++) {
    		while(top1&&p[stk1[top1]].y>p[i].y)top1--;
    		stk1[++top1]=i;
    		while(pos<=mid&&p[pos].x<p[i].x) {
    			while(top2&&p[stk2[top2]].y<p[pos].y)top2--;
    			stk2[++top2]=pos;pos++;
    		}
    		int l=1,r=top2,lmt=p[stk1[top1-1]].x;
    		while(l<r) {
    			int mid=(l+r)>>1;
    			if(p[stk2[mid]].x>lmt)r=mid;
    			else l=mid+1;
    		}
    		if(p[stk2[r]].x>lmt)ans+=top2-r+1;
    	}
    }
    
    int main() {
    	n=read();
    	for(int i=1;i<=n;i++)
    		p[i].x=read(),p[i].y=read();
    	sort(p+1,p+n+1);
    	p[0].x=-1,solve(1,n);
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    Django模型层之ORM
    bzoj1037 [ZJOI2008]生日聚会
    bzoj4423 [AMPPZ2013]Bytehattan
    bzoj1018 [SHOI2008]堵塞的交通
    关于弦图一些问题的解法
    bzoj1006 [HNOI2008]神奇的国度
    bzoj2561 最小生成树
    bzoj3720 Gty的妹子树
    bzoj3489 A simple rmq problem
    bzoj4066 简单题
  • 原文地址:https://www.cnblogs.com/AKMer/p/10430431.html
Copyright © 2020-2023  润新知