Submission #77714

# Submission time Handle Problem Language Result Execution time Memory
77714 2018-09-30T03:42:06 Z autumn_eel Circle selection (APIO18_circle_selection) C++14
87 / 100
3000 ms 367988 KB
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 1e10
using namespace std;

int ans[400000];
struct Circle{
	double x,y,r;
	int id;
	double bbox[2][2];
	Circle(){}
	Circle(double x,double y,double r,int id):x(x),y(y),r(r),id(id){
		bbox[0][0]=x-r;bbox[0][1]=y-r;
		bbox[1][0]=x+r;bbox[1][1]=y+r;
	}
};
struct node{
	double bbox[2][2];
	node*l,*r;
	vector<Circle*>circle;
};
double area(double bbox[2][2]){
	return (bbox[1][0]-bbox[0][0])*(bbox[1][1]-bbox[0][1]);
}
void init(double bbox[2][2]){
	bbox[0][0]=bbox[0][1]=INF;
	bbox[1][0]=bbox[1][1]=-INF;
}
void mergeAABB(double bbox[2][2],double bbox2[2][2],double result[2][2]){
	rep(i,2){
		result[0][i]=min(bbox[0][i],bbox2[0][i]);
		result[1][i]=max(bbox[1][i],bbox2[1][i]);
	}
}
void createAABB(vector<Circle*>&circle,double result[2][2]){
	init(result);
	for(auto&c:circle){
		mergeAABB(result,c->bbox,result);
	}
}
node bvh[400000];
int pointer=0;

#define t_tri 1.
#define t_aabb 1.

void setleaf(vector<Circle*>circle,node*n){
	n->l=n->r=NULL;
	n->circle=circle;
}
void build(vector<Circle*>&circle,node*n){
	createAABB(circle,n->bbox);
	double Min=t_tri*circle.size();
	int axis=-1,id=-1;
	double s=area(n->bbox);
	rep(i,2){
		sort(circle.begin(),circle.end(),[&](auto a,auto b){
			if(i==0)return a->x<b->x;
			return a->y<b->y;
		});
		double box[2][2];init(box);
		vector<double>s1(circle.size());
		rep(j,circle.size()){
			mergeAABB(box,circle[j]->bbox,box);
			s1[j]=area(box);
		}
		init(box);
		vector<double>s2(circle.size());
		for(int j=circle.size()-1;j>=0;j--){
			mergeAABB(box,circle[j]->bbox,box);
			s2[j]=area(box);
		}
		for(int j=0;j+1<circle.size();j++){
			double cost=2*t_aabb+(s1[j]/s)*(j+1)*t_tri+(s2[j+1]/s)*(circle.size()-j-1)*t_tri;
			if(Min>cost){
				Min=cost;
				axis=i;id=j;
			}
		}
	}
	if(axis==-1){
		setleaf(circle,n);
	}
	else{
		sort(circle.begin(),circle.end(),[&](auto a,auto b){
			if(axis==0)return a->x<b->x;
			return a->y<b->y;
		});
		n->l=&bvh[pointer++];
		n->r=&bvh[pointer++];
		vector<Circle*>left(circle.begin(),circle.begin()+id+1);
		vector<Circle*>right(circle.begin()+id+1,circle.end());
		build(left,n->l);
		build(right,n->r);
	}
}
void dfs_bvh(node*n,int depth){
	if(depth==3){
		cout<<"(minx,miny),(maxx,maxy) = "<<"("<<n->bbox[0][0]<<","<<n->bbox[0][1]<<")"<<","<<"("<<n->bbox[1][0]<<","<<n->bbox[1][1]<<")"<<endl;
	}
	if(n->l)dfs_bvh(n->l,depth+1);
	if(n->r)dfs_bvh(n->r,depth+1);
}
bool intersect(double x,double y,double minx,double miny,double maxx,double maxy){
	return minx<=x&&x<=maxx&&miny<=y&&y<=maxy;
}
bool intersect(Circle*c1,Circle*c2){
	return pow(c1->x-c2->x,2)+pow(c1->y-c2->y,2)<=pow(c1->r+c2->r,2);
}
bool intersect(Circle*c,double bbox[2][2]){
	if(intersect(c->x,c->y,bbox[0][0]-c->r,bbox[0][1],bbox[1][0]+c->r,bbox[1][1]))return true;
	if(intersect(c->x,c->y,bbox[0][0],bbox[0][1]-c->r,bbox[1][0],bbox[1][1]+c->r))return true;
	rep(i,2)rep(j,2){
		if(pow(bbox[i][0]-c->x,2)+pow(bbox[j][1]-c->y,2)<=pow(c->r,2))return true;
	}
	return false;
}
node*intersect(node*n,Circle*c){
	if(!intersect(c,n->bbox))return n;
	if(n->l){
		auto l=intersect(n->l,c);
		auto r=intersect(n->r,c);
		if(l==NULL&&r==NULL)return NULL;
		if(l==NULL)return r;
		if(r==NULL)return l;
		n->l=l;n->r=r;
		init(n->bbox);
		mergeAABB(l->bbox,r->bbox,n->bbox);
		return n;
	}
	else{
		auto&circle=n->circle;
		vector<Circle*>res;
		rep(i,circle.size()){
			if(intersect(circle[i],c)){
				ans[circle[i]->id]=c->id;
			}
			else{
				res.push_back(circle[i]);
			}
		}
		if(res.empty())return NULL;
		circle=res;
		createAABB(circle,n->bbox);
		return n;
	}
}

