#include <bits/stdc++.h>
using i64 = long long;
struct Point {
i64 x;
i64 y;
};
constexpr double eps = 1e-9;
struct Segment {
Point a;
Point b;
double eval(i64 x) {
assert(a.x <= b.x);
if (a.x == b.x) {
return a.y;
} else {
double ac = a.y * (b.x - x);
double bc = b.y * (x - a.x);
return (ac + bc) / (double)(b.x - a.x);
}
}
};
constexpr i64 maxC = 20000000;
int main() {
auto compSegment = [](Segment a, Segment b) -> bool {
// not cross
bool isSwapped = false;
if (a.a.x < b.a.x) {
std::swap(a, b);
isSwapped = true;
}
i64 s = a.a.y;
const auto t = b.eval(a.a.x);
bool u = s - t < eps;
bool eq = std::abs(s - t) < eps;
if (eq) {
return false;
}
return u xor isSwapped;
};
auto compPoint = [](Point a, Point b) {
// tokuni iminasi
return a.x * maxC + a.y < b.x * maxC + b.y;
};
int N;
std::cin >> N;
std::vector<Segment> segments(N);
for (auto &[as, bs] : segments) {
auto &[ax, ay] = as;
auto &[bx, by] = bs;
std::cin >> ax >> ay >> bx >> by;
}
// std::map<i64, std::vector<int>> addList, eraseList;
std::map<i64, std::vector<std::pair<int, bool>>> eventList;
for (int i = 0; i < N; ++i) {
auto &[as, bs] = segments[i];
if (as.x > bs.x) {
std::swap(as, bs);
}
if (as.x == bs.x) {
assert(as.y != bs.y);
if (as.y > bs.y) {
std::swap(as, bs);
}
}
eventList[as.x].push_back({i, true});
eventList[bs.x].push_back({i, false});
}
std::map<Point, std::pair<int, bool>, decltype(compPoint)> indexList(compPoint);
for (int i = 0; i < N; ++i) {
indexList[segments[i].a] = {i, true};
indexList[segments[i].b] = {i, false};
}
std::set<Segment, decltype(compSegment)> segmentSet(compSegment);
std::vector<Point> safePoint(N);
std::vector<std::pair<Point, Point>> answer;
Point safeMi;
bool isFirst = true;
for (auto &[xVal, vec] : eventList) {
std::sort(vec.begin(), vec.end(), [&](auto &a, auto &b) {
const auto c = a.second ? segments[a.first].a : segments[a.first].b;
const auto d = b.second ? segments[b.first].a : segments[b.first].b;
return c.y < d.y;
});
for (const auto &[i, type] : vec) {
if (type) {
auto itr = segmentSet.insert(segments[i]).first;
if (itr == segmentSet.begin()) {
if (isFirst) {
safeMi = segments[i].a;
isFirst = false;
} else {
answer.push_back({segments[i].a, safeMi});
safeMi = segments[i].a;
}
safePoint[i] = segments[i].a;
if (segments[i].a.x == segments[i].b.x) {
safePoint[i] = segments[i].b;
// ok ?
}
continue;
}
--itr;
answer.push_back({segments[i].a, safePoint[indexList[itr->a].first]});
safePoint[indexList[itr->a].first] = segments[i].a;
safePoint[i] = segments[i].a;
if (segments[i].a.x == segments[i].b.x) {
safePoint[i] = segments[i].b;
// ok ?
}
} else {
auto itr = segmentSet.find(segments[i]);
itr = segmentSet.erase(itr);
if (itr == segmentSet.begin()) {
safeMi = segments[i].b;
continue;
}
--itr;
safePoint[indexList[itr->a].first] = segments[i].b;
}
}
}
assert(answer.size() == N - 1);
for (const auto &[ap, bp] : answer) {
std::cout << ap.x << ' ' << ap.y << ' ' << bp.x << ' ' << bp.y << std::endl;
}
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from roads.cpp:1:
roads.cpp: In function 'int main()':
roads.cpp:130:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<Point, Point> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
130 | assert(answer.size() == N - 1);
| ~~~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
162 ms |
16060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
596 KB |
Output is correct |
4 |
Correct |
36 ms |
4048 KB |
Output is correct |
5 |
Correct |
76 ms |
7792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
48 ms |
4124 KB |
Output is correct |
5 |
Correct |
73 ms |
7852 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
40 ms |
5164 KB |
Output is correct |
10 |
Correct |
586 ms |
47556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
696 KB |
Output is correct |
4 |
Correct |
36 ms |
4116 KB |
Output is correct |
5 |
Correct |
74 ms |
7832 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
39 ms |
4144 KB |
Output is correct |
10 |
Correct |
193 ms |
18868 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
36 ms |
4076 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
41 ms |
5188 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
35 ms |
4144 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
15 ms |
2004 KB |
Output is correct |
23 |
Correct |
15 ms |
2008 KB |
Output is correct |
24 |
Correct |
29 ms |
3684 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
164 ms |
16072 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
35 ms |
4052 KB |
Output is correct |
7 |
Correct |
74 ms |
7808 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
4 ms |
724 KB |
Output is correct |
11 |
Correct |
41 ms |
5204 KB |
Output is correct |
12 |
Correct |
569 ms |
47548 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
612 KB |
Output is correct |
16 |
Correct |
37 ms |
4064 KB |
Output is correct |
17 |
Correct |
192 ms |
18860 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
16 ms |
2016 KB |
Output is correct |
27 |
Correct |
15 ms |
2068 KB |
Output is correct |
28 |
Correct |
30 ms |
3712 KB |
Output is correct |
29 |
Correct |
219 ms |
23868 KB |
Output is correct |
30 |
Correct |
306 ms |
30024 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
320 ms |
30916 KB |
Output is correct |
33 |
Correct |
312 ms |
30932 KB |
Output is correct |
34 |
Correct |
444 ms |
40972 KB |
Output is correct |
35 |
Correct |
434 ms |
42764 KB |
Output is correct |
36 |
Correct |
380 ms |
39740 KB |
Output is correct |