이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
template<class node, class F = std::function<node(const node &, const node &)>>
class SegTree {
int n = 0;
std::vector<node> t;
F unite;
void build(const int x, const int l, const int r, const std::vector<node> &a) {
if (l == r) {
t[x] = a[l];
return;
}
const int mid = (l + r) / 2;
const int y = 2 * (mid - l + 1) + x;
build(x + 1, l, mid, a);
build(y, mid + 1, r, a);
t[x] = unite(t[x + 1], t[y]);
}
void update(const int x, const int l, const int r, const int p, const node &v) {
if (l == p and p == r) {
t[x] = v;
return;
}
const int mid = (l + r) / 2;
const int y = 2 * (mid - l + 1) + x;
if (p <= mid) {
update(x + 1, l, mid, p, v);
} else {
update(y, mid + 1, r, p, v);
}
t[x] = unite(t[x + 1], t[y]);
}
node query(const int x, const int l, const int r, const int ql, const int qr) const {
if (ql <= l and r <= qr) {
return t[x];
}
const int mid = (l + r) / 2;
const int y = 2 * (mid - l + 1) + x;
if (qr <= mid) {
return query(x + 1, l, mid, ql, qr);
} else if (mid < ql) {
return query(y, mid + 1, r, ql, qr);
} else {
return unite(query(x + 1, l, mid, ql, qr), query(y, mid + 1, r, ql, qr));
}
}
public:
SegTree() = default;
explicit SegTree(const int n, const node e, F f) : n(n), t(2 * n - 1, e), unite(std::move(f)) {}
explicit SegTree(const std::vector<node> &a, F f) : n(a.size()), t(2 * (a.size()) - 1), unite(std::move(f)) {
build(0, 0, n - 1, a);
}
void set(const int p, const node &v) {
assert(0 <= p and p < n);
update(0, 0, n - 1, p, v);
}
node get(const int l, const int r) const {
assert(0 <= l and l <= r and r < n);
return query(0, 0, n - 1, l, r);
}
};
int H, W;
vector<int> R, C;
SegTree<int> leftmost, rightmost, topmost, bottommost;
struct foo {
int l, r, t, b;
};
void give_initial_chart(int H_, int W_, std::vector<int> R_, std::vector<int> C_) {
H = H_, W = W_;
R = move(R_), C = move(C_);
leftmost = SegTree<int>(C, [&](const int l, const int r) {
return min(l, r);
});
rightmost = SegTree<int>(C, [&](const int l, const int r) {
return max(l, r);
});
topmost = SegTree<int>(R, [&](const int l, const int r) {
return min(l, r);
});
bottommost = SegTree<int>(R, [&](const int l, const int r) {
return max(l, r);
});
}
foo query(const int x) {
const int l = leftmost.get(0, x);
const int r = rightmost.get(0, x);
const int t = topmost.get(0, x);
const int b = bottommost.get(0, x);
return foo({l, r, t, b});
}
int count_beautiful_rectangles() {
const int total_seats = H * W;
int ans = 0;
for (int cur_seat = 0; cur_seat < total_seats; cur_seat++) {
const auto [l, r, t, b] = query(cur_seat);
const int rect_size = (r - l + 1) * (b - t + 1);
const bool beautiful = (cur_seat + 1 == rect_size);
ans += beautiful;
}
return ans;
}
int swap_seats(int x, int y) {
swap(R[x], R[y]);
swap(C[x], C[y]);
leftmost.set(x, C[x]), rightmost.set(x, C[x]);
topmost.set(x, R[x]), bottommost.set(x, R[x]);
leftmost.set(y, C[y]), rightmost.set(y, C[y]);
topmost.set(y, R[y]), bottommost.set(y, R[y]);
return count_beautiful_rectangles();
}
# | 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... |