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;
struct SegTree {
struct node {
int min;
int max;
node(int a, int b) {
min = a;
max = b;
}
node() {};
};
inline node combine(node a, node b) {
return node(min(a.min, b.min), max(a.max, b.max));
}
int size = 1;
vector<node> tree;
vector<int> arr;
inline void build(int v, int tl, int tr, vector<int> &vec) {
if (tl == tr) {
tree[v] = node(vec[tl], vec[tl]);
return;
}
int tm = (tl + tr) >> 1;
build(v + v, tl, tm, vec);
build(v + v + 1, tm + 1, tr, vec);
tree[v] = combine(tree[v + v], tree[v + v + 1]);
}
inline void init(vector<int> &vec) {
size = vec.size();
arr = vec;
tree.resize(size * 4 + 5);
build(1, 0, size - 1, vec);
}
inline void update(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
tree[v] = node(val, val);
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) update(v + v, tl, tm, pos, val);
else
update(v + v + 1, tm + 1, tr, pos, val);
tree[v] = combine(tree[v + v], tree[v + v + 1]);
}
inline void update(int pos, int val) {
update(1, 0, size - 1, pos, val);
}
inline void tree_swap(int a, int b) {
update(b, arr[a]);
update(a, arr[b]);
swap(arr[a], arr[b]);
}
inline node get(int v, int tl, int tr, int l, int r) {
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return node(1e9, -1e9);
int tm = (tl + tr) >> 1;
return combine(get(v + v, tl, tm, l, r), get(v + v + 1, tm + 1, tr, l, r));
}
inline node get(int l, int r) {
return get(1, 0, size - 1, l, r);
}
inline node get(int i) {
return get(0, i);
}
inline int get_diff(int i) {
node p = get(1, 0, size - 1, 0, i);
return p.max - p.min + 1;
}
};
int N;
int H, W;
vector<int> row, col;
SegTree A, B;
void give_initial_chart(int _H, int _W, vector<int> _R, vector<int> _C) {
H = _H;
W = _W;
row = _R;
col = _C;
A.init(row);
B.init(col);
N = H * W;
}
int swap_seats(int a, int b) {
if (a > b) swap(a, b);
A.tree_swap(a, b);
B.tree_swap(a, b);
swap(row[a], row[b]);
swap(col[a], col[b]);
int ans = 0;
int i = 0;
while (i < N) {
int val = A.get_diff(i) * B.get_diff(i);
if (val == i + 1) {
i++;
ans++;
} else {
i = val - 1;
}
}
return ans;
}
# | 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... |