답안 #77973

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77973 2018-10-01T14:35:48 Z autumn_eel 원 고르기 (APIO18_circle_selection) C++14
100 / 100
794 ms 42020 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];
}
struct st{
	int cost;
	int id;
	bool operator<(const st&r)const{
		return cost<r.cost;
	}
};
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:120: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:122: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 4 ms 1912 KB Output is correct
2 Correct 4 ms 2040 KB Output is correct
3 Correct 4 ms 2044 KB Output is correct
4 Correct 4 ms 2044 KB Output is correct
5 Correct 3 ms 2044 KB Output is correct
6 Correct 3 ms 2056 KB Output is correct
7 Correct 3 ms 2100 KB Output is correct
8 Correct 3 ms 2180 KB Output is correct
9 Correct 3 ms 2188 KB Output is correct
10 Correct 3 ms 2188 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 4 ms 2320 KB Output is correct
13 Correct 3 ms 2320 KB Output is correct
14 Correct 4 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 5 ms 2436 KB Output is correct
17 Correct 5 ms 2436 KB Output is correct
18 Correct 4 ms 2436 KB Output is correct
19 Correct 9 ms 3160 KB Output is correct
20 Correct 9 ms 3348 KB Output is correct
21 Correct 8 ms 3412 KB Output is correct
22 Correct 10 ms 3456 KB Output is correct
23 Correct 10 ms 3616 KB Output is correct
24 Correct 10 ms 3680 KB Output is correct
25 Correct 10 ms 3872 KB Output is correct
26 Correct 11 ms 3984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 401 ms 40732 KB Output is correct
2 Correct 392 ms 40808 KB Output is correct
3 Correct 389 ms 40808 KB Output is correct
4 Correct 351 ms 40808 KB Output is correct
5 Correct 402 ms 40808 KB Output is correct
6 Correct 518 ms 40808 KB Output is correct
7 Correct 438 ms 40808 KB Output is correct
8 Correct 416 ms 40808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 40808 KB Output is correct
2 Correct 205 ms 40808 KB Output is correct
3 Correct 752 ms 40808 KB Output is correct
4 Correct 713 ms 40808 KB Output is correct
5 Correct 665 ms 40808 KB Output is correct
6 Correct 308 ms 40808 KB Output is correct
7 Correct 140 ms 40808 KB Output is correct
8 Correct 27 ms 40808 KB Output is correct
9 Correct 765 ms 40944 KB Output is correct
10 Correct 680 ms 40944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 721 ms 40944 KB Output is correct
2 Correct 689 ms 40944 KB Output is correct
3 Correct 480 ms 40944 KB Output is correct
4 Correct 661 ms 40944 KB Output is correct
5 Correct 726 ms 41436 KB Output is correct
6 Correct 491 ms 41436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 2040 KB Output is correct
3 Correct 4 ms 2044 KB Output is correct
4 Correct 4 ms 2044 KB Output is correct
5 Correct 3 ms 2044 KB Output is correct
6 Correct 3 ms 2056 KB Output is correct
7 Correct 3 ms 2100 KB Output is correct
8 Correct 3 ms 2180 KB Output is correct
9 Correct 3 ms 2188 KB Output is correct
10 Correct 3 ms 2188 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 4 ms 2320 KB Output is correct
13 Correct 3 ms 2320 KB Output is correct
14 Correct 4 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 5 ms 2436 KB Output is correct
17 Correct 5 ms 2436 KB Output is correct
18 Correct 4 ms 2436 KB Output is correct
19 Correct 9 ms 3160 KB Output is correct
20 Correct 9 ms 3348 KB Output is correct
21 Correct 8 ms 3412 KB Output is correct
22 Correct 10 ms 3456 KB Output is correct
23 Correct 10 ms 3616 KB Output is correct
24 Correct 10 ms 3680 KB Output is correct
25 Correct 10 ms 3872 KB Output is correct
26 Correct 11 ms 3984 KB Output is correct
27 Correct 23 ms 41436 KB Output is correct
28 Correct 15 ms 41436 KB Output is correct
29 Correct 15 ms 41436 KB Output is correct
30 Correct 17 ms 41436 KB Output is correct
31 Correct 17 ms 41436 KB Output is correct
32 Correct 17 ms 41436 KB Output is correct
33 Correct 136 ms 41436 KB Output is correct
34 Correct 140 ms 41436 KB Output is correct
35 Correct 141 ms 41436 KB Output is correct
36 Correct 195 ms 41436 KB Output is correct
37 Correct 186 ms 41436 KB Output is correct
38 Correct 188 ms 41436 KB Output is correct
39 Correct 165 ms 41436 KB Output is correct
40 Correct 148 ms 41436 KB Output is correct
41 Correct 153 ms 41436 KB Output is correct
42 Correct 139 ms 41436 KB Output is correct
43 Correct 183 ms 41436 KB Output is correct
44 Correct 161 ms 41436 KB Output is correct
45 Correct 157 ms 41436 KB Output is correct
46 Correct 157 ms 41436 KB Output is correct
47 Correct 160 ms 41436 KB Output is correct
48 Correct 164 ms 41436 KB Output is correct
49 Correct 171 ms 41436 KB Output is correct
50 Correct 160 ms 41436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 2040 KB Output is correct
3 Correct 4 ms 2044 KB Output is correct
4 Correct 4 ms 2044 KB Output is correct
5 Correct 3 ms 2044 KB Output is correct
6 Correct 3 ms 2056 KB Output is correct
7 Correct 3 ms 2100 KB Output is correct
8 Correct 3 ms 2180 KB Output is correct
9 Correct 3 ms 2188 KB Output is correct
10 Correct 3 ms 2188 KB Output is correct
11 Correct 3 ms 2320 KB Output is correct
12 Correct 4 ms 2320 KB Output is correct
13 Correct 3 ms 2320 KB Output is correct
14 Correct 4 ms 2320 KB Output is correct
15 Correct 3 ms 2320 KB Output is correct
16 Correct 5 ms 2436 KB Output is correct
17 Correct 5 ms 2436 KB Output is correct
18 Correct 4 ms 2436 KB Output is correct
19 Correct 9 ms 3160 KB Output is correct
20 Correct 9 ms 3348 KB Output is correct
21 Correct 8 ms 3412 KB Output is correct
22 Correct 10 ms 3456 KB Output is correct
23 Correct 10 ms 3616 KB Output is correct
24 Correct 10 ms 3680 KB Output is correct
25 Correct 10 ms 3872 KB Output is correct
26 Correct 11 ms 3984 KB Output is correct
27 Correct 401 ms 40732 KB Output is correct
28 Correct 392 ms 40808 KB Output is correct
29 Correct 389 ms 40808 KB Output is correct
30 Correct 351 ms 40808 KB Output is correct
31 Correct 402 ms 40808 KB Output is correct
32 Correct 518 ms 40808 KB Output is correct
33 Correct 438 ms 40808 KB Output is correct
34 Correct 416 ms 40808 KB Output is correct
35 Correct 3 ms 40808 KB Output is correct
36 Correct 205 ms 40808 KB Output is correct
37 Correct 752 ms 40808 KB Output is correct
38 Correct 713 ms 40808 KB Output is correct
39 Correct 665 ms 40808 KB Output is correct
40 Correct 308 ms 40808 KB Output is correct
41 Correct 140 ms 40808 KB Output is correct
42 Correct 27 ms 40808 KB Output is correct
43 Correct 765 ms 40944 KB Output is correct
44 Correct 680 ms 40944 KB Output is correct
45 Correct 721 ms 40944 KB Output is correct
46 Correct 689 ms 40944 KB Output is correct
47 Correct 480 ms 40944 KB Output is correct
48 Correct 661 ms 40944 KB Output is correct
49 Correct 726 ms 41436 KB Output is correct
50 Correct 491 ms 41436 KB Output is correct
51 Correct 23 ms 41436 KB Output is correct
52 Correct 15 ms 41436 KB Output is correct
53 Correct 15 ms 41436 KB Output is correct
54 Correct 17 ms 41436 KB Output is correct
55 Correct 17 ms 41436 KB Output is correct
56 Correct 17 ms 41436 KB Output is correct
57 Correct 136 ms 41436 KB Output is correct
58 Correct 140 ms 41436 KB Output is correct
59 Correct 141 ms 41436 KB Output is correct
60 Correct 195 ms 41436 KB Output is correct
61 Correct 186 ms 41436 KB Output is correct
62 Correct 188 ms 41436 KB Output is correct
63 Correct 165 ms 41436 KB Output is correct
64 Correct 148 ms 41436 KB Output is correct
65 Correct 153 ms 41436 KB Output is correct
66 Correct 139 ms 41436 KB Output is correct
67 Correct 183 ms 41436 KB Output is correct
68 Correct 161 ms 41436 KB Output is correct
69 Correct 157 ms 41436 KB Output is correct
70 Correct 157 ms 41436 KB Output is correct
71 Correct 160 ms 41436 KB Output is correct
72 Correct 164 ms 41436 KB Output is correct
73 Correct 171 ms 41436 KB Output is correct
74 Correct 160 ms 41436 KB Output is correct
75 Correct 507 ms 41436 KB Output is correct
76 Correct 442 ms 41464 KB Output is correct
77 Correct 484 ms 41464 KB Output is correct
78 Correct 444 ms 41464 KB Output is correct
79 Correct 530 ms 41464 KB Output is correct
80 Correct 483 ms 41464 KB Output is correct
81 Correct 786 ms 41464 KB Output is correct
82 Correct 740 ms 41464 KB Output is correct
83 Correct 794 ms 41464 KB Output is correct
84 Correct 673 ms 41464 KB Output is correct
85 Correct 700 ms 41464 KB Output is correct
86 Correct 684 ms 41464 KB Output is correct
87 Correct 697 ms 41464 KB Output is correct
88 Correct 606 ms 41464 KB Output is correct
89 Correct 565 ms 41464 KB Output is correct
90 Correct 574 ms 41464 KB Output is correct
91 Correct 559 ms 41464 KB Output is correct
92 Correct 576 ms 41464 KB Output is correct
93 Correct 670 ms 41464 KB Output is correct
94 Correct 682 ms 41468 KB Output is correct
95 Correct 676 ms 41468 KB Output is correct
96 Correct 637 ms 41468 KB Output is correct
97 Correct 687 ms 41468 KB Output is correct
98 Correct 514 ms 41468 KB Output is correct
99 Correct 613 ms 41520 KB Output is correct
100 Correct 631 ms 41520 KB Output is correct
101 Correct 609 ms 41520 KB Output is correct
102 Correct 671 ms 41932 KB Output is correct
103 Correct 654 ms 41932 KB Output is correct
104 Correct 697 ms 41932 KB Output is correct
105 Correct 513 ms 41932 KB Output is correct
106 Correct 597 ms 41964 KB Output is correct
107 Correct 579 ms 41964 KB Output is correct
108 Correct 568 ms 41964 KB Output is correct
109 Correct 596 ms 41964 KB Output is correct
110 Correct 582 ms 41964 KB Output is correct
111 Correct 585 ms 41964 KB Output is correct
112 Correct 612 ms 41964 KB Output is correct
113 Correct 600 ms 41964 KB Output is correct
114 Correct 582 ms 41964 KB Output is correct
115 Correct 596 ms 41964 KB Output is correct
116 Correct 599 ms 42020 KB Output is correct