Submission #77875

# Submission time Handle Problem Language Result Execution time Memory
77875 2018-10-01T02:16:13 Z autumn_eel Circle selection (APIO18_circle_selection) C++14
100 / 100
904 ms 48108 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;
	}
};
struct node{
	int bbox[2][2];
	node*l,*r;
	vector<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(vector<Circle*>&circle,int result[2][2]){
	init(result);
	for(auto&c:circle){
		mergeAABB(result,c->bbox,result);
	}
}
node bvh[400000];
int pointer=0;

inline void setleaf(vector<Circle*>circle,node*n){
	n->l=n->r=NULL;
	n->circle=circle;
}
struct st{
	int cost;
	int id;
	bool operator<(const st&r)const{
		return cost<r.cost;
	}
};
void build(vector<Circle*>&circle,node*n,int depth){
	createAABB(circle,n->bbox);
	if(circle.size()<=2){
		setleaf(circle,n);
		return;
	}
	int axis=-1;
	int minx=INF,maxx=-INF,miny=INF,maxy=-INF;
	rep(i,circle.size()){
		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=circle.size()/2;
	nth_element(circle.begin(),circle.begin()+id,circle.end(),[&](auto a,auto b){
		if(axis==0)return a->x<b->x;
		return a->y<b->y;
	});
	vector<Circle*>left(circle.begin(),circle.begin()+id);
	vector<Circle*>right(circle.begin()+id,circle.end());
	build(left,n->l,depth+1);
	build(right,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{
		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;
	}
}
 
//~ #define DEBUG
Circle c[400000];
 
int main(){
#ifdef DEBUG
	auto start=chrono::system_clock::now();
#endif
	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,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 'void build(std::vector<Circle*>&, node*, int)':
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:61:2: note: in expansion of macro 'rep'
  rep(i,circle.size()){
  ^~~
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:108:3: note: in expansion of macro 'rep'
   rep(i,circle.size()){
   ^~~
circle_selection.cpp: In function 'int main()':
circle_selection.cpp:130: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:133: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 22 ms 23800 KB Output is correct
2 Correct 23 ms 23968 KB Output is correct
3 Correct 21 ms 23968 KB Output is correct
4 Correct 23 ms 23968 KB Output is correct
5 Correct 22 ms 23988 KB Output is correct
6 Correct 22 ms 23988 KB Output is correct
7 Correct 24 ms 24128 KB Output is correct
8 Correct 22 ms 24128 KB Output is correct
9 Correct 22 ms 24128 KB Output is correct
10 Correct 21 ms 24128 KB Output is correct
11 Correct 21 ms 24172 KB Output is correct
12 Correct 25 ms 24172 KB Output is correct
13 Correct 24 ms 24172 KB Output is correct
14 Correct 23 ms 24172 KB Output is correct
15 Correct 22 ms 24188 KB Output is correct
16 Correct 22 ms 24188 KB Output is correct
17 Correct 23 ms 24188 KB Output is correct
18 Correct 23 ms 24188 KB Output is correct
19 Correct 28 ms 24444 KB Output is correct
20 Correct 27 ms 24500 KB Output is correct
21 Correct 27 ms 24500 KB Output is correct
22 Correct 30 ms 24500 KB Output is correct
23 Correct 31 ms 24500 KB Output is correct
24 Correct 29 ms 24500 KB Output is correct
25 Correct 29 ms 24500 KB Output is correct
26 Correct 29 ms 24508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 469 ms 47904 KB Output is correct
2 Correct 458 ms 47980 KB Output is correct
3 Correct 441 ms 47980 KB Output is correct
4 Correct 393 ms 47980 KB Output is correct
5 Correct 454 ms 47980 KB Output is correct
6 Correct 583 ms 47980 KB Output is correct
7 Correct 451 ms 47980 KB Output is correct
8 Correct 461 ms 47980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 47980 KB Output is correct
2 Correct 233 ms 47980 KB Output is correct
3 Correct 872 ms 47980 KB Output is correct
4 Correct 843 ms 47980 KB Output is correct
5 Correct 726 ms 47980 KB Output is correct
6 Correct 331 ms 47980 KB Output is correct
7 Correct 171 ms 47980 KB Output is correct
8 Correct 46 ms 47980 KB Output is correct
9 Correct 801 ms 47980 KB Output is correct
10 Correct 715 ms 47980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 819 ms 47980 KB Output is correct
2 Correct 782 ms 47980 KB Output is correct
3 Correct 550 ms 47980 KB Output is correct
4 Correct 775 ms 48032 KB Output is correct
5 Correct 811 ms 48032 KB Output is correct
6 Correct 549 ms 48032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23800 KB Output is correct
2 Correct 23 ms 23968 KB Output is correct
3 Correct 21 ms 23968 KB Output is correct
4 Correct 23 ms 23968 KB Output is correct
5 Correct 22 ms 23988 KB Output is correct
6 Correct 22 ms 23988 KB Output is correct
7 Correct 24 ms 24128 KB Output is correct
8 Correct 22 ms 24128 KB Output is correct
9 Correct 22 ms 24128 KB Output is correct
10 Correct 21 ms 24128 KB Output is correct
11 Correct 21 ms 24172 KB Output is correct
12 Correct 25 ms 24172 KB Output is correct
13 Correct 24 ms 24172 KB Output is correct
14 Correct 23 ms 24172 KB Output is correct
15 Correct 22 ms 24188 KB Output is correct
16 Correct 22 ms 24188 KB Output is correct
17 Correct 23 ms 24188 KB Output is correct
18 Correct 23 ms 24188 KB Output is correct
19 Correct 28 ms 24444 KB Output is correct
20 Correct 27 ms 24500 KB Output is correct
21 Correct 27 ms 24500 KB Output is correct
22 Correct 30 ms 24500 KB Output is correct
23 Correct 31 ms 24500 KB Output is correct
24 Correct 29 ms 24500 KB Output is correct
25 Correct 29 ms 24500 KB Output is correct
26 Correct 29 ms 24508 KB Output is correct
27 Correct 40 ms 48032 KB Output is correct
28 Correct 35 ms 48032 KB Output is correct
29 Correct 73 ms 48032 KB Output is correct
30 Correct 41 ms 48032 KB Output is correct
31 Correct 40 ms 48032 KB Output is correct
32 Correct 37 ms 48032 KB Output is correct
33 Correct 163 ms 48032 KB Output is correct
34 Correct 159 ms 48032 KB Output is correct
35 Correct 174 ms 48032 KB Output is correct
36 Correct 256 ms 48032 KB Output is correct
37 Correct 245 ms 48032 KB Output is correct
38 Correct 242 ms 48032 KB Output is correct
39 Correct 198 ms 48032 KB Output is correct
40 Correct 212 ms 48032 KB Output is correct
41 Correct 197 ms 48032 KB Output is correct
42 Correct 177 ms 48032 KB Output is correct
43 Correct 213 ms 48032 KB Output is correct
44 Correct 220 ms 48032 KB Output is correct
45 Correct 201 ms 48032 KB Output is correct
46 Correct 211 ms 48032 KB Output is correct
47 Correct 212 ms 48032 KB Output is correct
48 Correct 210 ms 48032 KB Output is correct
49 Correct 209 ms 48032 KB Output is correct
50 Correct 217 ms 48032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 23800 KB Output is correct
2 Correct 23 ms 23968 KB Output is correct
3 Correct 21 ms 23968 KB Output is correct
4 Correct 23 ms 23968 KB Output is correct
5 Correct 22 ms 23988 KB Output is correct
6 Correct 22 ms 23988 KB Output is correct
7 Correct 24 ms 24128 KB Output is correct
8 Correct 22 ms 24128 KB Output is correct
9 Correct 22 ms 24128 KB Output is correct
10 Correct 21 ms 24128 KB Output is correct
11 Correct 21 ms 24172 KB Output is correct
12 Correct 25 ms 24172 KB Output is correct
13 Correct 24 ms 24172 KB Output is correct
14 Correct 23 ms 24172 KB Output is correct
15 Correct 22 ms 24188 KB Output is correct
16 Correct 22 ms 24188 KB Output is correct
17 Correct 23 ms 24188 KB Output is correct
18 Correct 23 ms 24188 KB Output is correct
19 Correct 28 ms 24444 KB Output is correct
20 Correct 27 ms 24500 KB Output is correct
21 Correct 27 ms 24500 KB Output is correct
22 Correct 30 ms 24500 KB Output is correct
23 Correct 31 ms 24500 KB Output is correct
24 Correct 29 ms 24500 KB Output is correct
25 Correct 29 ms 24500 KB Output is correct
26 Correct 29 ms 24508 KB Output is correct
27 Correct 469 ms 47904 KB Output is correct
28 Correct 458 ms 47980 KB Output is correct
29 Correct 441 ms 47980 KB Output is correct
30 Correct 393 ms 47980 KB Output is correct
31 Correct 454 ms 47980 KB Output is correct
32 Correct 583 ms 47980 KB Output is correct
33 Correct 451 ms 47980 KB Output is correct
34 Correct 461 ms 47980 KB Output is correct
35 Correct 21 ms 47980 KB Output is correct
36 Correct 233 ms 47980 KB Output is correct
37 Correct 872 ms 47980 KB Output is correct
38 Correct 843 ms 47980 KB Output is correct
39 Correct 726 ms 47980 KB Output is correct
40 Correct 331 ms 47980 KB Output is correct
41 Correct 171 ms 47980 KB Output is correct
42 Correct 46 ms 47980 KB Output is correct
43 Correct 801 ms 47980 KB Output is correct
44 Correct 715 ms 47980 KB Output is correct
45 Correct 819 ms 47980 KB Output is correct
46 Correct 782 ms 47980 KB Output is correct
47 Correct 550 ms 47980 KB Output is correct
48 Correct 775 ms 48032 KB Output is correct
49 Correct 811 ms 48032 KB Output is correct
50 Correct 549 ms 48032 KB Output is correct
51 Correct 40 ms 48032 KB Output is correct
52 Correct 35 ms 48032 KB Output is correct
53 Correct 73 ms 48032 KB Output is correct
54 Correct 41 ms 48032 KB Output is correct
55 Correct 40 ms 48032 KB Output is correct
56 Correct 37 ms 48032 KB Output is correct
57 Correct 163 ms 48032 KB Output is correct
58 Correct 159 ms 48032 KB Output is correct
59 Correct 174 ms 48032 KB Output is correct
60 Correct 256 ms 48032 KB Output is correct
61 Correct 245 ms 48032 KB Output is correct
62 Correct 242 ms 48032 KB Output is correct
63 Correct 198 ms 48032 KB Output is correct
64 Correct 212 ms 48032 KB Output is correct
65 Correct 197 ms 48032 KB Output is correct
66 Correct 177 ms 48032 KB Output is correct
67 Correct 213 ms 48032 KB Output is correct
68 Correct 220 ms 48032 KB Output is correct
69 Correct 201 ms 48032 KB Output is correct
70 Correct 211 ms 48032 KB Output is correct
71 Correct 212 ms 48032 KB Output is correct
72 Correct 210 ms 48032 KB Output is correct
73 Correct 209 ms 48032 KB Output is correct
74 Correct 217 ms 48032 KB Output is correct
75 Correct 564 ms 48032 KB Output is correct
76 Correct 500 ms 48032 KB Output is correct
77 Correct 519 ms 48032 KB Output is correct
78 Correct 489 ms 48032 KB Output is correct
79 Correct 584 ms 48032 KB Output is correct
80 Correct 521 ms 48032 KB Output is correct
81 Correct 805 ms 48032 KB Output is correct
82 Correct 821 ms 48032 KB Output is correct
83 Correct 816 ms 48032 KB Output is correct
84 Correct 855 ms 48032 KB Output is correct
85 Correct 891 ms 48108 KB Output is correct
86 Correct 904 ms 48108 KB Output is correct
87 Correct 879 ms 48108 KB Output is correct
88 Correct 718 ms 48108 KB Output is correct
89 Correct 669 ms 48108 KB Output is correct
90 Correct 706 ms 48108 KB Output is correct
91 Correct 676 ms 48108 KB Output is correct
92 Correct 706 ms 48108 KB Output is correct
93 Correct 709 ms 48108 KB Output is correct
94 Correct 740 ms 48108 KB Output is correct
95 Correct 722 ms 48108 KB Output is correct
96 Correct 804 ms 48108 KB Output is correct
97 Correct 773 ms 48108 KB Output is correct
98 Correct 621 ms 48108 KB Output is correct
99 Correct 799 ms 48108 KB Output is correct
100 Correct 746 ms 48108 KB Output is correct
101 Correct 686 ms 48108 KB Output is correct
102 Correct 728 ms 48108 KB Output is correct
103 Correct 709 ms 48108 KB Output is correct
104 Correct 738 ms 48108 KB Output is correct
105 Correct 558 ms 48108 KB Output is correct
106 Correct 674 ms 48108 KB Output is correct
107 Correct 704 ms 48108 KB Output is correct
108 Correct 693 ms 48108 KB Output is correct
109 Correct 677 ms 48108 KB Output is correct
110 Correct 685 ms 48108 KB Output is correct
111 Correct 683 ms 48108 KB Output is correct
112 Correct 687 ms 48108 KB Output is correct
113 Correct 701 ms 48108 KB Output is correct
114 Correct 676 ms 48108 KB Output is correct
115 Correct 674 ms 48108 KB Output is correct
116 Correct 718 ms 48108 KB Output is correct