跳到正文

个人记录

【模板】极角排序

yqzx0307约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年11月6日
收录
2026年11月6日
更新
2026年11月13日
篇幅
约 6 分钟

版本

  1. v1当前

叉乘+半平面判定

C++
//#pragma GCC optimize("-Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=1e6+10;
constexpr int maxm=5e3+10;
constexpr int maxk=(1<<10)+10;
constexpr int log2n=20;
constexpr int mod=998244353;
constexpr int INF=1e15;
constexpr double eps=1e-8;
constexpr double pi=acosl(-1.0);
mt19937_64 rd(random_device{}());
uniform_real_distribution<double> qr(0.0,1.0);
int n,m;
typedef struct node
{
	int x,y;
	node()=default;
	node(int a,int b):x(a),y(b){}
	int operator*(const node &o) const
	{
		return x*o.y-y*o.x;
	}
	bool half() const
	{
		if((!y&&x<0)||y<0)
		{
			return 1;
		}
		return 0;
	}
	bool operator<(const node& o) const
	{
		bool a=this->half();
		bool b=o.half();
		if(a!=b)
		{
			return a<b;
		}
		int cross=(*this)*o;
		return cross>0;
	}
}node;
node p[maxn];
int calc(node u,node v)
{
	int l=lower_bound(p+1,p+n+1,u)-p;
	int r=upper_bound(p+1,p+n+1,v)-p;
	if(u<v)
	{
		return r-l;
	}
	else
	{
		return n-l+r;
	}
}
signed main()
{
    #ifdef lcpz
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
    #endif

	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%lld%lld",&p[i].x,&p[i].y);
	}
	sort(p+1,p+n+1);
	node pre=node(1,0);
	for(int i=1,u,v;i<=m;++i)
	{
		scanf("%lld%lld",&u,&v);
		node now=node(u,v);
		int ans1=calc(pre,now);
		int ans2=calc(now,pre);
		int ans=ans1>ans2?ans1:ans2;
		printf("%lld\n",ans);
		pre=now;
	}
	return 0;
}
//&#x1F62D; &#x1F449;

https://www.luogu.com.cn/problem/P7133

atan2 求角度

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=1e6+10;
constexpr int maxm=3e5+10;
constexpr int INF=1e18;
constexpr double pi=acos(-1.0);
constexpr double eps=1e-10;
int n;
double dx,dy,ans;
typedef struct node
{
    double x,y;
    double angle;
    node()=default;
    node(double a,double b,double c)
    {
        this->x=a;
        this->y=b;
        this->angle=c;
    }
    bool operator <(const node& l) const
    {
        return this-> angle<l.angle;
    }
};
node line[maxn];
signed main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;++i)
    {
        double x,y;
        scanf("%lf%lf",&x,&y);
        line[i].x=x;
        line[i].y=y;
        if(!x&&!y)
        {
            --i;
            --n;
        }
        else
        {
            line[i].angle=atan2(line[i].y,line[i].x);
        }
    }
    sort(line+1,line+n+1);
    for(int i=1;i<=n;++i)
    {
        line[i+n]=line[i];
        line[i+n].angle+=2*pi;
    }
    for(int l=1,r=1;l<=n;++l)
    {
        while(r<n*2&&line[r].angle-line[l].angle<pi+eps)
        {
            dx+=line[r].x;
            dy+=line[r].y;
            ++r;
            ans=max(ans,dx*dx+dy*dy);
        }
        dx-=line[l].x;
        dy-=line[l].y;
        ans=max(ans,dx*dx+dy*dy);
    }
    printf("%.3lf",ans);
    return 0;
}

https://www.luogu.com.cn/problem/P10671

原文