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: 26.06.2023 11:25:22 *
*************************************/
#include "seats.h"
#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--; )
#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)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
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 << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
struct Node {
int lazy, cnt_min, mi;
Node(int cm = 0, int m = 0): lazy(0), cnt_min(cm), mi(m) {}
void add(int v) {
lazy += v;
mi += v;
}
Node operator + (const Node &other) const {
if (mi == other.mi) return Node(cnt_min + other.cnt_min, mi);
return mi < other.mi ? Node(cnt_min, mi) : Node(other.cnt_min, other.mi);
}
};
// end of template
int H, W, cnt;
vector <vector <int>> board;
vector <int> R, C;
vector <Node> st;
bool inside(int x, int y) {
return 0 <= x && x < H && 0 <= y && y < W;
}
int delta(int i) {
if (!i) return 0;
int res = 0;
REP(sx, 2) REP(sy, 2) {
int cnt = 0;
REP(tx, 2) REP(ty, 2) {
int u = R[i] - sx + tx;
int v = C[i] - sy + ty;
cnt += !inside(u, v) || board[u][v] > i;
}
res += cnt == 1 || cnt == 3 ? 1 : -1;
}
return res;
}
void build(int i, int l, int r, vector <int> &pref) {
if (r - l == 1) {
st[i] = Node(1, pref[l]);
return;
}
int m = l + r >> 1;
build(i << 1, l, m, pref);
build(i << 1 | 1, m, r, pref);
st[i] = st[i << 1] + st[i << 1 | 1];
}
void pushDown(int i) {
if (!st[i].lazy) return;
st[i << 1].add(st[i].lazy);
st[i << 1 | 1].add(st[i].lazy);
st[i].lazy = 0;
}
void update(int i, int l, int r, int u, int v, int val) {
if (l >= v || r <= u) return;
if (u <= l && r <= v) {
st[i].add(val);
return;
}
pushDown(i);
int m = l + r >> 1;
update(i << 1, l, m, u, v, val);
update(i << 1 | 1, m, r, u, v, val);
st[i] = st[i << 1] + st[i << 1 | 1];
}
void give_initial_chart(int h, int w, vector <int> r, vector <int> c) {
H = h; W = w; R = r; C = c;
board.resize(H, vector<int>(W));
REP(i, H * W) board[R[i]][C[i]] = i;
st.resize(H * W * 4 + 23);
vector <int> pref(H * W);
FOR(i, 1, H * W) pref[i] = pref[i - 1] + delta(i);
build(1, 0, H * W, pref);
}
int swap_seats(int a, int b) {
set <pair <int, int>> ss;
for (int x = -1; x <= 1; ++x) for (int y = -1; y <= 1; ++y) {
int u = R[a] + x, v = C[a] + y;
if (inside(u, v)) ss.insert(make_pair(board[u][v], delta(board[u][v])));
u = R[b] + x, v = C[b] + y;
if (inside(u, v)) ss.insert(make_pair(board[u][v], delta(board[u][v])));
}
swap(board[R[a]][C[a]], board[R[b]][C[b]]);
swap(R[a], R[b]);
swap(C[a], C[b]);
for (auto [x, r]: ss) update(1, 0, H * W, x, H * W, delta(x) - r);
return st[1].cnt_min;
}
#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "seats.h"
namespace {
int read_int() {
int x;
if (scanf("%d", &x) != 1) {
fprintf(stderr, "Error while reading input\n");
exit(1);
}
return x;
}
} // namespace
int main() {
file("seats");
int H = read_int();
int W = read_int();
int Q = read_int();
std::vector<int> R(H * W), C(H * W);
for (int i = 0; i < H * W; ++i) {
R[i] = read_int();
C[i] = read_int();
}
std::vector<int> A(Q), B(Q);
for (int j = 0; j < Q; ++j) {
A[j] = read_int();
B[j] = read_int();
}
give_initial_chart(H, W, R, C);
for (int j = 0; j < Q; ++j) {
int answer = swap_seats(A[j], B[j]);
printf("%d\n", answer);
}
return 0;
}
#endif
Compilation message (stderr)
seats.cpp: In function 'void build(int, int, int, std::vector<int>&)':
seats.cpp:90:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int m = l + r >> 1;
| ~~^~~
seats.cpp: In function 'void update(int, int, int, int, int, int)':
seats.cpp:110:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
110 | int m = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |