답안 #252907

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252907 2020-07-26T12:33:46 Z user202729 원 고르기 (APIO18_circle_selection) C++17
100 / 100
2289 ms 118244 KB
// Remark: when the program exceeds the time limit by a small factor, I usually look for constant speedups first.
// That's a bad habit.
// (okay, bitset is a special case.)
#ifndef LOCAL
#define NDEBUG 1
#endif
#include<bits/stdc++.h>

#pragma GCC optimize("O3")

auto const callRange=[](auto& object, int first, int last, auto function){
	auto iterator=object.lower_bound(first);
	while(iterator!=object.end() and iterator->first<last){
		function(iterator->second);
		++iterator;
	}
};

int main(){
	std::ios::sync_with_stdio(0);std::cin.tie(0);

	using Index=int; // for circles
	struct Circle{
		int x, y, r;
		bool intersect(Circle other) const{
			assert(r>0); assert(other.r>0);
			return int64_t(x-other.x)*(x-other.x)+int64_t(y-other.y)*(y-other.y)<=
				int64_t(r+other.r)*(r+other.r);
		}
	};
	int number; std::cin>>number;
	std::vector<Circle> circles(number);
	for(auto& [x, y, r]: circles){
		std::cin>>x>>y>>r; // r==0: deleted
		x+=1000000000; y+=1000000000;
		assert(x>=0 and y>=0);
	}

	struct A{
		Index index; int r;
		bool operator<(A other) const{
			return r!=other.r ? r<other.r: index>other.index;
		}
	};
	std::priority_queue<A> q;
	for(Index index=0; index<number; ++index)
		q.push({index, circles[index].r});


	std::vector<int> result(number);
	assert((result.assign(number, -1), true));

	struct Cell{
		std::vector<Index> indices;
		std::array<std::array<Cell*, 3>, 3> adjacent; // the numbering scheme is obvious. Note that [1][1] is always *this.
		std::array<std::array<Cell*, 2>, 2> children;
	};
	std::deque<Cell> grid;
	grid.resize(1);
	for(Index index=0; index<number; ++index)
		grid[0].indices.push_back(index);
	grid[0].adjacent[1][1]=&grid[0];

	std::vector<Cell*> contain(number, &grid[0]);

	for(int step=30; step>1;--step){
		auto oldGrid=std::move(grid);
		grid.clear();

		// construct all cells and children link of old layer & new contain array
		for(auto& cell: oldGrid){
			std::array<std::array<Cell, 2>, 2> children{};
			for(Index index: cell.indices){
				auto const [x, y, r]=circles[index];
				assert(r>0);
				children[x>>step&1][y>>step&1].indices.push_back(index);
			}

			for(int c=0; c<2; ++c)
				for(int d=0; d<2; ++d)
					if(not children[c][d].indices.empty()){
						grid.push_back(std::move(children[c][d]));
						auto const cur=&grid.back();
						cell.children[c][d]=cur;
						for(Index index: cur->indices) contain[index]=cur;
					}
		}

		// construct adjacent link of new layer
		for(auto& cell: oldGrid){
			std::array<std::array<Cell*, 4>, 4> data{};
			for(int c=0; c<3; ++c)
			for(int d=0; d<3; ++d){
				Cell* cur=(c==1 and d==1 ? &cell: cell.adjacent[c][d]);
				if(not cur) continue;
				for(int e=0; e<2; ++e)
				for(int f=0; f<2; ++f){
					unsigned const g=c*2+e-1, h=d*2+f-1;
					if(g<4 and h<4) data[g][h]=cur->children[e][f];
				}
			}

			for(int e=0; e<2; ++e)
			for(int f=0; f<2; ++f){
				Cell* cur=cell.children[e][f];
				if(not cur) continue;
				for(int c=0; c<3; ++c)
				for(int d=0; d<3; ++d){
					cur->adjacent[c][d]=data[c+e][d+f];
				}
			}
		}
					
		while(true){
			if(q.empty())
				goto break_outer;

			auto [index, r_]=q.top();
			auto const cur=circles[index]; auto const [x, y, r]=cur;
			if(r==0) {
				q.pop();
				continue;
			}
			assert(r==r_);
			if(r<((1<<step)>>2))
				break; // goto next step -- without popping from q

			q.pop();
			circles[index].r=0;

			assert(result[index]==-1); result[index]=index;
			auto const curCell=contain[index];
			assert(std::any_of(begin(grid), end(grid), [&](auto const& it){ return &it==curCell; }));

			assert(curCell->adjacent[1][1]==curCell);
			
			auto const process=[&](Cell* cell){
				if(not cell) return;
				cell->indices.erase(std::remove_if(begin(cell->indices), end(cell->indices), [&](Index index1){
					if(index1==index){
						return true;
					}

					auto& cur1=circles[index1];
					assert(cur1.r>0);
					if(cur1.intersect(cur)){
						assert(result[index1]==-1);
						result[index1]=index;
						cur1.r=0;
						return true;
					} else return false;
				}),cell->indices.end());
			};
			for(int c=0; c<3; ++c)
			for(int d=0; d<3; ++d)
				process(curCell->adjacent[c][d]);
		}
	}
break_outer:

	for(Index index=0; index<number; ++index){
		auto const [x, y, r]=circles[index];
		assert(result[index]!=-1);
		assert(r==0);
	}

	for(auto it: result) std::cout<<it+1<<' ';
	std::cout<<'\n';
}

