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 <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e6 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
typedef tuple <int, int, int, int> pir;
pir add(pir a, pir b) {
auto [minx1, maxx1, miny1, maxy1] = a;
auto [minx2, maxx2, miny2, maxy2] = b;
return {
min(minx1, minx2),
max(maxx1, maxx2),
min(miny1, miny2),
max(maxy1, maxy2)
};
}
pir st[N << 2];
int x[N];
int y[N];
int n,m;
void update(int id, int l, int r, int pos, int x, int y) {
if (l > pos || r < pos)
return;
if (l == r) {
st[id] = {x, x, y, y};
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, pos, x, y);
update(id << 1 | 1, mid + 1, r, pos, x, y);
st[id] = add(st[id << 1], st[id << 1 | 1]);
}
void update(int pos, int x, int y) {
update(1, 0, n - 1, pos, x, y);
}
pir getval(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return {oo, -oo, oo, -oo};
if (l >= u && r <= v)
return st[id];
int mid = (l + r) >> 1;
auto lnode = getval(id << 1, l, mid, u, v);
auto rnode = getval(id << 1 | 1, mid + 1, r, u, v);
return add(lnode, rnode);
}
pir getval(int l, int r) {
return getval(1, 0, n - 1, l, r);
}
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
n = H;
m = W;
for (int i = 0; i < n * m; i++) {
x[i] = R[i], y[i] = C[i];
update(i, x[i], y[i]);
}
}
int brute() {
int ans = 0;
int minx = oo, miny = oo, maxx = -oo, maxy = -oo;
for (int i = 0; i < n * m; i++) {
mini(minx, x[i]);
maxi(maxx, x[i]);
mini(miny, y[i]);
maxi(maxy, y[i]);
int dx = maxx - minx + 1;
int dy = maxy - miny + 1;
ans += (dx * dy == i + 1);
}
return ans;
}
int sub2() {
int res = 1;
pair <int, int> rx = {x[0], x[0]}, ry{y[0], y[0]};
int cur = 0;
while (cur < n * m - 1) {
int dx = rx.se - rx.fi + 1;
int dy = ry.se - ry.fi + 1;
// cerr << cur << " " << dx << " " << dy << "\n";
int cnt = dx * dy;
int t = 0;
if (cnt == cur + 1) {
int d = oo;
mini(d, (dx == n) ? dy : dx);
mini(d, (dy == m) ? dx : dy);
t = cur + d;
res++;
} else {
t = cnt - 1;
}
cur = t;
auto [l, r, u, v] = getval(0, t);
rx = {l, r};
ry = {u, v};
}
return res;
}
int swap_seats(int a, int b) {
swap(x[a], x[b]);
swap(y[a], y[b]);
update(a, x[a], y[a]);
update(b, x[b], y[b]);
if (n * m <= 10000)
return brute();
if (max(n, m) <= 1000)
return sub2();
return -1;
}
//#include "grader.cpp"
# | 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... |