跳到正文

题解

题解 #7862 Land Trade

Diaоsi约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 15 分钟

版本

  1. v1当前

Land Trade

nn 条直线只会将 LL 划分成 O(n2)\mathcal{O}(n^2) 个区域,每个区域都是一个凸多边形。把所有的多边形求出来,然后找到多边形内的一个点(比如重心),判断这个点是否满足逻辑表达式,若是则将面积计入答案。

如何求出所有的多边形呢?新加入一条直线只会产生 O(n)\mathcal{O}(n) 个新的交点,即只会增加 O(n2)\mathcal{O}(n^2) 个多边形。采用 convex cut 的思想,维护当前所有的多边形,判断是否被当前直线切开,然后将多边形分成两半。

时间复杂度 O(n3)\mathcal{O}(n^3)。不知道为啥代码写了这么长。

C++
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define poly vector<node>
typedef long long ll;
typedef long double ld;
using namespace std;
const int N=100010,INF=0x3f3f3f3f;

const ld eps=1e-11,pi=acos(-1),inf=1e18;

mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

int dcmp(ld x){return x<-eps?-1:(x>eps?1:0);}

struct node{
	ld x,y;
	node(ld xx=0,ld yy=0){x=xx,y=yy;}
	bool operator <(const node &a)const{return dcmp(x-a.x)?x<a.x:y<a.y;}
	bool operator ==(const node &a)const{return !dcmp(x-a.x)&&!dcmp(y-a.y);}
	node &operator +=(const node &b){x+=b.x,y+=b.y;return *this;}
	node &operator -=(const node &b){x-=b.x,y-=b.y;return *this;}
	node &operator *=(const ld &b){x*=b,y*=b;return *this;}
	node &operator /=(const ld &b){x/=b,y/=b;return *this;}
	friend istream &operator >>(istream &is,node &a){return is>>a.x>>a.y;}
	friend ostream &operator <<(ostream &os,node &a){return os<<'('<<a.x<<','<<a.y<<')';}
}O(0,0);

ld lx,rx,ly,ry;

node operator +(const node &a,const node &b){return node(a.x+b.x,a.y+b.y);}
node operator -(const node &a,const node &b){return node(a.x-b.x,a.y-b.y);}
node operator *(const ld &x,const node &a){return node(x*a.x,x*a.y);}
node operator /(const node &a,const ld &x){return node(a.x/x,a.y/x);}
ld operator *(const node &a,const node &b){return a.x*b.x+a.y*b.y;}
ld operator ^(const node &a,const node &b){return a.x*b.y-a.y*b.x;}

ld sqr(ld x){return x*x;}
ld len(node a){return sqrt(a*a);}
ld dist(node a,node b){return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));}
ld atan2(node a){return std::atan2(a.y,a.x);}
bool cmp2(const node &a,const node &b){return atan2(a-O)<atan2(b-O);}

node normal(node a){return node(-a.y,a.x);}

bool rectangle(node a){
	return lx-eps<=a.x&&a.x<=rx+eps&&ly-eps<=a.y&&a.y<=ry+eps;
}

bool contain(node a,node b,node c){
	node x=a-b,y=c-b;
	return (!dcmp(x^y))&&(dcmp(x*y)<=0);
}

bool judge(node a,node b,node c){
	return dcmp((b-a)^(c-a))>0;
}

node intersect(node a,node b,node c,node d){
	node x=b-a,y=d-c,z=a-c;
    return a+((y^z)/(x^y))*x;
}

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

struct line{
	ll a,b,c;
	line(ll aa=0,ll bb=0,ll cc=0){
		ll d=gcd(aa,gcd(bb,cc));
		a=aa/d,b=bb/d,c=cc/d;
		if(a<0)a=-a,b=-b,c=-c;
	}
	bool operator ==(const line &l)const{return a*l.b==b*l.a;}
};

node lineinter(line p,line q){
	ld a1=p.a,b1=p.b,c1=p.c;
	ld a2=q.a,b2=q.b,c2=q.c;
	ld det=a1*b2-a2*b1;
	if(!dcmp(det))return node(-inf,-inf);
	else return node((b1*c2-b2*c1)/det,(a2*c1-a1*c2)/det);
}

bool ole(node pa,node pb,node a){return dcmp((a-pa)^(pb-pa))<0;}
bool ori(node pa,node pb,node a){return dcmp((a-pa)^(pb-pa))>0;}

void out(poly p){
	cout<<"poly:\n";
	for(node it:p)cout<<it<<'\n';
}

node center(poly p){
	int n=(int)p.size();
	node C;
	for(node z:p)C+=z;
	return C/(ld)n;
}

ld area(poly p){
	int n=(int)p.size();
	ld S=0;
	#define nxt(i) i==n-1?0:i+1
	for(int i=0;i<n;i++)
		S+=p[i]^p[nxt(i)];
	#undef nxt
	return fabs(S)/2.0;
}

int n,vis[N];

ld ans;

bool inpoly(poly &p,node a){
	int n=(int)p.size();
	#define nxt(i) i==n-1?0:i+1
	O=p[0];
	if(dcmp((a-O)^(p[1]-O))>0
	||dcmp((p[n-1]-O)^(a-O))>0
	||(!dcmp((a-O)^(p[1]-O))&&!contain(p[1],a,p[0]))
	||(!dcmp((a-O)^(p[n-1]-O))&&!contain(p[n-1],a,p[0]))){O=node(0,0);return 0;}
	int i=lower_bound(p.begin(),p.end(),a,cmp2)-p.begin()-1;
	O=node(0,0);
	return dcmp((p[i]-a)^(p[nxt(i)]-p[i]))>=0;
	#undef nxt
}

