说在前面
早早的计划了,要把这道题干掉
中午写了题解,晚上7点半第一发提交,8点半首次AC,发现这份代码貌似有拿下Rank1的潜质,于是决定优化一波代码。然后就和zyc一起优化到了9点半….
zyc实在是太菜了,优化半天反而变慢hhhhh
题目
题目大意
给出平面上N个点(保证点的坐标非负,且没有两个点的横坐标 或 纵坐标相同),请求出符合以下条件的矩形的个数:
- 矩形的左下角和右上角有一个点
- 矩形的内部没有其他点
输入输出格式
输入格式:
第一行一个整数N,表示点的个数
接下来N行,每行两个数x,y描述一个点
输出格式:
输出一行一个整数表示答案
解法
感觉me写多了sabi数据结构之后,正常思考的功能都丧失了…
题目要求的是符合这样条件的矩形:
- 矩形的左下角和右上角有一个点
- 矩形内部没有点
在纸上画一画(或者脑补一番),大概能感受到这个约束条件,其实是 二维偏序 与 合法点分布的单调性(固定右上角点,那么合法的左下角点一定是呈 从左上到右下 的方式分布,单调栈既视感)的结合体
二维偏序上再加约束,于是想到CDQ。以横坐标排序,然后二分纵坐标,把大于mid的点丢到上面,小于mid的点丢到下面。现在累计所有右上角点的贡献,同侧点之间的贡献可以看作子问题递归处理,因此只需要讨论异侧点之间的贡献。
可以发现,下侧的点可能会对上侧的点产生贡献,并且有贡献的点分布 一定是从左上到右下的(这个可以用单调下降的栈维护)。当然下侧的点也有可能被上侧的点挡住(到这里me就不知道该怎么办了…哎…真是滞胀),所以要把被挡住的点的贡献减掉。一个上侧点的贡献会被 在它左边第一个比他矮的上侧的点 挡住,找到这个点(这里可以用一个单调上升的单调栈维护,一开始me脑抽了用的树状数组维护),然后再在下侧的单调栈里二分确定挡住点的个数,在ans里减掉即可。
然后这道题就做完了= =…心疼自己1.5 seconds
下面是自带大长度的代码
原版代码:
/**************************************************************
Problem: 4237
User: Izumihanako
Language: C++
Result: Accepted
Time:4544 ms
Memory:8640 kb
****************************************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
long long ans ;
int N ;
struct Unique_Data{
int num , id ;
bool operator < ( const Unique_Data &A ) const {
return num < A.num ;
}
}U[200005] ;
struct Points{
int x , y ;
bool operator < ( const Points &A ) const {
return x < A.x ;
}
}p[200005] , Ltmp[200005] , Rtmp[200005] ;
void Unique_(){
sort( U + 1 , U + N + 1 ) ;
for( register int i = 1 ; i <= N ; i ++ )
p[ U[i].id ].y = i ;
}
int downs[200005] , down_topp , ups[200005] , up_topp ;
int get_pos( int *sta , const int &topp , const int &aim ){//单调栈中x本身有序
int lf = 1 , rg = topp , rt = 0 ;
while( lf <= rg ){
int mid = ( lf + rg ) >> 1 ;
if( p[ sta[mid] ].x <= aim ){//找第一个小的
rt = mid ;
lf = mid + 1 ;
} else rg = mid - 1 ;
}
return rt ;
}
void divc( int st , int ed , int down , int up ){
int mid = ( up + down ) >> 1 , Ltop = 0 , Rtop = 0 ;
up_topp = down_topp = 0 ;
for( int i = st ; i <= ed ; i ++ ){
if( p[i].y > mid ){
Rtmp[++Rtop] = p[i] ;
while( up_topp && p[ ups[up_topp] ].y > p[i].y )
up_topp -- ;
ans += down_topp - get_pos( downs , down_topp , p[ ups[up_topp] ].x ) ;
ups[++up_topp] = i ;
} else {
Ltmp[++Ltop] = p[i] ;
while( down_topp && p[ downs[down_topp] ].y < p[i].y )
down_topp -- ;
downs[++down_topp] = i ;
}
}
if( down != mid ) memcpy( p + st , Ltmp + 1 , Ltop * sizeof( Points ) ) ;
if( mid+1 != up ) memcpy( p + st + Ltop , Rtmp + 1 , Rtop * sizeof( Points ) ) ;
if( down != mid ) divc( st , st + Ltop - 1 , down , mid ) ;
if( mid+1 != up ) divc( st + Ltop , ed , mid+1 , up ) ;
}
void solve(){
sort( p + 1 , p + N + 1 ) ; //外层保证x有序
divc( 1 , N , 1 , N ) ;//内层二分保证y有序( 已经离散化 )
printf( "%lld" , ans ) ;
}
inline void read_( int &x ){
x = 0 ;
char ch = getchar() ;
while( ch < '0' || ch > '9' ) ch = getchar() ;
while( ch >='0' && ch <='9' ) x = (x<<1)+(x<<3) + ch - '0' , ch = getchar() ;
}
int main(){
scanf( "%d" , &N ) ;
for( int i = 1 ; i <= N ; i ++ ){
read_( p[i].x ) , read_( p[i].y ) ;
U[i] = ( Unique_Data ){ p[i].y , i } ;
}
p[0] = ( Points ){ -0x3f3f3f3f , -0x3f3f3f3f } ;
Unique_() ;
solve() ;
}
不忍直视的代码
暂时Rank1的代码:
/**************************************************************
Problem: 4237
User: Izumihanako
Language: C++
Result: Accepted
Time:3356 ms
Memory:8672 kb
****************************************************************/
#include <cstdio>
#include <cstring>
#include <algorithm>
#define getc() (xS==xTT&&(xTT=(xS=xB)+fread(xB,1,1<<15,stdin),xS==xTT)?0:*xS++)
using namespace std ;
char xch,xB[1<<15],*xS=xB,*xTT=xB;
long long ans ;
int N , maxy ;
struct Unique_Data{
int num , id ;
bool operator < ( const Unique_Data &A ) const {
return num < A.num ;
}
}U[200005] ;
struct Points{
int x , y ;
bool operator < ( const Points &A ) const {
return x < A.x ;
}
}p[200005] , Ltmp[200005] , Rtmp[200005] ;
void Unique_(){
sort( U + 1 , U + N + 1 ) ;
for( register int i = 1 ; i <= N ; i ++ )
p[ U[i].id ].y = i ;
}
int downs[200005] , down_topp , ups[200005] , up_topp ;
int get_pos( int *sta , const int &topp , const int &aim ){//单调栈中x本身有序
int lf = 1 , rg = topp , rt = 0 ;
while( lf <= rg ){
int mid = ( lf + rg ) >> 1 ;
if( p[ sta[mid] ].x <= aim ){//找第一个小的
rt = mid ;
lf = mid + 1 ;
} else rg = mid - 1 ;
}
return rt ;
}
void divc( int st , int ed , int down , int up ){
int mid = ( up + down ) >> 1 , Ltop = 0 , Rtop = 0 ;
up_topp = down_topp = 0 ;
for( int i = st ; i <= ed ; i ++ ){
if( p[i].y > mid ){
Rtmp[++Rtop] = p[i] ;
while( up_topp && p[ ups[up_topp] ].y > p[i].y )
up_topp -- ;
ans += down_topp - get_pos( downs , down_topp , p[ ups[up_topp] ].x ) ;
ups[++up_topp] = i ;
} else {
Ltmp[++Ltop] = p[i] ;
while( down_topp && p[ downs[down_topp] ].y < p[i].y )
down_topp -- ;
downs[++down_topp] = i ;
}
}
if( down != mid && Ltop > 1 ) memcpy( p + st , Ltmp + 1 , Ltop * sizeof( Points ) ) ;
if( mid+1 != up && Rtop > 1 ) memcpy( p + st + Ltop , Rtmp + 1 , Rtop * sizeof( Points ) ) ;
if( down != mid && Ltop > 1 ) divc( st , st + Ltop - 1 , down , mid ) ;
if( mid+1 != up && Rtop > 1 ) divc( st + Ltop , ed , mid+1 , up ) ;
}
void solve(){
sort( p + 1 , p + N + 1 ) ; //外层保证x有序
divc( 1 , N , 0 , maxy ) ;//内层二分保证y有序( 已经离散化 )
printf( "%lld" , ans ) ;
}
inline void read_( int &x ){
x = 0 ;
char ch = getc() ;
while( ch < '0' || ch > '9' ) ch = getc() ;
while( ch >='0' && ch <='9' ) x = (x<<1)+(x<<3) + ch - '0' , ch = getc() ;
}
int main(){
scanf( "%d" , &N ) ;
for( int i = 1 ; i <= N ; i ++ ){
read_( p[i].x ) , read_( p[i].y ) ;
maxy = max( maxy , p[i].y ) ;
// U[i] = ( Unique_Data ){ p[i].y , i } ;
}
p[0] = ( Points ){ -0x3f3f3f3f , -0x3f3f3f3f } ;
// Unique_() ;
solve() ;
}