Compilation message

circle_selection.cpp: In function 'int main()':
circle_selection.cpp:74:24: warning: unused variable 'r' [-Wunused-variable]
     auto const [x, y, r]=circles[index];
                        ^
circle_selection.cpp:118:19: warning: unused variable 'r_' [-Wunused-variable]
    auto [index, r_]=q.top();
                   ^
circle_selection.cpp:119:54: warning: unused variable 'x' [-Wunused-variable]
    auto const cur=circles[index]; auto const [x, y, r]=cur;
                                                      ^
circle_selection.cpp:119:54: warning: unused variable 'y' [-Wunused-variable]
circle_selection.cpp:162:22: warning: unused variable 'x' [-Wunused-variable]
   auto const [x, y, r]=circles[index];
                      ^
circle_selection.cpp:162:22: warning: unused variable 'y' [-Wunused-variable]
circle_selection.cpp:162:22: warning: unused variable 'r' [-Wunused-variable]
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 13 ms 2304 KB Output is correct
23 Correct 14 ms 2304 KB Output is correct
24 Correct 13 ms 2304 KB Output is correct
25 Correct 16 ms 2304 KB Output is correct
26 Correct 14 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 14952 KB Output is correct
2 Correct 216 ms 14696 KB Output is correct
3 Correct 208 ms 18332 KB Output is correct
4 Correct 210 ms 14952 KB Output is correct
5 Correct 346 ms 16556 KB Output is correct
6 Correct 561 ms 29924 KB Output is correct
7 Correct 380 ms 17540 KB Output is correct
8 Correct 442 ms 18112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 299 ms 35996 KB Output is correct
3 Correct 1131 ms 108516 KB Output is correct
4 Correct 1194 ms 108212 KB Output is correct
5 Correct 846 ms 74212 KB Output is correct
6 Correct 363 ms 36200 KB Output is correct
7 Correct 173 ms 20336 KB Output is correct
8 Correct 35 ms 4788 KB Output is correct
9 Correct 1045 ms 99096 KB Output is correct
10 Correct 753 ms 61924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1090 ms 111576 KB Output is correct
2 Correct 2289 ms 111300 KB Output is correct
3 Correct 327 ms 26216 KB Output is correct
4 Correct 1542 ms 118056 KB Output is correct
5 Correct 1544 ms 118244 KB Output is correct
6 Correct 289 ms 23972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 13 ms 2304 KB Output is correct
23 Correct 14 ms 2304 KB Output is correct
24 Correct 13 ms 2304 KB Output is correct
25 Correct 16 ms 2304 KB Output is correct
26 Correct 14 ms 2304 KB Output is correct
27 Correct 7 ms 1152 KB Output is correct
28 Correct 7 ms 1152 KB Output is correct
29 Correct 7 ms 1152 KB Output is correct
30 Correct 27 ms 4224 KB Output is correct
31 Correct 25 ms 4224 KB Output is correct
32 Correct 27 ms 4224 KB Output is correct
33 Correct 73 ms 6768 KB Output is correct
34 Correct 83 ms 7024 KB Output is correct
35 Correct 80 ms 6260 KB Output is correct
36 Correct 342 ms 37344 KB Output is correct
37 Correct 401 ms 37452 KB Output is correct
38 Correct 316 ms 37328 KB Output is correct
39 Correct 146 ms 9448 KB Output is correct
40 Correct 155 ms 9616 KB Output is correct
41 Correct 149 ms 9492 KB Output is correct
42 Correct 113 ms 8876 KB Output is correct
43 Correct 286 ms 35696 KB Output is correct
44 Correct 292 ms 35700 KB Output is correct
45 Correct 282 ms 35696 KB Output is correct
46 Correct 259 ms 35696 KB Output is correct
47 Correct 258 ms 35952 KB Output is correct
48 Correct 261 ms 35700 KB Output is correct
49 Correct 266 ms 35952 KB Output is correct
50 Correct 254 ms 35696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 4 ms 768 KB Output is correct
20 Correct 4 ms 768 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 13 ms 2304 KB Output is correct
23 Correct 14 ms 2304 KB Output is correct
24 Correct 13 ms 2304 KB Output is correct
25 Correct 16 ms 2304 KB Output is correct
26 Correct 14 ms 2304 KB Output is correct
27 Correct 216 ms 14952 KB Output is correct
28 Correct 216 ms 14696 KB Output is correct
29 Correct 208 ms 18332 KB Output is correct
30 Correct 210 ms 14952 KB Output is correct
31 Correct 346 ms 16556 KB Output is correct
32 Correct 561 ms 29924 KB Output is correct
33 Correct 380 ms 17540 KB Output is correct
34 Correct 442 ms 18112 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 299 ms 35996 KB Output is correct
37 Correct 1131 ms 108516 KB Output is correct
38 Correct 1194 ms 108212 KB Output is correct
39 Correct 846 ms 74212 KB Output is correct
40 Correct 363 ms 36200 KB Output is correct
41 Correct 173 ms 20336 KB Output is correct
42 Correct 35 ms 4788 KB Output is correct
43 Correct 1045 ms 99096 KB Output is correct
44 Correct 753 ms 61924 KB Output is correct
45 Correct 1090 ms 111576 KB Output is correct
46 Correct 2289 ms 111300 KB Output is correct
47 Correct 327 ms 26216 KB Output is correct
48 Correct 1542 ms 118056 KB Output is correct
49 Correct 1544 ms 118244 KB Output is correct
50 Correct 289 ms 23972 KB Output is correct
51 Correct 7 ms 1152 KB Output is correct
52 Correct 7 ms 1152 KB Output is correct
53 Correct 7 ms 1152 KB Output is correct
54 Correct 27 ms 4224 KB Output is correct
55 Correct 25 ms 4224 KB Output is correct
56 Correct 27 ms 4224 KB Output is correct
57 Correct 73 ms 6768 KB Output is correct
58 Correct 83 ms 7024 KB Output is correct
59 Correct 80 ms 6260 KB Output is correct
60 Correct 342 ms 37344 KB Output is correct
61 Correct 401 ms 37452 KB Output is correct
62 Correct 316 ms 37328 KB Output is correct
63 Correct 146 ms 9448 KB Output is correct
64 Correct 155 ms 9616 KB Output is correct
65 Correct 149 ms 9492 KB Output is correct
66 Correct 113 ms 8876 KB Output is correct
67 Correct 286 ms 35696 KB Output is correct
68 Correct 292 ms 35700 KB Output is correct
69 Correct 282 ms 35696 KB Output is correct
70 Correct 259 ms 35696 KB Output is correct
71 Correct 258 ms 35952 KB Output is correct
72 Correct 261 ms 35700 KB Output is correct
73 Correct 266 ms 35952 KB Output is correct
74 Correct 254 ms 35696 KB Output is correct
75 Correct 244 ms 24364 KB Output is correct
76 Correct 250 ms 24040 KB Output is correct
77 Correct 243 ms 24804 KB Output is correct
78 Correct 246 ms 25956 KB Output is correct
79 Correct 263 ms 24292 KB Output is correct
80 Correct 236 ms 25956 KB Output is correct
81 Correct 1179 ms 116328 KB Output is correct
82 Correct 1199 ms 115556 KB Output is correct
83 Correct 1217 ms 116196 KB Output is correct
84 Correct 1109 ms 113880 KB Output is correct
85 Correct 1187 ms 115760 KB Output is correct
86 Correct 1134 ms 115848 KB Output is correct
87 Correct 954 ms 105928 KB Output is correct
88 Correct 549 ms 29236 KB Output is correct
89 Correct 524 ms 28984 KB Output is correct
90 Correct 551 ms 29128 KB Output is correct
91 Correct 597 ms 29088 KB Output is correct
92 Correct 521 ms 29028 KB Output is correct
93 Correct 1208 ms 114156 KB Output is correct
94 Correct 1234 ms 112868 KB Output is correct
95 Correct 1230 ms 114528 KB Output is correct
96 Correct 1139 ms 111808 KB Output is correct
97 Correct 1597 ms 115572 KB Output is correct
98 Correct 605 ms 37804 KB Output is correct
99 Correct 1301 ms 113764 KB Output is correct
100 Correct 1537 ms 117604 KB Output is correct
101 Correct 667 ms 46380 KB Output is correct
102 Correct 1085 ms 101632 KB Output is correct
103 Correct 1054 ms 101488 KB Output is correct
104 Correct 1424 ms 115052 KB Output is correct
105 Correct 460 ms 28052 KB Output is correct
106 Correct 849 ms 99044 KB Output is correct
107 Correct 850 ms 99304 KB Output is correct
108 Correct 851 ms 99112 KB Output is correct
109 Correct 824 ms 99044 KB Output is correct
110 Correct 837 ms 98916 KB Output is correct
111 Correct 867 ms 98916 KB Output is correct
112 Correct 1014 ms 98916 KB Output is correct
113 Correct 885 ms 98904 KB Output is correct
114 Correct 846 ms 99176 KB Output is correct
115 Correct 839 ms 99124 KB Output is correct
116 Correct 841 ms 98860 KB Output is correct