QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373791 | #5565. Mirror Madness | kevinyang# | TL | 311ms | 84080kb | C++20 | 10.5kb | 2024-04-02 06:33:49 | 2024-04-02 06:33:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
/* Macros {{{ */
/* A lot of this is from some of Benq's submissions
[https://codeforces.com/profile/Benq]
Ugly af to the eyes, but with vim fold its barable
Hopefully c++20 concepts can make all this stuff must cleaner */
/* Basics {{{ */
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define mp make_pair
#define fi first
#define se second
#define arr array
#define ve vector
using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpi = vector<pi>;
using vpll = vector<pll>;
using vpld = vector<pld>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vvld = vector<vld>;
using vvpi = vector<vpi>;
using vvpll = vector<vpll>;
using vvpld = vector<vpld>;
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz size()
#define rsz(a) resize(a)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define For(i, a, b) for (int i = a; i < b; ++i)
#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(a) For(_, 0, a)
#define each(a, x) for (auto &a : x)
#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)
template <typename T, typename U>
inline void cmin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
inline void cmax(T &x, U y) {
if (x < y) x = y;
}
/*}}}*/
/* IO {{{ */
/* Template Macros {{{ */
#define tcT template <class T
#define tcTU tcT, class U
#define tcTUU tcT, class... U
/*}}}*/
inline namespace Helpers { /*{{{*/
tcT, class = void > struct is_iterable : false_type {};
tcT > struct is_iterable<
T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>
: true_type {};
tcT > constexpr bool is_iterable_v = is_iterable<T>::value;
tcT, class = void > struct is_readable : false_type {};
tcT > struct is_readable<T, typename std::enable_if_t<is_same_v<
decltype(cin >> declval<T &>()), istream &>>>
: true_type {};
tcT > constexpr bool is_readable_v = is_readable<T>::value;
tcT, class = void > struct is_printable : false_type {};
tcT > struct is_printable<T, typename std::enable_if_t<is_same_v<
decltype(cout << declval<T>()), ostream &>>>
: true_type {};
tcT > constexpr bool is_printable_v = is_printable<T>::value;
} /* namespace Helpers */
/*}}}*/
inline namespace Input { /*{{{*/
tcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
tcTUU > void re(T &t, U &...u);
tcTU > void re(pair<T, U> &p); /* pairs */
/* re: read{{{ */
tcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {
cin >> x;
} /* default */
tcT > typename enable_if<needs_input_v<T>, void>::type re(
T &i); // vectors, arrays, etc...
tcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs
tcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {
each(x, i) re(x);
}
tcTUU > void re(T &t, U &...u) {
re(t);
re(u...);
} /* read multiple}}} */
/* rv: resize and read vectors{{{ */
void rv(size_t) {}
tcTUU > void rv(size_t N, ve<T> &t, U &...u);
template <class... U>
void rv(size_t, size_t N2, U &...u);
tcTUU > void rv(size_t N, ve<T> &t, U &...u) {
t.rsz(N);
re(t);
rv(N, u...);
}
template <class... U>
void rv(size_t, size_t N2, U &...u) {
rv(N2, u...);
} /*}}}*/
/* dumb shortcuts to read in ints{{{ */
void decrement() {} /* subtract one from each */
tcTUU > void decrement(T &t, U &...u) {
--t;
decrement(u...);
}
#define ints(...) \
int __VA_ARGS__; \
re(__VA_ARGS__);
#define int1(...) \
ints(__VA_ARGS__); \
decrement(__VA_ARGS__); /*}}}*/
} /* namespace Input */
/*}}}*/
inline namespace ToString { /*{{{*/
tcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;
/* ts: string representation to print */
tcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {
stringstream ss;
ss << fixed << setprecision(15) << v;
return ss.str();
} /* default */
tcT > str bit_vec(T t) { /* bit vector to string */
str res = "{";
For(i, 0, t.sz) res += ts(t[i]);
res += "}";
return res;
}
str ts(ve<bool> v) { return bit_vec(v); }
template <size_t SZ>
str ts(bitset<SZ> b) {
return bit_vec(b);
} /* bit vector */
tcTU > str ts(pair<T, U> p); /* pairs */
tcT > typename enable_if<needs_output_v<T>, str>::type ts(
T v); /* vectors, arrays */
tcTU > str ts(pair<T, U> p) { return "(" + ts(p.fi) + ", " + ts(p.se) + ")"; }
tcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {
/* convert container to string w/ separator sep */
bool fst = 1;
str res = "";
for (const auto &x : v) {
if (!fst) res += sep;
fst = 0;
res += ts(x);
}
return res;
}
tcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {
return "{" + ts_sep(v, ", ") + "}";
}
/* for nested DS */
template <int, class T>
typename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
return {ts(v)};
}
template <int lev, class T>
typename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
if (lev == 0 || !v.sz) return {ts(v)};
ve<str> res;
for (const auto &t : v) {
if (res.sz) res.back() += ",";
ve<str> tmp = ts_lev<lev - 1>(t);
res.insert(end(res), all(tmp));
}
For(i, 0, res.sz) {
str bef = " ";
if (i == 0) bef = "{";
res[i] = bef + res[i];
}
res.back() += "}";
return res;
}
} /* namespace ToString */
/*}}}*/
inline namespace Output { /*{{{*/
template <class T>
void pr_sep(ostream &os, str, const T &t) {
os << ts(t);
}
template <class T, class... U>
void pr_sep(ostream &os, str sep, const T &t, const U &...u) {
pr_sep(os, sep, t);
os << sep;
pr_sep(os, sep, u...);
}
/* print w/ no spaces */
template <class... T>
void pr(const T &...t) {
pr_sep(cout, "", t...);
}
/* print w/ spaces, end with newline */
void ps() { cout << "\n"; }
template <class... T>
void ps(const T &...t) {
pr_sep(cout, " ", t...);
ps();
}
/* debug to cerr */
template <class... T>
void dbg_out(const T &...t) {
pr_sep(cerr, " | ", t...);
cerr << endl;
}
void loc_info(int line, str names) {
cerr << "Line(" << line << ") -> [" << names << "]: ";
}
template <int lev, class T>
void dbgl_out(const T &t) {
cerr << "\n\n" << ts_sep(ts_lev<lev>(t), "\n") << "\n" << endl;
}
} /* namespace Output */
/*}}}}}}}}}*/
int n, m;
vpi vertices, points;
pi start;
vvi point_to_line_type;
ve<vvpi> mps;
void solve() {
re(n, m), rv(n, vertices), re(start);
// make all poins appear above the x axis
int dy = -transform_reduce(all(vertices), INT_MAX, [](auto l, auto r){ return min(l, r); }, [](auto v){ return v.se; });
for(auto &[x, y] : vertices) y+=dy;
start.se += dy;
// I would like all points to appear under the line y=x
int dx = -transform_reduce(all(vertices), INT_MAX, [](auto l, auto r){ return min(l, r); }, [](auto v){ return v.fi - v.se; });
for(auto &[x, y] : vertices) x+=dx;
start.fi += dx;
int sx = transform_reduce(all(vertices), INT_MIN, [](auto l, auto r){ return max(l, r); }, [](auto v){ return v.fi; })+1;
int sy = transform_reduce(all(vertices), INT_MIN, [](auto l, auto r){ return max(l, r); }, [](auto v){ return v.se; })+1;
point_to_line_type.assign(sx, vi(sy));
vertices.pb(vertices[0]);
for(int i=0; i<vertices.sz-1; ++i) {
auto [x0, y0] = vertices[i];
auto [x1, y1] = vertices[i+1];
int line_type = (x0 == x1); // 1 means vertical
while(x0 < x1) {
points.pb({ ++x0, y0 });
point_to_line_type[x0][y0] = line_type;
}
while(x0 > x1) {
points.pb({ --x0, y0 });
point_to_line_type[x0][y0] = line_type;
}
while(y0 < y1) {
points.pb({ x0, ++y0 });
point_to_line_type[x0][y0] = line_type;
}
while(y0 > y1) {
points.pb({ x0, --y0 });
point_to_line_type[x0][y0] = line_type;
}
}
int siz = transform_reduce(all(points), 0, [](auto l, auto r){ return max(l, r); }, [](auto v){ return v.fi + v.se; }) + 1;
mps.assign(2, vvpi(siz, vpi()));
for(auto [x, y] : points) mps[0][x-y].pb({x, y}), mps[1][x+y].pb({x, y});
// auto cmp1 = [](auto l, auto r) { return (l.se - l.fi) < (r.se - r.fi); };
// auto cmp2 = [](auto l, auto r) { return (l.fi + l.se) < (r.fi + r.se); };
for(int j=0; j<siz; ++j) sort(all(mps[0][j])), sort(all(mps[1][j]));
vpi res;
int dir = 0; // 0 up right, 1 up left, 2 down left, 3 down right
pi cur = start;
rep(m) {
auto [cx, cy] = cur;
int line_id = (dir%2) ? cx+cy : cx-cy;
vpi &cur_line = mps[dir%2][line_id];
// ps(cx, cy, dir, line_id);
// ps(cx, cy, dir, cur_line);
int nx, ny;
if(dir == 0 || dir == 3) {
auto nxt = ++lower_bound(all(cur_line), cur);
nx = nxt->fi, ny = nxt->se;
} else if (dir == 1 || dir == 2) {
auto nxt = --lower_bound(all(cur_line), cur);
nx = nxt->fi, ny = nxt->se;
}
res.pb({nx, ny});
int tp = point_to_line_type[nx][ny];
// ps(nx, ny, tp);
if(dir == 0 && tp == 0) dir = 3;
else if(dir == 0 && tp == 1) dir = 1;
else if(dir == 1 && tp == 0) dir = 2;
else if(dir == 1 && tp == 1) dir = 0;
else if(dir == 2 && tp == 0) dir = 1;
else if(dir == 2 && tp == 1) dir = 3;
else if(dir == 3 && tp == 0) dir = 0;
else if(dir == 3 && tp == 1) dir = 2;
cur = { nx, ny };
}
for(auto [x, y] : res) ps(x-dx, y-dy);
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
/* cout << fixed << setprecision(6); */
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) solve();
return 0;
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
4 6 0 0 10 0 10 10 0 10 1 0
output:
10 9 9 10 0 1 1 0 10 9 9 10
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
10 10 -2 -2 8 -2 8 8 4 8 4 0 2 0 2 6 -4 6 -4 2 -2 2 4 1
output:
8 5 5 8 4 7 8 3 3 -2 -4 5 -3 6 2 1 -1 -2 -2 -1
result:
ok 10 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
20 50 0 0 18 0 18 10 16 10 16 2 14 2 14 10 12 10 12 2 10 2 10 10 8 10 8 2 6 2 6 10 4 10 4 2 2 2 2 10 0 10 1 0
output:
3 2 5 0 7 2 9 0 11 2 13 0 15 2 17 0 18 1 16 3 18 5 16 7 18 9 17 10 16 9 18 7 16 5 18 3 15 0 12 3 14 5 12 7 14 9 13 10 12 9 14 7 12 5 14 3 11 0 8 3 10 5 8 7 10 9 9 10 8 9 10 7 8 5 10 3 7 0 4 3 6 5 4 7 6 9 5 10 4 9 6 7 4 5 6 3 3 0 0 3
result:
ok 50 lines
Test #4:
score: 0
Accepted
time: 204ms
memory: 49788kb
input:
1996 500000 0 0 1994 0 1994 1000 1992 1000 1992 2 1990 2 1990 1000 1988 1000 1988 2 1986 2 1986 1000 1984 1000 1984 2 1982 2 1982 1000 1980 1000 1980 2 1978 2 1978 1000 1976 1000 1976 2 1974 2 1974 1000 1972 1000 1972 2 1970 2 1970 1000 1968 1000 1968 2 1966 2 1966 1000 1964 1000 1964 2 1962 2 1962 ...
output:
3 2 5 0 7 2 9 0 11 2 13 0 15 2 17 0 19 2 21 0 23 2 25 0 27 2 29 0 31 2 33 0 35 2 37 0 39 2 41 0 43 2 45 0 47 2 49 0 51 2 53 0 55 2 57 0 59 2 61 0 63 2 65 0 67 2 69 0 71 2 73 0 75 2 77 0 79 2 81 0 83 2 85 0 87 2 89 0 91 2 93 0 95 2 97 0 99 2 101 0 103 2 105 0 107 2 109 0 111 2 113 0 115 2 117 0 119 2...
result:
ok 500000 lines
Test #5:
score: 0
Accepted
time: 311ms
memory: 84080kb
input:
333332 500000 0 0 333330 0 333330 4 333328 4 333328 2 333326 2 333326 4 333324 4 333324 2 333322 2 333322 4 333320 4 333320 2 333318 2 333318 4 333316 4 333316 2 333314 2 333314 4 333312 4 333312 2 333310 2 333310 4 333308 4 333308 2 333306 2 333306 4 333304 4 333304 2 333302 2 333302 4 333300 4 333...
output:
3 2 5 0 7 2 9 0 11 2 13 0 15 2 17 0 19 2 21 0 23 2 25 0 27 2 29 0 31 2 33 0 35 2 37 0 39 2 41 0 43 2 45 0 47 2 49 0 51 2 53 0 55 2 57 0 59 2 61 0 63 2 65 0 67 2 69 0 71 2 73 0 75 2 77 0 79 2 81 0 83 2 85 0 87 2 89 0 91 2 93 0 95 2 97 0 99 2 101 0 103 2 105 0 107 2 109 0 111 2 113 0 115 2 117 0 119 2...
result:
ok 500000 lines
Test #6:
score: 0
Accepted
time: 2ms
memory: 3788kb
input:
340 3000 2 0 2 -2 4 -2 4 0 6 0 6 2 8 2 8 4 10 4 10 2 12 2 12 0 14 0 14 2 16 2 16 0 18 0 18 -2 20 -2 20 -4 22 -4 22 -6 24 -6 24 -4 26 -4 26 -6 28 -6 28 -4 30 -4 30 -2 32 -2 32 -4 34 -4 34 -6 36 -6 36 -8 38 -8 38 -10 40 -10 40 -8 42 -8 42 -10 44 -10 44 -8 46 -8 46 -10 48 -10 48 -12 50 -12 50 -14 52 -1...
output:
44 -9 3 32 2 31 3 30 32 59 31 60 4 33 45 -8 108 55 107 56 42 -9 43 -10 44 -9 3 32 2 31 3 30 32 59 31 60 4 33 45 -8 108 55 107 56 42 -9 43 -10 44 -9 3 32 2 31 3 30 32 59 31 60 4 33 45 -8 108 55 107 56 42 -9 43 -10 44 -9 3 32 2 31 3 30 32 59 31 60 4 33 45 -8 108 55 107 56 42 -9 43 -10 44 -9 3 32 2 31 ...
result:
ok 3000 lines
Test #7:
score: -100
Time Limit Exceeded
input:
499996 50000 2 0 2 -2 4 -2 4 0 6 0 6 -2 8 -2 8 -4 10 -4 10 -2 12 -2 12 -4 14 -4 14 -6 16 -6 16 -8 18 -8 18 -6 20 -6 20 -4 22 -4 22 -2 24 -2 24 -4 26 -4 26 -2 28 -2 28 -4 30 -4 30 -2 32 -2 32 0 34 0 34 2 36 2 36 4 38 4 38 2 40 2 40 4 42 4 42 6 44 6 44 4 46 4 46 6 48 6 48 8 50 8 50 10 52 10 52 8 54 8 ...