이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const int N = 1e6 + 7;
const int Inf = 1e9 + 7;
struct SegmentTree {
struct Node {
int mn, cnt;
Node(int x) {
mn = x;
cnt = 1;
}
Node() {
mn = Inf;
cnt = 0;
}
Node operator +(const Node &other) {
Node res;
res.mn = min(mn, other.mn);
if (res.mn == mn) res.cnt += cnt;
if (res.mn == other.mn) res.cnt += other.cnt;
return res;
}
};
Node t[4 * N];
int mod[4 * N];
void update(int v, int val) {
t[v].mn += val;
mod[v] += val;
}
void push(int v) {
if (mod[v]) {
update(2 * v + 1, mod[v]);
update(2 * v + 2, mod[v]);
mod[v] = 0;
}
}
void build(int l, int r, int v, vector <int> &a) {
if (l == r) {
t[v] = Node(a[l]);
mod[v] = 0;
} else {
int m = (l + r) >> 1;
build(l, m, 2 * v + 1, a);
build(m + 1, r, 2 * v + 2, a);
t[v] = t[2 * v + 1] + t[2 * v + 2];
mod[v] = 0;
}
}
void modify(int ql, int qr, int val, int l, int r, int v) {
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
update(v, val);
} else {
push(v);
int m = (l + r) >> 1;
modify(ql, qr, val, l, m, 2 * v + 1);
modify(ql, qr, val, m + 1, r, 2 * v + 2);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
int get(int pos, int l, int r, int v) {
if (l == r) {
return t[v].mn;
} else {
push(v);
int m = (l + r) >> 1;
if (pos <= m) return get(pos, l, m, 2 * v + 1);
else return get(pos, m + 1, r, 2 * v + 2);
}
}
};
vector <vector <int>> a;
vector <int> r, c;
int n, m;
SegmentTree tr, str;
bool ok(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
vector <int> dx = {0, -1, 0, 1};
vector <int> dy = {-1, 0, 1, 0};
int tmm(int x, int y, int k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (ok(nx, ny)) return a[nx][ny];
return n * m;
}
void add(int x, int y, int s) {
vector <int> who;
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k];
int ny = y + dy[k];
if (ok(nx, ny)) {
who.push_back(a[nx][ny]);
}
}
sort(who.begin(), who.end());
{
int l_good = 0;
int r_good = a[x][y] - 1;
if ((int)who.size() >= 2) {
r_good = min(r_good, who[1] - 1);
}
if (l_good <= r_good) {
tr.modify(l_good, r_good, -1 * s, 0, n * m - 1, 0);
}
l_good = a[x][y];
if ((int)who.size() >= 2) {
l_good = max(l_good, who[1]);
} else {
l_good = Inf;
}
r_good = n * m - 1;
if ((int)who.size() >= 3) {
r_good = who[2] - 1;
}
if (l_good <= r_good) {
tr.modify(l_good, r_good, 1 * s, 0, n * m - 1, 0);
}
l_good = a[x][y];
r_good = n * m - 1;
if ((int)who.size() >= 2) {
r_good = who[1] - 1;
}
if (l_good <= r_good) {
tr.modify(l_good, r_good, 100 * s, 0, n * m - 1, 0);
}
}
{
int l_good = 0;
int r_good = a[x][y] - 1;
if ((int)who.size() >= 2) {
r_good = min(r_good, who[1] - 1);
}
if (l_good <= r_good) {
str.modify(l_good, r_good, -1 * s, 0, n * m - 1, 0);
}
for (int i = 0; i < 4; ++i) {
int l = max(a[x][y], max(tmm(x, y, i), tmm(x, y, (i + 1) % 4)));
int r = n * m - 1;
if (l <= r) {
str.modify(l, r, 100 * s, 0, n * m - 1, 0);
}
}
l_good = a[x][y];
r_good = n * m - 1;
if ((int)who.size() >= 2) {
r_good = who[1] - 1;
}
if (l_good <= r_good) {
str.modify(l_good, r_good, 1 * s, 0, n * m - 1, 0);
}
}
}
void give_initial_chart(int N, int M, vector<int> R, vector<int> C) {
n = N, m = M;
r = R, c = C;
a.resize(n, vector <int> (m));
for (int i = 0; i < n * m; ++i) {
a[r[i]][c[i]] = i;
}
vector <int> tr_start(n * m);
for (int i = 0; i < n * m; ++i) {
tr_start[i] = n * m - (i + 1) - 4;
}
vector <int> str_start(n * m);
for (int i = 0; i < n * m; ++i) {
str_start[i] = n * m - (i + 1);
str_start[i] -= min(2, i + 1);
}
tr.build(0, n * m - 1, 0, tr_start);
str.build(0, n * m - 1, 0, str_start);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
add(i, j, 1);
}
}
}
int swap_seats(int x, int y) {
set <pair <int, int>> go;
for (int k = 0; k < 4; ++k) {
int nx = r[x] + dx[k];
int ny = c[x] + dy[k];
if (ok(nx, ny)) {
go.insert({nx, ny});
}
}
go.insert({r[x], c[x]});
for (int k = 0; k < 4; ++k) {
int nx = r[y] + dx[k];
int ny = c[y] + dy[k];
if (ok(nx, ny)) {
go.insert({nx, ny});
}
}
go.insert({r[y], c[y]});
for (auto p : go) {
add(p.first, p.second, -1);
}
swap(a[r[x]][c[x]], a[r[y]][c[y]]);
swap(r[x], r[y]);
swap(c[x], c[y]);
for (auto p : go) {
add(p.first, p.second, 1);
}
int ans = (tr.t[0].mn == 0 ? tr.t[0].cnt : 0) + (str.t[0].mn == 0 ? str.t[0].cnt : 0);;
return ans;
}
#ifdef LOCAL
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m, q;
cin >> n >> m >> q;
vector <int> R(n * m), C(n * m);
for (int i = 0; i < n * m; ++i) {
cin >> R[i] >> C[i];
}
vector <int> aq(q), bq(q);
for (int i = 0; i < q; ++i) {
cin >> aq[i] >> bq[i];
}
give_initial_chart(n, m, R, C);
for (int j = 0; j < q; ++j) {
cout << swap_seats(aq[j], bq[j]) << '\n';
}
}
#endif
# | 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... |