Submission #78069

# Submission time Handle Problem Language Result Execution time Memory
78069 2018-10-02T09:23:17 Z autumn_eel Circle selection (APIO18_circle_selection) C++14
100 / 100
734 ms 39840 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{
	int x,y,r;
	int id;
	int bbox[2][2];
	Circle(){}
	Circle(int x,int y,int 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{
	int bbox[2][2];
	node*l,*r;
	Circle*circle;
};
void init(int bbox[2][2]){
	bbox[0][0]=bbox[0][1]=INF;
	bbox[1][0]=bbox[1][1]=-INF;
}
void mergeAABB(int bbox[2][2],int bbox2[2][2],int 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,int 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;
	int 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 ll sq(int x){
	return x*(ll)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(int bbox1[2][2],int 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 1912 KB Output is correct
2 Correct 3 ms 1984 KB Output is correct
3 Correct 3 ms 1984 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 4 ms 2000 KB Output is correct
6 Correct 4 ms 2056 KB Output is correct
7 Correct 3 ms 2184 KB Output is correct
8 Correct 4 ms 2184 KB Output is correct
9 Correct 3 ms 2184 KB Output is correct
10 Correct 4 ms 2184 KB Output is correct
11 Correct 3 ms 2184 KB Output is correct
12 Correct 3 ms 2184 KB Output is correct
13 Correct 4 ms 2184 KB Output is correct
14 Correct 4 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 5 ms 2196 KB Output is correct
17 Correct 4 ms 2248 KB Output is correct
18 Correct 4 ms 2248 KB Output is correct
19 Correct 9 ms 2756 KB Output is correct
20 Correct 13 ms 2812 KB Output is correct
21 Correct 10 ms 2812 KB Output is correct
22 Correct 10 ms 2812 KB Output is correct
23 Correct 13 ms 2916 KB Output is correct
24 Correct 10 ms 2916 KB Output is correct
25 Correct 10 ms 2916 KB Output is correct
26 Correct 10 ms 2916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 404 ms 39552 KB Output is correct
2 Correct 432 ms 39552 KB Output is correct
3 Correct 377 ms 39552 KB Output is correct
4 Correct 388 ms 39720 KB Output is correct
5 Correct 378 ms 39720 KB Output is correct
6 Correct 496 ms 39720 KB Output is correct
7 Correct 401 ms 39720 KB Output is correct
8 Correct 442 ms 39720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 39720 KB Output is correct
2 Correct 197 ms 39720 KB Output is correct
3 Correct 704 ms 39720 KB Output is correct
4 Correct 696 ms 39720 KB Output is correct
5 Correct 666 ms 39720 KB Output is correct
6 Correct 274 ms 39720 KB Output is correct
7 Correct 141 ms 39720 KB Output is correct
8 Correct 26 ms 39720 KB Output is correct
9 Correct 697 ms 39756 KB Output is correct
10 Correct 609 ms 39756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 639 ms 39756 KB Output is correct
2 Correct 657 ms 39756 KB Output is correct
3 Correct 493 ms 39756 KB Output is correct
4 Correct 707 ms 39756 KB Output is correct
5 Correct 644 ms 39756 KB Output is correct
6 Correct 459 ms 39756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1984 KB Output is correct
3 Correct 3 ms 1984 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 4 ms 2000 KB Output is correct
6 Correct 4 ms 2056 KB Output is correct
7 Correct 3 ms 2184 KB Output is correct
8 Correct 4 ms 2184 KB Output is correct
9 Correct 3 ms 2184 KB Output is correct
10 Correct 4 ms 2184 KB Output is correct
11 Correct 3 ms 2184 KB Output is correct
12 Correct 3 ms 2184 KB Output is correct
13 Correct 4 ms 2184 KB Output is correct
14 Correct 4 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 5 ms 2196 KB Output is correct
17 Correct 4 ms 2248 KB Output is correct
18 Correct 4 ms 2248 KB Output is correct
19 Correct 9 ms 2756 KB Output is correct
20 Correct 13 ms 2812 KB Output is correct
21 Correct 10 ms 2812 KB Output is correct
22 Correct 10 ms 2812 KB Output is correct
23 Correct 13 ms 2916 KB Output is correct
24 Correct 10 ms 2916 KB Output is correct
25 Correct 10 ms 2916 KB Output is correct
26 Correct 10 ms 2916 KB Output is correct
27 Correct 15 ms 39756 KB Output is correct
28 Correct 18 ms 39756 KB Output is correct
29 Correct 15 ms 39756 KB Output is correct
30 Correct 17 ms 39756 KB Output is correct
31 Correct 17 ms 39756 KB Output is correct
32 Correct 18 ms 39756 KB Output is correct
33 Correct 135 ms 39756 KB Output is correct
34 Correct 139 ms 39756 KB Output is correct
35 Correct 136 ms 39756 KB Output is correct
36 Correct 181 ms 39756 KB Output is correct
37 Correct 196 ms 39756 KB Output is correct
38 Correct 196 ms 39756 KB Output is correct
39 Correct 163 ms 39756 KB Output is correct
40 Correct 161 ms 39756 KB Output is correct
41 Correct 149 ms 39756 KB Output is correct
42 Correct 142 ms 39756 KB Output is correct
43 Correct 167 ms 39756 KB Output is correct
44 Correct 166 ms 39756 KB Output is correct
45 Correct 157 ms 39756 KB Output is correct
46 Correct 165 ms 39756 KB Output is correct
47 Correct 170 ms 39756 KB Output is correct
48 Correct 169 ms 39756 KB Output is correct
49 Correct 167 ms 39756 KB Output is correct
50 Correct 168 ms 39756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1984 KB Output is correct
3 Correct 3 ms 1984 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 4 ms 2000 KB Output is correct
6 Correct 4 ms 2056 KB Output is correct
7 Correct 3 ms 2184 KB Output is correct
8 Correct 4 ms 2184 KB Output is correct
9 Correct 3 ms 2184 KB Output is correct
10 Correct 4 ms 2184 KB Output is correct
11 Correct 3 ms 2184 KB Output is correct
12 Correct 3 ms 2184 KB Output is correct
13 Correct 4 ms 2184 KB Output is correct
14 Correct 4 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 5 ms 2196 KB Output is correct
17 Correct 4 ms 2248 KB Output is correct
18 Correct 4 ms 2248 KB Output is correct
19 Correct 9 ms 2756 KB Output is correct
20 Correct 13 ms 2812 KB Output is correct
21 Correct 10 ms 2812 KB Output is correct
22 Correct 10 ms 2812 KB Output is correct
23 Correct 13 ms 2916 KB Output is correct
24 Correct 10 ms 2916 KB Output is correct
25 Correct 10 ms 2916 KB Output is correct
26 Correct 10 ms 2916 KB Output is correct
27 Correct 404 ms 39552 KB Output is correct
28 Correct 432 ms 39552 KB Output is correct
29 Correct 377 ms 39552 KB Output is correct
30 Correct 388 ms 39720 KB Output is correct
31 Correct 378 ms 39720 KB Output is correct
32 Correct 496 ms 39720 KB Output is correct
33 Correct 401 ms 39720 KB Output is correct
34 Correct 442 ms 39720 KB Output is correct
35 Correct 4 ms 39720 KB Output is correct
36 Correct 197 ms 39720 KB Output is correct
37 Correct 704 ms 39720 KB Output is correct
38 Correct 696 ms 39720 KB Output is correct
39 Correct 666 ms 39720 KB Output is correct
40 Correct 274 ms 39720 KB Output is correct
41 Correct 141 ms 39720 KB Output is correct
42 Correct 26 ms 39720 KB Output is correct
43 Correct 697 ms 39756 KB Output is correct
44 Correct 609 ms 39756 KB Output is correct
45 Correct 639 ms 39756 KB Output is correct
46 Correct 657 ms 39756 KB Output is correct
47 Correct 493 ms 39756 KB Output is correct
48 Correct 707 ms 39756 KB Output is correct
49 Correct 644 ms 39756 KB Output is correct
50 Correct 459 ms 39756 KB Output is correct
51 Correct 15 ms 39756 KB Output is correct
52 Correct 18 ms 39756 KB Output is correct
53 Correct 15 ms 39756 KB Output is correct
54 Correct 17 ms 39756 KB Output is correct
55 Correct 17 ms 39756 KB Output is correct
56 Correct 18 ms 39756 KB Output is correct
57 Correct 135 ms 39756 KB Output is correct
58 Correct 139 ms 39756 KB Output is correct
59 Correct 136 ms 39756 KB Output is correct
60 Correct 181 ms 39756 KB Output is correct
61 Correct 196 ms 39756 KB Output is correct
62 Correct 196 ms 39756 KB Output is correct
63 Correct 163 ms 39756 KB Output is correct
64 Correct 161 ms 39756 KB Output is correct
65 Correct 149 ms 39756 KB Output is correct
66 Correct 142 ms 39756 KB Output is correct
67 Correct 167 ms 39756 KB Output is correct
68 Correct 166 ms 39756 KB Output is correct
69 Correct 157 ms 39756 KB Output is correct
70 Correct 165 ms 39756 KB Output is correct
71 Correct 170 ms 39756 KB Output is correct
72 Correct 169 ms 39756 KB Output is correct
73 Correct 167 ms 39756 KB Output is correct
74 Correct 168 ms 39756 KB Output is correct
75 Correct 516 ms 39756 KB Output is correct
76 Correct 470 ms 39756 KB Output is correct
77 Correct 447 ms 39756 KB Output is correct
78 Correct 434 ms 39756 KB Output is correct
79 Correct 516 ms 39756 KB Output is correct
80 Correct 471 ms 39756 KB Output is correct
81 Correct 734 ms 39756 KB Output is correct
82 Correct 709 ms 39756 KB Output is correct
83 Correct 695 ms 39840 KB Output is correct
84 Correct 719 ms 39840 KB Output is correct
85 Correct 682 ms 39840 KB Output is correct
86 Correct 709 ms 39840 KB Output is correct
87 Correct 697 ms 39840 KB Output is correct
88 Correct 574 ms 39840 KB Output is correct
89 Correct 612 ms 39840 KB Output is correct
90 Correct 602 ms 39840 KB Output is correct
91 Correct 567 ms 39840 KB Output is correct
92 Correct 577 ms 39840 KB Output is correct
93 Correct 612 ms 39840 KB Output is correct
94 Correct 637 ms 39840 KB Output is correct
95 Correct 619 ms 39840 KB Output is correct
96 Correct 626 ms 39840 KB Output is correct
97 Correct 631 ms 39840 KB Output is correct
98 Correct 507 ms 39840 KB Output is correct
99 Correct 670 ms 39840 KB Output is correct
100 Correct 609 ms 39840 KB Output is correct
101 Correct 594 ms 39840 KB Output is correct
102 Correct 651 ms 39840 KB Output is correct
103 Correct 640 ms 39840 KB Output is correct
104 Correct 606 ms 39840 KB Output is correct
105 Correct 486 ms 39840 KB Output is correct
106 Correct 555 ms 39840 KB Output is correct
107 Correct 581 ms 39840 KB Output is correct
108 Correct 552 ms 39840 KB Output is correct
109 Correct 545 ms 39840 KB Output is correct
110 Correct 559 ms 39840 KB Output is correct
111 Correct 561 ms 39840 KB Output is correct
112 Correct 577 ms 39840 KB Output is correct
113 Correct 570 ms 39840 KB Output is correct
114 Correct 574 ms 39840 KB Output is correct
115 Correct 547 ms 39840 KB Output is correct
116 Correct 552 ms 39840 KB Output is correct