# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1099985 | marvinthang | A Light Inconvenience (CEOI23_light) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*************************************
* author: marvinthang *
* created: 24.08.2023 17:47:06 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int MAX = 5e5 + 5;
int N, S, T;
vector <int> tasks[MAX];
vector <pair <int, int>> edges;
vector <int> adj[MAX];
int deg[MAX];
bool used[MAX];
void init(void) {
cin >> N >> S >> T;
REP(i, N) {
tasks[i].resize(S);
cin >> tasks[i];
}
}
void euler_walk(int u) {
list <int> ans;
while (!adj[u].empty()) {
int id = adj[u].back(); adj[u].pop_back();
if (used[id]) continue;
--deg[u];
used[id] = true;
if (u == edges[id].se) swap(edges[id].fi, edges[id].se);
u = edges[id].se;
--deg[u];
}
for (auto it: ans) euler_walk(it);
}
void add_edge(int u, int v) {
adj[u].push_back(edges.size());
adj[v].push_back(edges.size());
++deg[u]; ++deg[v];
edges.emplace_back(u, v);
}
void process(void) {
for (int mid = S >> 1; mid >= 1; mid >>= 1) {
for (int c = 0; c < S; c += mid << 1) {
edges.clear();
REP(r, N) REP(i, mid) add_edge(tasks[r][c + i], tasks[r][c + i + mid]);
memset(used, false, edges.size() * sizeof(bool));
for (auto [u, v]: edges) {
if (deg[u] & 1) euler_walk(u);
if (deg[v] & 1) euler_walk(v);
}
for (auto [u, v]: edges) {
euler_walk(u);
euler_walk(v);
}
int id = 0;
REP(r, N) REP(i, mid) {
if (tasks[r][c + i] == edges[id++].se) swap(tasks[r][c + i], tasks[r][c + i + mid]);
}
}
}
REP(i, N) {
for (int x: tasks[i]) cout << x << ' ';
cout << '\n';
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("balance");
init();
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}