Circle c[400000];
int main(){
	int n;scanf("%d",&n);
	vector<Circle*>circle;
	rep(i,n){
		int x,y,r;scanf("%d%d%d",&x,&y,&r);
		c[i]=Circle(x,y,r,i);
	}
	sort(c,c+n,[](auto a,auto b){
		if(a.r==b.r)return a.id<b.id;
		return a.r>b.r;
	});
	rep(i,n){
		circle.push_back(&c[i]);
	}
	node*root=&bvh[pointer++];
	build(circle,root);
	//~ dfs_bvh(root,0);
	memset(ans,-1,sizeof(ans));
	rep(i,n){
		if(ans[c[i].id]!=-1)continue;
		root=intersect(root,&c[i]);
		if(root==NULL)break;
	}
	rep(i,n){
		if(i)printf(" ");
		printf("%d",ans[i]+1);
	}
	puts("");
}

Compilation message

circle_selection.cpp: In function 'void build(std::vector<Circle*>&, node*)':
circle_selection.cpp:2:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,n)for(int i=0;i<(n);i++)
                              ^
circle_selection.cpp:63:3: note: in expansion of macro 'rep'
   rep(j,circle.size()){
   ^~~
circle_selection.cpp:73:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j+1<circle.size();j++){
               ~~~^~~~~~~~~~~~~~
circle_selection.cpp: In function 'node* intersect(node*, Circle*)':
circle_selection.cpp:2:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i,n)for(int i=0;i<(n);i++)
                              ^
