当前位置:网站首页>Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]

Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]

2022-06-11 07:30:00 Master. Yi

Title Description

Link

Two dimensional plane n n n A little bit ( x i , y i ) (x_i,y_i) (xi,yi), Q Q Q Query distance ( 0 , z ) (0,z) (0,z) Less than or equal to R R R The number of points .

n ≤ 12000 , Q ≤ 1 0 6 , ∣ x i ∣ , ∣ y i ∣ , ∣ z i ∣ , R ≤ 1 0 9 n\le12000,Q\le10^6,|x_i|,|y_i|,|z_i|,R\le10^9 n12000,Q106,xi,yi,zi,R109

Topic analysis

x 2 + ( y − z ) 2 ≤ R 2 x^2+(y-z)^2\le R^2 x2+(yz)2R2

x 2 + y 2 ≤ R 2 − z 2 + 2 y z x^2+y^2\le R^2-z^2+2yz x2+y2R2z2+2yz

( x , y ) → ( y , x 2 + y 2 ) (x,y)\to(y,x^2+y^2) (x,y)(y,x2+y2)

y ′ ≤ k x ′ + b , k = 2 z , b = R 2 − z 2 y'\le kx'+b,k=2z,b=R^2-z^2 ykx+b,k=2z,b=R2z2

The problem is to find the number of points under a line in the plane . Maintain the partial order relationship of each element under the slope determination , Asking can be based on b b b Two points. .

y − x k ≤ b y-xk\le b yxkb

If for a certain k k k, x 1 x_1 x1 be better than x 2 x_2 x2, x 1 < x 2 x_1<x_2 x1<x2

be y 1 − x 1 k ≤ y 2 − x 2 k y_1-x_1k\le y_2-x_2k y1x1ky2x2k

namely y 1 − y 2 ≤ k ( x 1 − x 2 ) * y 1 − y 2 x 1 − x 2 ≥ k y_1-y_2\le k(x_1-x_2) \Longleftrightarrow \frac {y_1-y_2}{x_1-x_2}\ge k y1y2k(x1x2)*x1x2y1y2k

Initially, it was assumed that k = − ∞ k=-\infty k=, k k k Gradually increase .

if x 1 = x 2 x_1=x_2 x1=x2, be y y y Small henggengyou .

So the initial sorting is based on x x x For the first keyword , y y y For the second keyword .

Maintain such a partially ordered sequence , When k k k Reach a certain value ( ≤ n ( n − 1 ) / 2 \le n(n-1)/2 n(n1)/2 Secondary exchange ) when , Exchange the order of the elements .

If the swapped position is not adjacent , It means that they coincide with the exchange time of the intermediate elements , The operation of time coincidence can be switched arbitrarily . Cross out the preceding paragraph , fake .

This is equivalent to saying that if you change the exchange process of a selection sort in reverse order to n ( n − 1 ) / 2 n(n-1)/2 n(n1)/2 The exchange of sub random order can still be arranged in reverse order ( Obviously false , such as 1 2 3 4 If you swap (1,2),(1,3),(3,4), that 1 It's impossible to get to the end ).

Therefore, when the exchange operation time is the same, it needs to ensure that it is a process similar to the selection sorting process , That is, operations with the same exchange time are sorted by the first dimension , And then sort by the second dimension .

Note that in this way, we still cannot guarantee that the adjacent two bits are exchanged each time , Because there may be multiple points distributed in two coordinates , But sorting is normal .

This section can be understood as a rotating scanning line , What needs to be considered is the case of multi-point collinearity and multi-point co coordinates .

The complexity of doing it directly is O ( n 2 log ⁡ n + Q log ⁡ n ) O(n^2\log n+Q\log n) O(n2logn+Qlogn)

If the point is divided into several blocks , The size of the score block is S S S, Then the complexity becomes O ( ( S 2 + Q ) log ⁡ n ∗ n S ) O((S^2+Q)\log n*\frac nS) O((S2+Q)lognSn)

S = Q S=\sqrt Q S=Q Best when , O ( n Q log ⁡ n ) O(n\sqrt Q\log n) O(nQlogn)

PS: If the coordinates of the center of the circle can be taken arbitrarily , The problem becomes counting points in the three-dimensional half plane ( However, it seems that the author said that he would not ? Manual formation

Code:

#include<bits/stdc++.h>
#define maxn 12005
#define maxq 1000005
#define LL long long
using namespace std;
char cb[1<<20],*cs,*ct;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<20,stdin),cs==ct)?0:*cs++)
template<class T>void read(T &a){
    
	char c;bool f=0;while(!isdigit(c=getc())) c=='-'&&(f=1);
	for(a=c-'0';isdigit(c=getc());a=a*10+c-'0'); f&&(a=-a);
}
inline void write(int x){
    
	if(x>=10) write(x/10);
	putchar(x%10+48);
}
const int S = 1000;
int n,Q,ans[maxq];
struct node{
    
	LL x,y; int id;
	void init1(){
    read(y),read(x),y=x*x+y*y;}// x<=1e9
	void init2(int i){
    read(x),read(y),y=y*y-x*x,x*=2,id=i;}// x<=2e9
	bool operator < (const node &p)const{
    return x^p.x?x<p.x:y<p.y;}
}a[maxn],q[maxq],b[maxn];
struct change{
    
	double t;
	int x,y;
	bool operator < (const change &p)const{
    return t==p.t?x^p.x?x<p.x:y<p.y:t<p.t;}
}C[S*S]; int Cn;
int pos[maxn];
void mdf(int i,int j){
    
	if(pos[i]>pos[j]) return;
	//assert(pos[j]-pos[i]==1); assert failed.
	swap(b[pos[i]],b[pos[j]]);
	swap(pos[i],pos[j]);
}
void solve(node *a,int n){
    
	sort(a+1,a+1+n);
	Cn=0;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++) if(a[i].x!=a[j].x){
    
			double t=1.0*(a[i].y-a[j].y)/(a[i].x-a[j].x); if(t>=q[Q].x) continue;
			C[++Cn]=(change){
    t,i,j};
		}
	sort(C+1,C+1+Cn);
	for(int i=1;i<=n;i++) pos[i]=i;
	for(int i=1,j=1;i<=Q;i++){
    
		for(;j<=Cn&&C[j].t<q[i].x;j++) mdf(C[j].x,C[j].y);
		int l=0,r=n,mid;
		while(l<r) mid=(l+r+1)>>1,a[mid].y-a[mid].x*q[i].x<=q[i].y?l=mid:r=mid-1;
		ans[q[i].id]+=l;
	}
}
int main()
{
    
	read(n),read(Q);
	for(int i=1;i<=n;i++) a[i].init1();
	for(int i=1;i<=Q;i++) q[i].init2(i);
	sort(q+1,q+1+Q);
	for(int i=1;i<=n;i+=S){
    
		for(int j=1;j<=S&&i+j-1<=n;j++) b[j]=a[i+j-1];
		solve(b,min(S,n-i+1));
	}
	for(int i=1;i<=Q;i++) write(ans[i]),putchar('\n');
}
原网站

版权声明
本文为[Master. Yi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020520542832.html