Submission #78068

# Submission time Handle Problem Language Result Execution time Memory
78068 2018-10-02T09:21:15 Z autumn_eel Circle selection (APIO18_circle_selection) C++14
100 / 100
904 ms 88600 KB
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF INT_MAX
using namespace std;
typedef long long ll;
 
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;
	}
};
vector<Circle*>circle;
struct node{
	double bbox[2][2];
	node*l,*r;
	Circle*circle;
};
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(int l,int r,double result[2][2]){
	init(result);
	for(int i=l;i<r;i++){
		mergeAABB(result,circle[i]->bbox,result);
	}
}
node bvh[800000];
int pointer=0;
 
inline void setleaf(int l,node*n){
	n->l=n->r=NULL;
	n->circle=circle[l];
}
void build(int l,int r,node*n,int depth){
	createAABB(l,r,n->bbox);
	if(r-l==1){
		setleaf(l,n);
		return;
	}
	int axis=-1;
	double minx=INF,maxx=-INF,miny=INF,maxy=-INF;
	for(int i=l;i<r;i++){
		minx=min(minx,circle[i]->bbox[0][0]);
		maxx=max(maxx,circle[i]->bbox[1][0]);
		miny=min(miny,circle[i]->bbox[0][1]);
		maxy=max(maxy,circle[i]->bbox[1][1]);
	}
	if(maxx-minx>maxy-miny)axis=0;
	else axis=1;
	
	n->l=&bvh[pointer++];
	n->r=&bvh[pointer++];
	int id=(r+l)/2;
	nth_element(circle.begin()+l,circle.begin()+id,circle.begin()+r,[&](auto a,auto b){
		if(axis==0)return a->x<b->x;
		return a->y<b->y;
	});
	build(l,id,n->l,depth+1);
	build(id,r,n->r,depth+1);
}
inline double sq(double x){
	return x*x;
}
inline bool intersect(Circle*c1,Circle*c2){
	return sq(c1->x-c2->x)+sq(c1->y-c2->y)<=sq(c1->r+c2->r);
}
inline bool intersect(double bbox1[2][2],double bbox2[2][2]){
	if(bbox1[1][0]<bbox2[0][0]||bbox2[1][0]<bbox1[0][0]||bbox1[1][1]<bbox2[0][1]||bbox2[1][1]<bbox1[0][1])return false;
	return true;
}
node*intersect(node*n,Circle*c){
	if(!intersect(c->bbox,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{
		if(intersect(n->circle,c)){
			ans[n->circle->id]=c->id;
			return NULL;
		}
		return n;
	}
}
 
//~ #define DEBUG
Circle c[400000];
 
int main(){
#ifdef DEBUG
	auto start=chrono::system_clock::now();
#endif
	int n;scanf("%d",&n);
	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(0,circle.size(),root,0);
#ifdef DEBUG
	auto end=chrono::system_clock::now();
	double tim=chrono::duration_cast<chrono::milliseconds>(end-start).count();
	printf("build time = %.3lf sec\n",tim/1000);
#endif
	memset(ans,-1,sizeof(ans));
	rep(i,n){
		if(ans[c[i].id]!=-1)continue;
		root=intersect(root,&c[i]);
		if(root==NULL)break;
	}
#ifndef DEBUG
	rep(i,n){
		if(i)printf(" ");
		printf("%d",ans[i]+1);
	}
	puts("");
#endif
#ifdef DEBUG
	auto end2=chrono::system_clock::now();
	double tim2=chrono::duration_cast<chrono::milliseconds>(end2-end).count();
	printf("intersection time = %.3lf sec\n",tim2/1000);
	double tim3=chrono::duration_cast<chrono::milliseconds>(end2-start).count();
	printf("total time = %.3lf sec\n",tim3/1000);
#endif
}

Compilation message

circle_selection.cpp: In function 'int main()':
circle_selection.cpp:113: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:115: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 3 ms 1916 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 3 ms 2048 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2196 KB Output is correct
9 Correct 3 ms 2196 KB Output is correct
10 Correct 3 ms 2196 KB Output is correct
11 Correct 3 ms 2196 KB Output is correct
12 Correct 4 ms 2196 KB Output is correct
13 Correct 4 ms 2244 KB Output is correct
14 Correct 3 ms 2244 KB Output is correct
15 Correct 4 ms 2244 KB Output is correct
16 Correct 5 ms 2360 KB Output is correct
17 Correct 4 ms 2392 KB Output is correct
18 Correct 4 ms 2432 KB Output is correct
19 Correct 9 ms 3484 KB Output is correct
20 Correct 10 ms 3632 KB Output is correct
21 Correct 11 ms 3800 KB Output is correct
22 Correct 11 ms 3844 KB Output is correct
23 Correct 12 ms 3976 KB Output is correct
24 Correct 11 ms 4236 KB Output is correct
25 Correct 12 ms 4248 KB Output is correct
26 Correct 14 ms 4504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 471 ms 65048 KB Output is correct
2 Correct 486 ms 67548 KB Output is correct
3 Correct 484 ms 67548 KB Output is correct
4 Correct 468 ms 67548 KB Output is correct
5 Correct 467 ms 67548 KB Output is correct
6 Correct 556 ms 67548 KB Output is correct
7 Correct 480 ms 67868 KB Output is correct
8 Correct 512 ms 69992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 69992 KB Output is correct
2 Correct 245 ms 69992 KB Output is correct
3 Correct 824 ms 69992 KB Output is correct
4 Correct 810 ms 70508 KB Output is correct
5 Correct 725 ms 70508 KB Output is correct
6 Correct 323 ms 70508 KB Output is correct
7 Correct 154 ms 70508 KB Output is correct
8 Correct 31 ms 70508 KB Output is correct
9 Correct 804 ms 70508 KB Output is correct
10 Correct 704 ms 70508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 70508 KB Output is correct
2 Correct 737 ms 70508 KB Output is correct
3 Correct 566 ms 70508 KB Output is correct
4 Correct 791 ms 71340 KB Output is correct
5 Correct 779 ms 75528 KB Output is correct
6 Correct 543 ms 75528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 3 ms 2048 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2196 KB Output is correct
9 Correct 3 ms 2196 KB Output is correct
10 Correct 3 ms 2196 KB Output is correct
11 Correct 3 ms 2196 KB Output is correct
12 Correct 4 ms 2196 KB Output is correct
13 Correct 4 ms 2244 KB Output is correct
14 Correct 3 ms 2244 KB Output is correct
15 Correct 4 ms 2244 KB Output is correct
16 Correct 5 ms 2360 KB Output is correct
17 Correct 4 ms 2392 KB Output is correct
18 Correct 4 ms 2432 KB Output is correct
19 Correct 9 ms 3484 KB Output is correct
20 Correct 10 ms 3632 KB Output is correct
21 Correct 11 ms 3800 KB Output is correct
22 Correct 11 ms 3844 KB Output is correct
23 Correct 12 ms 3976 KB Output is correct
24 Correct 11 ms 4236 KB Output is correct
25 Correct 12 ms 4248 KB Output is correct
26 Correct 14 ms 4504 KB Output is correct
27 Correct 20 ms 75528 KB Output is correct
28 Correct 16 ms 75528 KB Output is correct
29 Correct 16 ms 75528 KB Output is correct
30 Correct 19 ms 75528 KB Output is correct
31 Correct 19 ms 75528 KB Output is correct
32 Correct 20 ms 75528 KB Output is correct
33 Correct 172 ms 75528 KB Output is correct
34 Correct 160 ms 75528 KB Output is correct
35 Correct 159 ms 75528 KB Output is correct
36 Correct 223 ms 75528 KB Output is correct
37 Correct 222 ms 75528 KB Output is correct
38 Correct 225 ms 75528 KB Output is correct
39 Correct 197 ms 75528 KB Output is correct
40 Correct 188 ms 75528 KB Output is correct
41 Correct 183 ms 75528 KB Output is correct
42 Correct 167 ms 75528 KB Output is correct
43 Correct 187 ms 75528 KB Output is correct
44 Correct 191 ms 75528 KB Output is correct
45 Correct 185 ms 75528 KB Output is correct
46 Correct 186 ms 75528 KB Output is correct
47 Correct 193 ms 75528 KB Output is correct
48 Correct 206 ms 75528 KB Output is correct
49 Correct 185 ms 75528 KB Output is correct
50 Correct 201 ms 75528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1916 KB Output is correct
2 Correct 3 ms 2048 KB Output is correct
3 Correct 3 ms 2048 KB Output is correct
4 Correct 3 ms 2048 KB Output is correct
5 Correct 3 ms 2048 KB Output is correct
6 Correct 3 ms 2048 KB Output is correct
7 Correct 3 ms 2048 KB Output is correct
8 Correct 3 ms 2196 KB Output is correct
9 Correct 3 ms 2196 KB Output is correct
10 Correct 3 ms 2196 KB Output is correct
11 Correct 3 ms 2196 KB Output is correct
12 Correct 4 ms 2196 KB Output is correct
13 Correct 4 ms 2244 KB Output is correct
14 Correct 3 ms 2244 KB Output is correct
15 Correct 4 ms 2244 KB Output is correct
16 Correct 5 ms 2360 KB Output is correct
17 Correct 4 ms 2392 KB Output is correct
18 Correct 4 ms 2432 KB Output is correct
19 Correct 9 ms 3484 KB Output is correct
20 Correct 10 ms 3632 KB Output is correct
21 Correct 11 ms 3800 KB Output is correct
22 Correct 11 ms 3844 KB Output is correct
23 Correct 12 ms 3976 KB Output is correct
24 Correct 11 ms 4236 KB Output is correct
25 Correct 12 ms 4248 KB Output is correct
26 Correct 14 ms 4504 KB Output is correct
27 Correct 471 ms 65048 KB Output is correct
28 Correct 486 ms 67548 KB Output is correct
29 Correct 484 ms 67548 KB Output is correct
30 Correct 468 ms 67548 KB Output is correct
31 Correct 467 ms 67548 KB Output is correct
32 Correct 556 ms 67548 KB Output is correct
33 Correct 480 ms 67868 KB Output is correct
34 Correct 512 ms 69992 KB Output is correct
35 Correct 4 ms 69992 KB Output is correct
36 Correct 245 ms 69992 KB Output is correct
37 Correct 824 ms 69992 KB Output is correct
38 Correct 810 ms 70508 KB Output is correct
39 Correct 725 ms 70508 KB Output is correct
40 Correct 323 ms 70508 KB Output is correct
41 Correct 154 ms 70508 KB Output is correct
42 Correct 31 ms 70508 KB Output is correct
43 Correct 804 ms 70508 KB Output is correct
44 Correct 704 ms 70508 KB Output is correct
45 Correct 740 ms 70508 KB Output is correct
46 Correct 737 ms 70508 KB Output is correct
47 Correct 566 ms 70508 KB Output is correct
48 Correct 791 ms 71340 KB Output is correct
49 Correct 779 ms 75528 KB Output is correct
50 Correct 543 ms 75528 KB Output is correct
51 Correct 20 ms 75528 KB Output is correct
52 Correct 16 ms 75528 KB Output is correct
53 Correct 16 ms 75528 KB Output is correct
54 Correct 19 ms 75528 KB Output is correct
55 Correct 19 ms 75528 KB Output is correct
56 Correct 20 ms 75528 KB Output is correct
57 Correct 172 ms 75528 KB Output is correct
58 Correct 160 ms 75528 KB Output is correct
59 Correct 159 ms 75528 KB Output is correct
60 Correct 223 ms 75528 KB Output is correct
61 Correct 222 ms 75528 KB Output is correct
62 Correct 225 ms 75528 KB Output is correct
63 Correct 197 ms 75528 KB Output is correct
64 Correct 188 ms 75528 KB Output is correct
65 Correct 183 ms 75528 KB Output is correct
66 Correct 167 ms 75528 KB Output is correct
67 Correct 187 ms 75528 KB Output is correct
68 Correct 191 ms 75528 KB Output is correct
69 Correct 185 ms 75528 KB Output is correct
70 Correct 186 ms 75528 KB Output is correct
71 Correct 193 ms 75528 KB Output is correct
72 Correct 206 ms 75528 KB Output is correct
73 Correct 185 ms 75528 KB Output is correct
74 Correct 201 ms 75528 KB Output is correct
75 Correct 587 ms 75832 KB Output is correct
76 Correct 552 ms 76100 KB Output is correct
77 Correct 565 ms 76260 KB Output is correct
78 Correct 580 ms 76260 KB Output is correct
79 Correct 577 ms 76260 KB Output is correct
80 Correct 558 ms 76260 KB Output is correct
81 Correct 820 ms 76260 KB Output is correct
82 Correct 854 ms 76260 KB Output is correct
83 Correct 904 ms 76260 KB Output is correct
84 Correct 836 ms 76260 KB Output is correct
85 Correct 816 ms 76260 KB Output is correct
86 Correct 833 ms 76260 KB Output is correct
87 Correct 826 ms 76260 KB Output is correct
88 Correct 712 ms 76260 KB Output is correct
89 Correct 688 ms 76260 KB Output is correct
90 Correct 719 ms 78564 KB Output is correct
91 Correct 670 ms 81048 KB Output is correct
92 Correct 698 ms 81172 KB Output is correct
93 Correct 691 ms 81356 KB Output is correct
94 Correct 787 ms 81368 KB Output is correct
95 Correct 773 ms 81368 KB Output is correct
96 Correct 737 ms 81396 KB Output is correct
97 Correct 743 ms 81592 KB Output is correct
98 Correct 611 ms 81592 KB Output is correct
99 Correct 732 ms 81592 KB Output is correct
100 Correct 683 ms 81592 KB Output is correct
101 Correct 667 ms 81592 KB Output is correct
102 Correct 700 ms 85100 KB Output is correct
103 Correct 741 ms 88484 KB Output is correct
104 Correct 689 ms 88540 KB Output is correct
105 Correct 580 ms 88540 KB Output is correct
106 Correct 677 ms 88540 KB Output is correct
107 Correct 651 ms 88540 KB Output is correct
108 Correct 687 ms 88600 KB Output is correct
109 Correct 667 ms 88600 KB Output is correct
110 Correct 647 ms 88600 KB Output is correct
111 Correct 650 ms 88600 KB Output is correct
112 Correct 637 ms 88600 KB Output is correct
113 Correct 666 ms 88600 KB Output is correct
114 Correct 653 ms 88600 KB Output is correct
115 Correct 667 ms 88600 KB Output is correct
116 Correct 678 ms 88600 KB Output is correct