이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__);
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
struct DS {
vector<int> psum;
void init(int n) {
psum.resize(n + 1);
}
void update(int l, int r, int x) {
if (l > r) {
return;
}
psum[l] += x;
psum[r + 1] -= x;
}
int query() const {
int cur = 0, ans = 0;
for (auto& a : psum) {
ans += (cur += a) == 1;
dbg(cur);
}
return ans;
}
} ds;
int n, m;
vector<vector<int>> arr;
vector<pair<int, int>> pos;
bool ibs(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
void update1(int x, int y, int v) {
int ans = n * m;
auto go = [&](int x, int y) -> void {
if (ibs(x, y)) {
ans = min(ans, arr[x][y]);
}
};
go(x - 1, y);
go(x, y - 1);
ds.update(arr[x][y], ans - 1, v);
}
void update2(int x, int y, int v) {
int adj[4];
for (int i = 0; i < 4; i++) {
int cx = x + dx[i], cy = y + dy[i];
if (ibs(cx, cy)) {
adj[i] = arr[cx][cy];
} else {
adj[i] = 1e9;
}
}
sort(begin(adj), end(adj));
ds.update(adj[1], arr[x][y] - 1, v);
}
void update(int x, int y, int v) {
update1(x, y, v);
update2(x, y, v);
}
void set_pos(int u) {
auto& [x, y] = pos[u];
arr[x][y] = u;
}
void give_initial_chart(int _n, int _m, vector<int> r, vector<int> c) {
n = _n;
m = _m;
ds.init(n * m);
pos.resize(n * m);
arr.resize(n, vector<int>(m));
for (int i = 0; i < n * m; i++) {
pos[i] = {r[i], c[i]};
set_pos(i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
update(i, j, 1);
}
}
}
int swap_seats(int u, int v) {
set<pair<int, int>> changed;
for (auto& [x, y]: {pos[u], pos[v]}) {
changed.emplace(x, y);
for (int i = 0; i < 4; i++) {
int cx = x + dx[i], cy = y + dy[i];
if (ibs(cx, cy)) {
changed.emplace(cx, cy);
}
}
}
for (auto& [x, y] : changed) {
update(x, y, -1);
}
swap(pos[u], pos[v]);
set_pos(u);
set_pos(v);
for (auto& [x, y] : changed) {
update(x, y, 1);
}
return ds.query();
}
# | 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... |