답안 #78114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
78114 2018-10-02T13:01:26 Z autumn_eel 원 고르기 (APIO18_circle_selection) C++14
100 / 100
759 ms 39852 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){
	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);
	build(id,r,n->r);
}
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);
#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);
             ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1980 KB Output is correct
3 Correct 3 ms 1980 KB Output is correct
4 Correct 3 ms 1980 KB Output is correct
5 Correct 3 ms 1992 KB Output is correct
6 Correct 3 ms 2084 KB Output is correct
7 Correct 3 ms 2132 KB Output is correct
8 Correct 3 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 3 ms 2184 KB Output is correct
14 Correct 3 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 4 ms 2244 KB Output is correct
17 Correct 4 ms 2244 KB Output is correct
18 Correct 4 ms 2244 KB Output is correct
19 Correct 9 ms 2768 KB Output is correct
20 Correct 9 ms 2768 KB Output is correct
21 Correct 9 ms 2836 KB Output is correct
22 Correct 11 ms 2836 KB Output is correct
23 Correct 10 ms 2956 KB Output is correct
24 Correct 11 ms 3020 KB Output is correct
25 Correct 10 ms 3020 KB Output is correct
26 Correct 10 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 419 ms 39792 KB Output is correct
2 Correct 413 ms 39792 KB Output is correct
3 Correct 415 ms 39792 KB Output is correct
4 Correct 362 ms 39792 KB Output is correct
5 Correct 408 ms 39792 KB Output is correct
6 Correct 542 ms 39792 KB Output is correct
7 Correct 434 ms 39792 KB Output is correct
8 Correct 434 ms 39792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 39792 KB Output is correct
2 Correct 199 ms 39792 KB Output is correct
3 Correct 731 ms 39792 KB Output is correct
4 Correct 744 ms 39792 KB Output is correct
5 Correct 664 ms 39792 KB Output is correct
6 Correct 292 ms 39792 KB Output is correct
7 Correct 143 ms 39792 KB Output is correct
8 Correct 27 ms 39792 KB Output is correct
9 Correct 702 ms 39792 KB Output is correct
10 Correct 655 ms 39792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 676 ms 39792 KB Output is correct
2 Correct 641 ms 39792 KB Output is correct
3 Correct 494 ms 39792 KB Output is correct
4 Correct 656 ms 39792 KB Output is correct
5 Correct 686 ms 39792 KB Output is correct
6 Correct 487 ms 39792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1980 KB Output is correct
3 Correct 3 ms 1980 KB Output is correct
4 Correct 3 ms 1980 KB Output is correct
5 Correct 3 ms 1992 KB Output is correct
6 Correct 3 ms 2084 KB Output is correct
7 Correct 3 ms 2132 KB Output is correct
8 Correct 3 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 3 ms 2184 KB Output is correct
14 Correct 3 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 4 ms 2244 KB Output is correct
17 Correct 4 ms 2244 KB Output is correct
18 Correct 4 ms 2244 KB Output is correct
19 Correct 9 ms 2768 KB Output is correct
20 Correct 9 ms 2768 KB Output is correct
21 Correct 9 ms 2836 KB Output is correct
22 Correct 11 ms 2836 KB Output is correct
23 Correct 10 ms 2956 KB Output is correct
24 Correct 11 ms 3020 KB Output is correct
25 Correct 10 ms 3020 KB Output is correct
26 Correct 10 ms 3020 KB Output is correct
27 Correct 15 ms 39792 KB Output is correct
28 Correct 14 ms 39792 KB Output is correct
29 Correct 15 ms 39792 KB Output is correct
30 Correct 17 ms 39792 KB Output is correct
31 Correct 18 ms 39792 KB Output is correct
32 Correct 22 ms 39792 KB Output is correct
33 Correct 151 ms 39792 KB Output is correct
34 Correct 138 ms 39792 KB Output is correct
35 Correct 140 ms 39792 KB Output is correct
36 Correct 214 ms 39792 KB Output is correct
37 Correct 196 ms 39792 KB Output is correct
38 Correct 190 ms 39792 KB Output is correct
39 Correct 154 ms 39792 KB Output is correct
40 Correct 170 ms 39792 KB Output is correct
41 Correct 160 ms 39792 KB Output is correct
42 Correct 152 ms 39792 KB Output is correct
43 Correct 160 ms 39792 KB Output is correct
44 Correct 163 ms 39792 KB Output is correct
45 Correct 185 ms 39792 KB Output is correct
46 Correct 166 ms 39792 KB Output is correct
47 Correct 160 ms 39792 KB Output is correct
48 Correct 159 ms 39792 KB Output is correct
49 Correct 160 ms 39792 KB Output is correct
50 Correct 159 ms 39792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1980 KB Output is correct
3 Correct 3 ms 1980 KB Output is correct
4 Correct 3 ms 1980 KB Output is correct
5 Correct 3 ms 1992 KB Output is correct
6 Correct 3 ms 2084 KB Output is correct
7 Correct 3 ms 2132 KB Output is correct
8 Correct 3 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 3 ms 2184 KB Output is correct
14 Correct 3 ms 2184 KB Output is correct
15 Correct 3 ms 2184 KB Output is correct
16 Correct 4 ms 2244 KB Output is correct
17 Correct 4 ms 2244 KB Output is correct
18 Correct 4 ms 2244 KB Output is correct
19 Correct 9 ms 2768 KB Output is correct
20 Correct 9 ms 2768 KB Output is correct
21 Correct 9 ms 2836 KB Output is correct
22 Correct 11 ms 2836 KB Output is correct
23 Correct 10 ms 2956 KB Output is correct
24 Correct 11 ms 3020 KB Output is correct
25 Correct 10 ms 3020 KB Output is correct
26 Correct 10 ms 3020 KB Output is correct
27 Correct 419 ms 39792 KB Output is correct
28 Correct 413 ms 39792 KB Output is correct
29 Correct 415 ms 39792 KB Output is correct
30 Correct 362 ms 39792 KB Output is correct
31 Correct 408 ms 39792 KB Output is correct
32 Correct 542 ms 39792 KB Output is correct
33 Correct 434 ms 39792 KB Output is correct
34 Correct 434 ms 39792 KB Output is correct
35 Correct 3 ms 39792 KB Output is correct
36 Correct 199 ms 39792 KB Output is correct
37 Correct 731 ms 39792 KB Output is correct
38 Correct 744 ms 39792 KB Output is correct
39 Correct 664 ms 39792 KB Output is correct
40 Correct 292 ms 39792 KB Output is correct
41 Correct 143 ms 39792 KB Output is correct
42 Correct 27 ms 39792 KB Output is correct
43 Correct 702 ms 39792 KB Output is correct
44 Correct 655 ms 39792 KB Output is correct
45 Correct 676 ms 39792 KB Output is correct
46 Correct 641 ms 39792 KB Output is correct
47 Correct 494 ms 39792 KB Output is correct
48 Correct 656 ms 39792 KB Output is correct
49 Correct 686 ms 39792 KB Output is correct
50 Correct 487 ms 39792 KB Output is correct
51 Correct 15 ms 39792 KB Output is correct
52 Correct 14 ms 39792 KB Output is correct
53 Correct 15 ms 39792 KB Output is correct
54 Correct 17 ms 39792 KB Output is correct
55 Correct 18 ms 39792 KB Output is correct
56 Correct 22 ms 39792 KB Output is correct
57 Correct 151 ms 39792 KB Output is correct
58 Correct 138 ms 39792 KB Output is correct
59 Correct 140 ms 39792 KB Output is correct
60 Correct 214 ms 39792 KB Output is correct
61 Correct 196 ms 39792 KB Output is correct
62 Correct 190 ms 39792 KB Output is correct
63 Correct 154 ms 39792 KB Output is correct
64 Correct 170 ms 39792 KB Output is correct
65 Correct 160 ms 39792 KB Output is correct
66 Correct 152 ms 39792 KB Output is correct
67 Correct 160 ms 39792 KB Output is correct
68 Correct 163 ms 39792 KB Output is correct
69 Correct 185 ms 39792 KB Output is correct
70 Correct 166 ms 39792 KB Output is correct
71 Correct 160 ms 39792 KB Output is correct
72 Correct 159 ms 39792 KB Output is correct
73 Correct 160 ms 39792 KB Output is correct
74 Correct 159 ms 39792 KB Output is correct
75 Correct 491 ms 39792 KB Output is correct
76 Correct 483 ms 39828 KB Output is correct
77 Correct 486 ms 39828 KB Output is correct
78 Correct 462 ms 39828 KB Output is correct
79 Correct 508 ms 39828 KB Output is correct
80 Correct 491 ms 39828 KB Output is correct
81 Correct 731 ms 39828 KB Output is correct
82 Correct 722 ms 39828 KB Output is correct
83 Correct 722 ms 39828 KB Output is correct
84 Correct 696 ms 39828 KB Output is correct
85 Correct 732 ms 39828 KB Output is correct
86 Correct 759 ms 39828 KB Output is correct
87 Correct 689 ms 39828 KB Output is correct
88 Correct 575 ms 39828 KB Output is correct
89 Correct 564 ms 39828 KB Output is correct
90 Correct 589 ms 39828 KB Output is correct
91 Correct 573 ms 39828 KB Output is correct
92 Correct 587 ms 39828 KB Output is correct
93 Correct 664 ms 39828 KB Output is correct
94 Correct 642 ms 39852 KB Output is correct
95 Correct 634 ms 39852 KB Output is correct
96 Correct 611 ms 39852 KB Output is correct
97 Correct 677 ms 39852 KB Output is correct
98 Correct 532 ms 39852 KB Output is correct
99 Correct 665 ms 39852 KB Output is correct
100 Correct 594 ms 39852 KB Output is correct
101 Correct 584 ms 39852 KB Output is correct
102 Correct 616 ms 39852 KB Output is correct
103 Correct 641 ms 39852 KB Output is correct
104 Correct 609 ms 39852 KB Output is correct
105 Correct 481 ms 39852 KB Output is correct
106 Correct 577 ms 39852 KB Output is correct
107 Correct 584 ms 39852 KB Output is correct
108 Correct 583 ms 39852 KB Output is correct
109 Correct 577 ms 39852 KB Output is correct
110 Correct 589 ms 39852 KB Output is correct
111 Correct 565 ms 39852 KB Output is correct
112 Correct 587 ms 39852 KB Output is correct
113 Correct 570 ms 39852 KB Output is correct
114 Correct 565 ms 39852 KB Output is correct
115 Correct 633 ms 39852 KB Output is correct
116 Correct 580 ms 39852 KB Output is correct