circle_selection.cpp:134:3: note: in expansion of macro 'rep'
   rep(i,circle.size()){
   ^~~
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:151:13: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n;scanf("%d",&n);
        ~~~~~^~~~~~~~~
circle_selection.cpp:154:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x,y,r;scanf("%d%d%d",&x,&y,&r);
             ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 30200 KB Output is correct
2 Correct 33 ms 30208 KB Output is correct
3 Correct 28 ms 30288 KB Output is correct
4 Correct 27 ms 30468 KB Output is correct
5 Correct 29 ms 30696 KB Output is correct
6 Correct 28 ms 30696 KB Output is correct
7 Correct 28 ms 30696 KB Output is correct
8 Correct 28 ms 30696 KB Output is correct
9 Correct 27 ms 30696 KB Output is correct
10 Correct 28 ms 30696 KB Output is correct
11 Correct 28 ms 30696 KB Output is correct
12 Correct 31 ms 30696 KB Output is correct
13 Correct 27 ms 30696 KB Output is correct
14 Correct 27 ms 30696 KB Output is correct
15 Correct 27 ms 30696 KB Output is correct
16 Correct 30 ms 30696 KB Output is correct
17 Correct 30 ms 30716 KB Output is correct
18 Correct 30 ms 30768 KB Output is correct
19 Correct 41 ms 31312 KB Output is correct
20 Correct 41 ms 31480 KB Output is correct
21 Correct 42 ms 31648 KB Output is correct
22 Correct 46 ms 31804 KB Output is correct
23 Correct 46 ms 31936 KB Output is correct
24 Correct 48 ms 32196 KB Output is correct
25 Correct 46 ms 32224 KB Output is correct
26 Correct 46 ms 32404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1738 ms 69808 KB Output is correct
2 Correct 1641 ms 76428 KB Output is correct
3 Correct 1664 ms 82816 KB Output is correct
4 Correct 1744 ms 89752 KB Output is correct
5 Correct 1628 ms 94276 KB Output is correct
6 Correct 1744 ms 99768 KB Output is correct
7 Correct 1576 ms 104056 KB Output is correct
8 Correct 1614 ms 108852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 108852 KB Output is correct
2 Correct 690 ms 108852 KB Output is correct
3 Correct 2754 ms 122124 KB Output is correct
4 Correct 2729 ms 130108 KB Output is correct
5 Correct 2568 ms 136924 KB Output is correct
6 Correct 1125 ms 136924 KB Output is correct
7 Correct 493 ms 136924 KB Output is correct
8 Correct 100 ms 136924 KB Output is correct
9 Correct 2901 ms 153332 KB Output is correct
10 Correct 2483 ms 160952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2703 ms 169740 KB Output is correct
2 Correct 2696 ms 176828 KB Output is correct
3 Correct 2308 ms 182272 KB Output is correct
4 Correct 2693 ms 192716 KB Output is correct
5 Correct 2736 ms 200364 KB Output is correct
6 Correct 2231 ms 205500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 30200 KB Output is correct
2 Correct 33 ms 30208 KB Output is correct
3 Correct 28 ms 30288 KB Output is correct
4 Correct 27 ms 30468 KB Output is correct
5 Correct 29 ms 30696 KB Output is correct
6 Correct 28 ms 30696 KB Output is correct
7 Correct 28 ms 30696 KB Output is correct
8 Correct 28 ms 30696 KB Output is correct
9 Correct 27 ms 30696 KB Output is correct
10 Correct 28 ms 30696 KB Output is correct
11 Correct 28 ms 30696 KB Output is correct
12 Correct 31 ms 30696 KB Output is correct
13 Correct 27 ms 30696 KB Output is correct
14 Correct 27 ms 30696 KB Output is correct
15 Correct 27 ms 30696 KB Output is correct
16 Correct 30 ms 30696 KB Output is correct
17 Correct 30 ms 30716 KB Output is correct
18 Correct 30 ms 30768 KB Output is correct
19 Correct 41 ms 31312 KB Output is correct
20 Correct 41 ms 31480 KB Output is correct
21 Correct 42 ms 31648 KB Output is correct
22 Correct 46 ms 31804 KB Output is correct
23 Correct 46 ms 31936 KB Output is correct
24 Correct 48 ms 32196 KB Output is correct
25 Correct 46 ms 32224 KB Output is correct
26 Correct 46 ms 32404 KB Output is correct
27 Correct 61 ms 205500 KB Output is correct
28 Correct 60 ms 205500 KB Output is correct
29 Correct 61 ms 205500 KB Output is correct
30 Correct 70 ms 205500 KB Output is correct
31 Correct 70 ms 205500 KB Output is correct
32 Correct 74 ms 205500 KB Output is correct
33 Correct 554 ms 205500 KB Output is correct
34 Correct 524 ms 205500 KB Output is correct
35 Correct 566 ms 205500 KB Output is correct
36 Correct 715 ms 205500 KB Output is correct
37 Correct 699 ms 205500 KB Output is correct
38 Correct 706 ms 205500 KB Output is correct
39 Correct 949 ms 211504 KB Output is correct
40 Correct 852 ms 212728 KB Output is correct
41 Correct 756 ms 212728 KB Output is correct
42 Correct 505 ms 212728 KB Output is correct
43 Correct 466 ms 212728 KB Output is correct
44 Correct 462 ms 213716 KB Output is correct
45 Correct 473 ms 216312 KB Output is correct
46 Correct 456 ms 219084 KB Output is correct
47 Correct 439 ms 221552 KB Output is correct
48 Correct 468 ms 224292 KB Output is correct
49 Correct 461 ms 226772 KB Output is correct
50 Correct 441 ms 229428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 30200 KB Output is correct
2 Correct 33 ms 30208 KB Output is correct
3 Correct 28 ms 30288 KB Output is correct
4 Correct 27 ms 30468 KB Output is correct
5 Correct 29 ms 30696 KB Output is correct
6 Correct 28 ms 30696 KB Output is correct
7 Correct 28 ms 30696 KB Output is correct
8 Correct 28 ms 30696 KB Output is correct
9 Correct 27 ms 30696 KB Output is correct
10 Correct 28 ms 30696 KB Output is correct
11 Correct 28 ms 30696 KB Output is correct
12 Correct 31 ms 30696 KB Output is correct
13 Correct 27 ms 30696 KB Output is correct
14 Correct 27 ms 30696 KB Output is correct
15 Correct 27 ms 30696 KB Output is correct
16 Correct 30 ms 30696 KB Output is correct
17 Correct 30 ms 30716 KB Output is correct
18 Correct 30 ms 30768 KB Output is correct
19 Correct 41 ms 31312 KB Output is correct
20 Correct 41 ms 31480 KB Output is correct
21 Correct 42 ms 31648 KB Output is correct
22 Correct 46 ms 31804 KB Output is correct
23 Correct 46 ms 31936 KB Output is correct
24 Correct 48 ms 32196 KB Output is correct
25 Correct 46 ms 32224 KB Output is correct
26 Correct 46 ms 32404 KB Output is correct
27 Correct 1738 ms 69808 KB Output is correct
28 Correct 1641 ms 76428 KB Output is correct
29 Correct 1664 ms 82816 KB Output is correct
30 Correct 1744 ms 89752 KB Output is correct
31 Correct 1628 ms 94276 KB Output is correct
32 Correct 1744 ms 99768 KB Output is correct
33 Correct 1576 ms 104056 KB Output is correct
34 Correct 1614 ms 108852 KB Output is correct
35 Correct 27 ms 108852 KB Output is correct
36 Correct 690 ms 108852 KB Output is correct
37 Correct 2754 ms 122124 KB Output is correct
38 Correct 2729 ms 130108 KB Output is correct
39 Correct 2568 ms 136924 KB Output is correct
40 Correct 1125 ms 136924 KB Output is correct
41 Correct 493 ms 136924 KB Output is correct
42 Correct 100 ms 136924 KB Output is correct
43 Correct 2901 ms 153332 KB Output is correct
44 Correct 2483 ms 160952 KB Output is correct
45 Correct 2703 ms 169740 KB Output is correct
46 Correct 2696 ms 176828 KB Output is correct
47 Correct 2308 ms 182272 KB Output is correct
48 Correct 2693 ms 192716 KB Output is correct
49 Correct 2736 ms 200364 KB Output is correct
50 Correct 2231 ms 205500 KB Output is correct
51 Correct 61 ms 205500 KB Output is correct
52 Correct 60 ms 205500 KB Output is correct
53 Correct 61 ms 205500 KB Output is correct
54 Correct 70 ms 205500 KB Output is correct
55 Correct 70 ms 205500 KB Output is correct
56 Correct 74 ms 205500 KB Output is correct
57 Correct 554 ms 205500 KB Output is correct
58 Correct 524 ms 205500 KB Output is correct
59 Correct 566 ms 205500 KB Output is correct
60 Correct 715 ms 205500 KB Output is correct
61 Correct 699 ms 205500 KB Output is correct
62 Correct 706 ms 205500 KB Output is correct
63 Correct 949 ms 211504 KB Output is correct
64 Correct 852 ms 212728 KB Output is correct
65 Correct 756 ms 212728 KB Output is correct
66 Correct 505 ms 212728 KB Output is correct
67 Correct 466 ms 212728 KB Output is correct
68 Correct 462 ms 213716 KB Output is correct
69 Correct 473 ms 216312 KB Output is correct
70 Correct 456 ms 219084 KB Output is correct
71 Correct 439 ms 221552 KB Output is correct
72 Correct 468 ms 224292 KB Output is correct
73 Correct 461 ms 226772 KB Output is correct
74 Correct 441 ms 229428 KB Output is correct
75 Correct 2278 ms 256728 KB Output is correct
76 Correct 2289 ms 265604 KB Output is correct
77 Correct 2389 ms 274456 KB Output is correct
78 Correct 2350 ms 283012 KB Output is correct
79 Correct 2384 ms 290580 KB Output is correct
80 Correct 2267 ms 299188 KB Output is correct
81 Correct 2901 ms 309648 KB Output is correct
82 Correct 2778 ms 317540 KB Output is correct
83 Correct 2794 ms 325472 KB Output is correct
84 Correct 2836 ms 333968 KB Output is correct
85 Correct 2798 ms 341596 KB Output is correct
86 Correct 2841 ms 347688 KB Output is correct
87 Correct 2909 ms 355700 KB Output is correct
88 Execution timed out 3055 ms 367988 KB Time limit exceeded
89 Halted 0 ms 0 KB -