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>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4.2")
#pragma GCC target("avx2")
template<typename T>
using vec = vector<T>;
struct SegmentTree {
vec<pair<int, int>> t; // min, count
vec<int> lazy;
int _sz;
static pair<int, int> combine(const pair<int, int> &a, const pair<int, int> &b) {
pair<int, int> res = a;
if (a.first == b.first) {
res.second += b.second;
} else if (a.first > b.first) {
res = b;
}
return res;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = {0, 1};
lazy[v] = 0;
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
t[v] = combine(t[v * 2], t[v * 2 + 1]);
lazy[v] = 0;
}
void push(int v, int tl, int tr) {
if (lazy[v] == 0) {
return;
}
if (tl != tr) {
int tm = (tl + tr) / 2;
t[v * 2].first += lazy[v];
t[v * 2 + 1].first += lazy[v];
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
}
lazy[v] = 0;
}
pair<int, int> get(int v, int tl, int tr, int l, int r) {
if (l > r) {
return {INT_MAX, 0};
}
push(v, tl, tr);
if (l == tl and r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return combine(get(v * 2, tl, tm, l, min(r, tm)), get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
void update(int v, int tl, int tr, int l, int r, int val) {
if (l > r) {
return;
}
push(v, tl, tr);
if (l == tl and r == tr) {
t[v].first += val;
lazy[v] = val;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
update(v * 2, tl, tm, l, min(r, tm), val);
update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
t[v] = combine(t[v * 2], t[v * 2 + 1]);
}
public:
void init(int sz) {
_sz = sz;
t.resize(4 * _sz);
lazy.resize(4 * _sz);
build(1, 0, _sz - 1);
}
int get(int l, int r) {
auto [val, cnt] = get(1, 0, _sz - 1, l, r);
if (val > 4) return 0;
return cnt;
}
int get1(int l, int r) {
auto [val, cnt] = get(1, 0, _sz - 1, l, r);
return val;
}
void update(int l, int r, int val) {
update(1, 0, _sz - 1, l, r, val);
}
};
vec<pair<int, int>> pos;
SegmentTree stree;
vec<vec<int>> mtr;
struct fetch {
int l, r, x;
};
array<fetch, 2> get_fetches(int i, int j) {
int vals[4] = {mtr[i][j], mtr[i + 1][j], mtr[i][j + 1], mtr[i + 1][j + 1]};
for (int h = 0; h < 4; h++) {
for (int t = h + 1; t < 4; t++) {
if (vals[h] > vals[t]) {
swap(vals[h], vals[t]);
}
}
}
array<fetch, 2> res{};
res[0] = {vals[0], vals[1] - 1, 1};
res[1] = {vals[2], vals[3] - 1, 1000};
return res;
}
void apply(int i, int j, bool inverse = false) {
auto ft = get_fetches(i, j);
for (auto [l, r, x]: ft) {
if (inverse) x *= -1;
stree.update(l, r, x);
}
}
void Swap(int i, int j, int x, int y) {
for (int dx = -1; dx <= 0; dx++) {
for (int dy = -1; dy <= 0; dy++) {
apply(i + dx, j + dy, true);
}
}
for (int dx = -1; dx <= 0; dx++) {
for (int dy = -1; dy <= 0; dy++) {
apply(x + dx, y + dy, true);
}
}
swap(mtr[i][j], mtr[x][y]);
for (int dx = -1; dx <= 0; dx++) {
for (int dy = -1; dy <= 0; dy++) {
apply(i + dx, j + dy);
}
}
for (int dx = -1; dx <= 0; dx++) {
for (int dy = -1; dy <= 0; dy++) {
apply(x + dx, y + dy);
}
}
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
pos.resize(H * W);
stree.init(H * W + 2);
mtr.resize(H + 2, vec<int>(W + 2, H * W + 1));
for (int i = 0; i < H * W; i++) {
pos[i] = {R[i] + 1, C[i] + 1};
mtr[R[i] + 1][C[i] + 1] = i + 1;
}
for (int i = 0; i <= H; i++) {
for (int j = 0; j <= W; j++) {
apply(i, j);
}
}
}
int swap_seats(int a, int b) {
Swap(pos[a].first, pos[a].second, pos[b].first, pos[b].second);
swap(pos[a], pos[b]);
return stree.get(1, pos.size());
}
Compilation message (stderr)
seats.cpp: In member function 'void SegmentTree::push(int, int, int)':
seats.cpp:47:17: warning: unused variable 'tm' [-Wunused-variable]
47 | int tm = (tl + tr) / 2;
| ^~
# | 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... |