题意

PDF

分析

可以看成直线切割多边形,直接维护。

对每个多边形考虑每条边和每个点即可。

时间复杂度?不过\(n,m \leq 20\)这种数据怎么都过了。据说是\(O(n^3)\)的,而且常数也挺小。

一般的比赛估计不会出这种vector套vector的神题。

代码

注意初始化的时候的那个初始长方形的点必须按逆时针顺序来插入。

我写反了,导致调了一晚上。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read()
{
rg T data=0;
rg int w=1;
rg char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
{
data=data*10+ch-'0';
ch=getchar();
}
return data*w;
}
template<class T>T read(T&x)
{
return x=read<T>();
}
using namespace std;
typedef long long ll; co double eps=1e-8; int dcmp(double x)
{
return fabs(x)<eps?0:(x<0?-1:1);
} struct Point
{
double x,y; Point(double x=0,double y=0)
:x(x),y(y){};
};
typedef Point Vector;
typedef vector<Point> Polygon; Vector operator+(co Vector&A,co Vector&B)
{
return Vector(A.x+B.x,A.y+B.y);
} Vector operator-(co Vector&A,co Vector&B)
{
return Vector(A.x-B.x,A.y-B.y);
} Vector operator*(co Vector&A,double p)
{
return Vector(A.x*p,A.y*p);
} double Dot(co Vector&A,co Vector&B)
{
return A.x*B.x+A.y*B.y;
} double Cross(co Vector&A,co Vector&B)
{
return A.x*B.y-A.y*B.x;
} double Length2(co Vector&A)
{
return Dot(A,A);
} Point LineLineIntersection(co Point&P,co Vector&v,co Point&Q,co Vector&w)
{
Vector u=P-Q;
double t=Cross(w,u)/Cross(v,w);
return P+v*t;
} bool OnSegment(co Point&p,co Point&a1,co Point&a2)
{
return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0;
} double PolygonArea(co Polygon&poly)
{
double area=0;
int n=poly.size();
for(int i=1;i<n-1;++i)
area+=Cross(poly[i]-poly[0],poly[(i+1)%n]-poly[0]);
return area/2;
} Polygon CutPolygon(co Polygon&poly,co Point&A,co Point&B)
{
Polygon newpoly;
int n=poly.size();
for(int i=0;i<n;++i)
{
co Point&C=poly[i];
co Point&D=poly[(i+1)%n];
if(dcmp(Cross(B-A,C-A))>=0)
newpoly.push_back(C);
if(dcmp(Cross(B-A,C-D))!=0)
{
Point ip=LineLineIntersection(A,B-A,C,D-C);
if(OnSegment(ip,C,D))
newpoly.push_back(ip);
}
}
return newpoly;
} int PointInPolygon(co Point&p,co Polygon&v)
{
int wn=0;
int n=v.size();
for(int i=0;i<n;++i)
{
if(OnSegment(p,v[i],v[(i+1)%n]))
return -1;
int k=dcmp(Cross(v[(i+1)%n]-v[i],p-v[i]));
int d1=dcmp(v[i].y-p.y);
int d2=dcmp(v[(i+1)%n].y-p.y);
if(k>0&&d1<=0&&d2>0)
++wn;
if(k<0&&d2<=0&&d1>0)
--wn;
}
if(wn!=0)
return 1;
return 0;
} bool InCircle(co Point&p,co Point&center,double R)
{
return dcmp(Length2(p-center)-R*R)<0;
} int LineCircleIntersection(co Point&A,co Point&B,co Point&C,double r,double&t1,double&t2)
{
double a=B.x-A.x,
b=A.x-C.x,
c=B.y-A.y,
d=A.y-C.y; double e=a*a+c*c,
f=2*(a*b+c*d),
g=b*b+d*d-r*r,
delta=f*f-4*e*g;
if(dcmp(delta)<0)
return 0;
if(dcmp(delta)==0)
{
t1=t2=-f/(2*e);
return 1;
}
t1=(-f-sqrt(delta))/(2*e);
t2=(-f+sqrt(delta))/(2*e);
return 2;
} bool CircleIntersectSegment(co Point&A,co Point&B,co Point&p,double R)
{
double t1,t2;
int c=LineCircleIntersection(A,B,p,R,t1,t2);
if(c<=1)
return 0;
if(dcmp(t1)>0&&dcmp(t1-1)<0)
return 1;
if(dcmp(t2)>0&&dcmp(t2-1)<0)
return 1;
return 0;
} vector<Polygon> pieces,new_pieces; void Cut(int x1,int y1,int x2,int y2)
{
new_pieces.clear();
for(int i=0;i<pieces.size();++i)
{
Polygon left=CutPolygon(pieces[i],Point(x1,y1),Point(x2,y2));
Polygon right=CutPolygon(pieces[i],Point(x2,y2),Point(x1,y1));
if(left.size()>=3)
new_pieces.push_back(left);
if(right.size()>=3)
new_pieces.push_back(right);
// cerr<<"left="<<endl;
// for(int j=0;j<left.size();++j)
// cerr<<" "<<left[j].x<<","<<left[j].y;
// cerr<<endl;
// cerr<<"right="<<endl;
// for(int j=0;j<right.size();++j)
// cerr<<" "<<right[j].x<<","<<right[j].y;
// cerr<<endl;
}
pieces=new_pieces;
} bool DiscIntersectPolygon(co Polygon&poly,co Point&p,double R)
{
if(PointInPolygon(p,poly)!=0)
return 1;
if(InCircle(poly[0],p,R))
return 1;
int n=poly.size();
for(int i=0;i<n;++i)
{
if(CircleIntersectSegment(poly[i],poly[(i+1)%n],p,R))
return 1;
if(InCircle((poly[i]+poly[(i+1)%n])*0.5,p,R))
return 1;
}
return 0;
} void Query(co Point&p,int R)
{
vector<double>ans;
for(int i=0;i<pieces.size();++i)
if(DiscIntersectPolygon(pieces[i],p,R))
ans.push_back(fabs(PolygonArea(pieces[i])));
printf("%d",ans.size());
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();++i)
printf(" %.2lf",ans[i]);
printf("\n");
} int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n,m,L,W;
while(read(n)|read(m)|read(L)|read(W))
{
// cerr<<"n="<<n<<" m="<<m<<" L="<<L<<" W="<<W<<endl;
pieces.clear(); Polygon bbox;
bbox.push_back(Point(0,0)); // edit 1:must follow the order
bbox.push_back(Point(L,0));
bbox.push_back(Point(L,W));
bbox.push_back(Point(0,W));
pieces.push_back(bbox); for(int i=0;i<n;++i)
{
int x1,y1,x2,y2;
read(x1),read(y1),read(x2),read(y2);
// cerr<<"x1="<<x1<<" y1="<<y1<<" x2="<<x2<<" y2="<<y2<<endl;
Cut(x1,y1,x2,y2);
// for(int i=0;i<pieces.size();++i)
// {
// cerr<<i<<" poly="<<endl;
// for(int j=0;j<pieces[i].size();++j)
// cerr<<" "<<pieces[i][j].x<<","<<pieces[i][j].y;
// cerr<<endl;
// }
} for(int i=0;i<m;++i)
{
int x,y,R;
read(x),read(y),read(R);
Query(Point(x,y),R);
}
printf("\n");
}
return 0;
}

