题意:给出平面上$N$个点,求满足以下两个条件的矩形:①左下角与右上角各有一个点;②矩形内部没有点。$N \leq 2 \times 10^5$,所有数字大于等于$0$,保证坐标两两不同
最开始想到的是类似于楼房重建的算法,然后打炸了qwq
在多维问题上考虑分治可以降低一维限制,很多时候都会用到(比如三维偏序)。
我们先对$y$值从小到大排序,在分治内部对$x$值从小到大排序,然后考虑左边一半对右边的贡献。
可以知道对于左边一半的两个点$a,b(a<b)$,如果$y_a<y_b$,那么$a$会被$b$阻挡,就不会对答案产生贡献。所以我们可以维护一个$y$值递减的单调栈来保存可能对当前询问的点产生贡献的左边的点的集合,每一次询问下一个点时,把$x$值小于它的其他点加入单调栈即可。
接下来我们考虑右边的点对右边产生的影响(右边的某一些点阻挡了某一些左边的点产生贡献)。发现我们同样可以维护一个$y$值递增的单调栈来保存对当前点有影响的点的集合。每一次我们就取出栈顶的点,在左边的点对应的集合上二分,就可以得到它使多少个左边的点不产生贡献了。
//This code is written by Itst #include<bits/stdc++.h> #define BZ #define mid ((l + r) >> 1) using namespace std; inline int read(){ ; ; char c = getchar(); while(c != EOF && !isdigit(c)){ if(c == '-') f = ; c = getchar(); } while(c != EOF && isdigit(c)){ a = (a << ) + (a << ) + (c ^ '); c = getchar(); } return f ? -a : a; } ; struct node{ int x , y; }now[MAXN]; int N , hd , Stack[MAXN] , Stack2[MAXN] , top; long long ans; bool cmp1(node a , node b){ return a.y < b.y; } bool cmp2(node a , node b){ return a.x < b.x; } inline int bis(int x){ , R = hd; while(L < R){ >> ; now[Stack[m]].x < x ? L = m : R = m - ; } return L; } void solve(int l , int r){ if(l == r) return; solve(l , mid); solve(mid + , r); ; hd = top = ; while(p2 <= r){ while(top && now[Stack2[top]].y > now[p2].y) --top; Stack2[++top] = p2; while(p1 <= mid && now[p1].x < now[p2].x){ while(hd && now[Stack[hd]].y < now[p1].y) --hd; Stack[++hd] = p1++; } ans += hd - (top == ? : bis(now[Stack2[top - ]].x)); p2++; } sort(now + l , now + r + , cmp2); } int main(){ #ifdef BZ freopen("4237.in" , "r" , stdin); freopen("4237.out" , "w" , stdout); #endif N = read(); ; i <= N ; i++){ now[i].x = read(); now[i].y = read(); } sort(now + , now + N + , cmp1); solve( , N); cout << ans; ; }