ACM-ICPC 2017南寧エリアネットワーク予選F Overlapping Rectangles【線分樹のスキャン線の使い方】
3566 ワード
Although the problem look s simple at the first glance、it might take a while to figure out how to do it corectly.Note that the shop of the union can be very coplicated、and the intersected ares can
ノート:
(1)The coordinans of these rectangles are given in integers.So you dot have to worly about the float point round-off errors.However,these integers can be as 1,000,000.
(2)To make the probleam easire,you dot have to worly about the sum of the aras exceeding the long integer precision.That is,you can asume that the total ara does not relt in inteover flow.
Input Format
Several sets of rectangles configrations.The input are a list of integers.Within each set,the first integer represents the number of rectangles,n,which can as large 1000.After therecture.each line contains four integers , which means that the bottom left coner of the rectanle is located at (a,b)、and the top right coner of the rectanle is located at(c,d).Note that integers a, b, c, d can be as large as 1,000,000.
The configrations of rectangles occur repetively in the input as the pattern described above.An integer n=0 (ゼロ)signifies the end of input.
Output Format
For each set of the rectangles configrations apeared in the input、cacacallate the total ara of the union of the rectangles.Again、these rectanles might overlap each other、and the the the intersecting are the the the the of the contronglectinglecting of the controns caglectinglectinglecting of the
サンプル入力
2
0 0 2 2
1 1 3 3
3
0 0 1 1
2 2 3 3
4 4 5 5
0
サンプル出力7
3
*
問題:線分樹の走査線の使い方、テンプレートの問題、オンラインコードのテンプレートを使用します.ACのC++プログラム:
#include
#include
#include
using namespace std;
const int MAXN=2222;
#define lson i*2,l,m
#define rson i*2+1,m+1,r
#define root 1,1,k-1
double X[MAXN];
struct node
{
double l,r,h;
int d;
node(){}
node(double a,double b,double c,int d): l(a),r(b),h(c),d(d){}
bool operator =l)
{
int m=(r+l)/2;
if(d[m]==key)
return m;
else if(d[m]>key)
r=m-1;
else
l=m+1;
}
return -1;
}
int main()
{
int q;
int kase=0;
while(~scanf("%d",&q)&&q)
{
int n=0,m=0;
for(int i=1;i<=q;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
X[++n]=x1;
nodes[++m]=node(x1,x2,y1,1);
X[++n]=x2;
nodes[++m]=node(x1,x2,y2,-1);
}
sort(X+1,X+n+1);
sort(nodes+1,nodes+m+1);
int k=1;// k x , k-1
for(int i=2;i<=n;i++)
if(X[i]!=X[i-1]) X[++k]=X[i];
build(1,1,k-1);// build WA
double ret=0.0;//
for(int i=1;i