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);
pair <int, int> add(pair <int, int> a, pair <int, int> b) {
return {min(a.fi, b.fi), max(a.se, b.se)};
}
struct segtree {
int n;
vector <pair <int, int>> st;
segtree(int _n = 0) {
n = _n;
st.assign(4 * n + 5, mp(0, 0));
}
void update(int id, int l, int r, int pos, int val) {
if (l > pos || r < pos)
return;
if (l == r) {
st[id] = {val, val};
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, pos, val);
update(id << 1 | 1, mid + 1, r, pos, val);
st[id].fi = min(st[id << 1].fi, st[id << 1 | 1].se);
st[id].se = max(st[id << 1].se, st[id << 1 | 1].se);
}
void update(int pos, int val) {
update(1, 0, n - 1, pos, val);
}
pair <int, int> getval(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return mp(oo, -oo);
if (l >= u && r <= v)
return st[id];
int mid = (l + r) >> 1;
auto [a, b] = getval(id << 1, l, mid, u, v);
auto [x, y] = getval(id << 1 | 1, mid + 1, r, u, v);
return {min(a, x), max(b, y)};
}
pair <int, int> getval(int l, int r) {
return getval(1, 0, n - 1, l, r);
}
};
segtree segX, segY;
int x[N];
int y[N];
int n,m;
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
n = H;
m = W;
segX = segY = segtree(n * m);
for (int i = 0; i < n * m; i++) {
x[i] = R[i], y[i] = C[i];
segX.update(i, x[i]);
segY.update(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;
if (cnt == cur + 1) {
int d = min(dx, dy);
rx = segX.getval(0, cur + d);
ry = segY.getval(0, cur + d);
cur += d;
res++;
} else {
rx = segX.getval(0, cnt - 1);
ry = segY.getval(0, cnt - 1);
cur = cnt - 1;
}
}
return res;
}
int swap_seats(int a, int b) {
swap(x[a], x[b]);
segX.update(a, x[a]);
segX.update(b, x[b]);
swap(y[a], y[b]);
segY.update(a, y[a]);
segY.update(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... |