string s;

vector<poly> b,tmp;
vector<line> v;

bool onside(poly p,line q){
	ll a=q.a,b=q.b,c=q.c;
	int n=(int)p.size(),ok0=0,ok1=0;
	for(int i=0;i<n;i++){
		if(a*p[i].x+b*p[i].y+c>0)ok0=1;
		if(a*p[i].x+b*p[i].y+c<0)ok1=1;
	}
	return ok0^ok1;
}

void hull(poly &p){
	int n=(int)p.size(),pos=0;
	poly b;
	for(int i=0;i<n;i++)
		if(p[i]<p[pos])pos=i;
	for(int i=pos;i<n;i++)b.pb(p[i]);
	for(int i=0;i<pos;i++)b.pb(p[i]);
	p=b;
}

void split(poly p,node a,node b){
	int n=(int)p.size(),pos=-1;
	poly ls,rs;
	#define nxt(i) i==n-1?0:i+1
	for(int i=0;i<n;i++)
		if(ori(a,b,p[i])&&
			!ori(a,b,p[nxt(i)]))pos=nxt(i);
	for(int i=pos,j=0;j<n;i=nxt(i),j++){
		if(p[i]==a||p[i]==b)continue;
		if(!ori(a,b,p[i]))ls.pb(p[i]);
		else rs.pb(p[i]);
	}
	ls.pb(a);ls.pb(b);
	rs.pb(b);rs.pb(a);
	hull(ls);hull(rs);
	if(ls.size()>2)tmp.pb(ls);
	if(rs.size()>2)tmp.pb(rs);
	#undef nxt
}

tuple<ll,ll,ll> trans(string s){
	vector<ll> z;
	for(int i=1,f=1,val=0;i<(int)s.size();i++){
		if(s[i]=='-')f=-1;
		else if(isdigit(s[i]))val=10*val+s[i]-'0';
		else z.pb(f*val),val=0,f=1;
	}
	return mt(z[0],z[1],z[2]);
}

int cnt;

struct ExprTree{
	int l,r;
	char c;
	tuple<ll,ll,ll> t;
	#define l(x) tree[x].l
	#define r(x) tree[x].r
	#define c(x) tree[x].c
	#define t(x) tree[x].t
}tree[N];

int build(int l,int r){
	int p=++cnt;
	if(s[l]=='['&&s[r]==']'){
		ll a,b,c;
		tie(a,b,c)=trans(s.substr(l,r-l+1));
		v.pb(line(a,b,c));
		c(p)='e';
		t(p)=mt(a,b,c);
		return p;
	}
	l++,r--;
	if(s[l]=='!'){
		c(p)=s[l];
		l(p)=build(l+1,r);
		return p;
	}
	int cnt0=0,cnt1=0;
	for(int i=l,j=r;i<=r&&j>=l;i++,j--){
		if(s[i]=='(')cnt0++;
		else if(s[i]==')')cnt0--;
		else if(!cnt0&&(s[i]=='&'||s[i]=='|'||s[i]=='^')){
			c(p)=s[i];
			l(p)=build(l,i-1);
			r(p)=build(i+1,r);
			return p;
		}
		if(s[j]=='(')cnt1++;
		else if(s[j]==')')cnt1--;
		else if(!cnt1&&(s[j]=='&'||s[j]=='|'||s[j]=='^')){
			c(p)=s[j];
			l(p)=build(l,j-1);
			r(p)=build(j+1,r);
			return p;
		}
	}
	assert(false);
	return p;
}

bool query(int p,node o){
	if(c(p)=='e'){
		ll a,b,c;
		tie(a,b,c)=t(p);
		return a*o.x+b*o.y+c>=0;
	}
	if(c(p)=='!')return !query(l(p),o);
	if(c(p)=='&')return query(l(p),o)&query(r(p),o);
	if(c(p)=='|')return query(l(p),o)|query(r(p),o);
	if(c(p)=='^')return query(l(p),o)^query(r(p),o);
	assert(false);
	return 0;
}

int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>lx>>rx>>ly>>ry>>s;
	n=(int)s.size();
	poly z;
	z.pb(node(lx,ly));z.pb(node(rx,ly));
	z.pb(node(rx,ry));z.pb(node(lx,ry));
	v.pb(line(0,1,-ly));
	v.pb(line(1,0,-rx));
	v.pb(line(0,1,-ry));
	v.pb(line(1,0,-lx));
	b.pb(z);
	build(0,n-1);
	shuffle(v.begin()+4,v.end(),rnd);
	for(int i=0;i<(int)v.size();i++){
		vector<node> z;
		for(int j=0;j<i;j++){
			if(v[i]==v[j])continue;
			node it=lineinter(v[i],v[j]);
			if(rectangle(it))z.pb(it);
		}
		tmp.clear();
		sort(z.begin(),z.end());
		z.erase(unique(z.begin(),z.end()),z.end());
		vector<int> id;
		for(int j=0;j<(int)b.size();j++){
			vis[j]=0;
			if(!onside(b[j],v[i]))id.pb(j);
		}
		for(int j=1;j<(int)z.size();j++){
			node A=z[j-1],B=z[j],C=(A+B)/2.0;
			for(int k:id)
				if(!vis[k]&&inpoly(b[k],C)){
					split(b[k],A,B);
					vis[k]=1;
				}
		}
		for(int j=0;j<(int)b.size();j++)
			if(!vis[j])tmp.pb(b[j]);
		b=tmp;
	}
	for(int i=0;i<(int)b.size();i++)
		if(query(1,center(b[i])))ans+=area(b[i]);
	cout<<fixed<<setprecision(15)<<ans<<'\n';
	return 0;
}
原文