This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 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]);
} 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];
}
}
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];
}
}
vector <vector <int>> a;
vector <int> r, c;
int n, m;
bool ok(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
vector <int> dx = {0, 0, -1, 1};
vector <int> dy = {-1, 1, 0, 0};
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) {
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) {
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) {
modify(l_good, r_good, 100 * 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> start(n * m);
for (int i = 0; i < n * m; ++i) {
start[i] = n * m - (i + 1) - 4;
}
build(0, n * m - 1, 0, 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 = (t[0].mn == 0 ? t[0].cnt : 0);
int min_r = n, max_r = 0, min_c = m, max_c = 0;
for (int i = 0; i < n * m; ++i) {
min_r = min(min_r, r[i]);
max_r = max(max_r, r[i]);
min_c = min(min_c, c[i]);
max_c = max(max_c, c[i]);
if ((min_r == max_r || min_c == max_c) && (max_r - min_r + 1) * (max_c - min_c + 1) == i + 1) {
++ans;
}
}
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... |