UVA12296 Pieces and Discs的更多相关文章

  1. uva 12296 Pieces and Discs

    题意: 有个矩形,左下角(0,0),左上角(L,W). 思路: 除了圆盘之外,本题的输入也是个PSLG,因此可以按照前面叙述的算法求出各个区域:只需把线段视为直线,用切割凸多边形的方法 :每次读入线段 ...

  2. uva 12296 Pieces and Discs (Geometry)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. HITOJ 2662 Pieces Assignment(状压DP)

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  4. 1.4.8 拼凑在一起(putting the pieces together)

    putting the pieces together 在最高的级别,schema.xml结构如下, <schema> <types> <fields> <u ...

  5. Pizza pieces

    Pizza pieces Description In her trip to Italy, Elizabeth Gilbert made it her duty to eat perfect piz ...

  6. HDU 4628 Pieces(DP + 状态压缩)

    Pieces 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4628 题目大意:给定一个字符串s,如果子序列中有回文,可以一步删除掉它,求把整个序列删除 ...

  7. Codeforces 328B-Sheldon and Ice Pieces(馋)

    B. Sheldon and Ice Pieces time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. 2017 NAIPC A:Pieces of Parentheses

    my team solve the problem in the contest with similar ideathis is a more deep analysis The main idea ...

  9. hdu 4628 Pieces 状态压缩dp

    Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

随机推荐

  1. mysql 5.6 设置root初始密码正确步骤,避免入坑

    http://blog.csdn.net/lw_power/article/details/47368167

  2. ThinkPHP关于模板的一些嵌套、IF判断使用

    > ##### 前言,现在有一组数据(涉及到3个数据表,order订单表,order_process办理流程表,process_status流程描述表),根据当前订单,展示相应信息 1.办理流程 ...

  3. maven项目Dao层优化

    平时我们习惯一个实体类就对应一个dao类,这样做,增删改查都大同小异,只是实体类对象不一样而已,因此,我们可以把公用的方法抽取来,建立一个IBaseDao接口,如下: public interface ...

  4. 有意思的随机数 Random

    今天看到stackoverflow上一篇关于随机数的提问,关于如何用随机数生成 "hello world"的有趣问题 : 大家都知道JDK里面的Random类生成随机数其实并不是真 ...

  5. Spring -- spring 中使用jdbc, c3p0连接池, dao概念

    1. 示例代码 CustomerDao.java  ,dao接口 public interface CustomerDao { public void insertCustomer(Customer ...

  6. jmeter-执行多个sql查询语句

    1.添加jdbc connection(注意标红部分) 2.添加jdbc request 3.查看结果树

  7. Web Service简介

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  8. SQLite 插入大量数据慢的解决方法

    sqlite 插入数据很慢的原因:sqlite在没有显式使用事务的时候会为每条insert都使用事务操作,而sqlite数据库是以文件的形式存在磁盘中,就相当于每次访问时都要打开一次文件,如果对数据进 ...

  9. HDU-2224-双调TSP

    双调tsp的模板题,暑假时看没看懂,现在很好理解方程. f[i][j]表示dis[1...i]+dis[1...j]的最短路径长度(max(i,j)之前的点全部经过且仅经过一次),f[i][j]=f[ ...

  10. react-quill 富文本编辑器

    适合react的一款轻量级富文本编辑器 1.http://blog.csdn.net/xiaoxiao23333/article/details/62055128 (推荐一款Markdown富文本编辑 ...