이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAX_N = 1e6 + 5;
struct Seg {
int n;
vi seg, cnt, add;
Seg() {}
Seg(int m) {
n = m;
// for (n = 1; n < m; n <<= 1);
seg.resize(4 * n);
cnt.resize(4 * n);
add.resize(4 * n);
Init(1, 0, n);
}
void Init(int i, int l, int r) {
cnt[i] = r - l;
if (l + 1 == r) return;
Init(i << 1, l, (l + r) >> 1), Init(i << 1 | 1, (l + r) >> 1, r);
}
inline int Val(int i) {
return seg[i] + add[i];
}
inline void push(int i) {
add[i << 1] += add[i];
add[i << 1 | 1] += add[i];
seg[i] += add[i];
add[i] = 0;
}
inline void pull(int i) {
int x1 = Val(i << 1), x2 = Val(i << 1 | 1);
seg[i] = min(x1, x2);
if (x1 == x2) {
cnt[i] = cnt[i << 1] + cnt[i << 1 | 1];
} else if (x1 < x2) {
cnt[i] = cnt[i << 1];
} else cnt[i] = cnt[i << 1 | 1];
}
void update(int a, int b, int x, int i, int l, int r) {
if (a <= l && r <= b) {
add[i] += x;
return;
}
if (r <= a || b <= l) return;
push(i);
update(a, b, x, i << 1, l, (l + r) >> 1);
update(a, b, x, i << 1 | 1, (l + r) >> 1, r);
pull(i);
}
inline void update(int a, int b, int x) {
update(a, b, x, 1, 0, n);
}
};
int r[MAX_N], c[MAX_N];
vvi ind;
//int ind[MAX_N];
Seg seg;
int n;
int h, w;
inline void update(pii p1, pii p2, int x) {
if (p1.x > p2.x) swap(p1.x, p2.x);
if (p1.y > p2.y) swap(p1.y, p2.y);
vi y(4);
y[0] = p1.x < 0 || p1.y < 0 ? n : ind[p1.x][p1.y];
y[1] = p1.x < 0 ? n : ind[p1.x][p2.y];
y[2] = p1.y < 0 ? n : ind[p2.x][p1.y];
y[3] = ind[p2.x][p2.y];
sort(all(y));
seg.update(y[0], y[1], x);
seg.update(y[2], y[3], x);
}
inline void update(pii p1, int x) {
update({p1.x - 1, p1.y - 1}, p1, x);
update({p1.x - 1, p1.y + 1}, p1, x);
update({p1.x + 1, p1.y - 1}, p1, x);
update({p1.x + 1, p1.y + 1}, p1, x);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H, w = W;
n = h * w;
seg = Seg(n);
ind.resize(h + 1, vi(w + 1));
for (int i = 0; i < n; i++) r[i] = R[i], c[i] = C[i];
for (int i = 0; i < n; i++) ind[r[i]][c[i]] = i;
for (int i = 0; i <= w; i++) ind[h][i] = n;
for (int i = 0; i <= h; i++) ind[i][w] = n;
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= w; j++) {
update({i - 1, j - 1}, {i, j}, 1);
}
}
}
int swap_seats(int a, int b) {
// if (a > b) swap(a, b);
update({r[a], c[a]}, -1), update({r[b], c[b]}, -1);
swap(c[a], c[b]), swap(r[a], r[b]);
ind[r[a]][c[a]] = a, ind[r[b]][c[b]] = b;
update({r[a], c[a]}, 1), update({r[b], c[b]}, 1);
return seg.cnt[1];
}
//1 5 3
//0 2
//0 0
//0 3
//0 1
//0 4
//0 1
//2 4
//3 0
# | 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... |