이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "seats.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
int h, w;
std::vector<int> r, c;
vector<vector<int>> arr;
struct Fenwick {
int n, m;
vector<vector<ll>> t;
Fenwick() = default;
Fenwick(int n, int m) : n(n), m(m), t(n, vector<ll>(m)) {}
ll sum(int r, int c) {
ll s = 0;
for (int i = r; i >= 0; i = (i&(i+1))-1)
for (int j = c; j >= 0; j = (j&(j+1))-1)
s += t[i][j];
return s;
}
ll sum(int r1, int c1, int r2, int c2) {
return sum(r2, c2) + sum(r1-1, c1-1) - sum(r1-1, c2) - sum(r2, c1-1);
}
void inc(int r, int c, int d) {
for (; r < n; r |= r+1)
for (int i = c; i < m; i |= i+1)
t[r][i] += d;
}
};
Fenwick ft;
int nextPower2(int n) {
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return 1+n;
}
struct SegmentTree {
struct node {
int cnt = 1;
int min = 0;
int lz = 0;
};
int n;
vector<node> t;
SegmentTree()= default;
void init(int n) {
this->n = n;
t.assign(this->n*4, {});
build(1, 0, n-1);
}
void build(int v, int tl, int tr) {
if (tl == tr) return void(t[v] = node{.cnt=1, .min=0});
int mid = (tl+tr) / 2;
build(v*2, tl, mid);
build(v*2+1, mid+1, tr);
}
void apply(int v, int x) {
t[v].lz += x;
t[v].min += x;
}
void push(int v) {
if (t[v].lz) {
apply(v*2, t[v].lz);
apply(v*2+1, t[v].lz);
t[v].lz = 0;
}
}
node merge(const node& a, const node&b) {
node res{.min = min(a.min, b.min)};
if (a.min == b.min) res.cnt = a.cnt + b.cnt;
else if (a.min< b.min) res.cnt = a.cnt;
else res.cnt = b.cnt;
return res;
}
void update(int l, int r, int x, int v, int tl, int tr) {
if (tl > tr) return;
if (r < tl || tr < l) return;
if (l <= tl && tr <= r) {
apply(v, x);
return;
}
push(v);
int mid = (tl+tr) / 2;
update(l, r, x, v*2, tl, mid);
update(l, r, x, v*2+1, mid+1, tr);
t[v] = merge(t[v*2], t[v*2+1]);
}
void update(int l, int r, int x) {
update(l, r, x, 1, 0, n-1);
}
int query() {
return t[1].cnt;
}
node query(int l, int r, int v, int tl, int tr) {
if (r < tl || tr < l) return {.cnt=0, .min=int(1e9)};
if (l <= tl && tr <= r) return t[v];
push(v);
int mid = (tl+tr) / 2;
return merge(query(l, r, v*2, tl, mid), query(l, r, v*2+1, mid+1, tr));
}
node query(int l, int r) {
return query(l, r, 1, 0, n-1);
}
};
SegmentTree pot;
vector<int> raw;
int add(int i) {
if (i == 0) return 0;
auto& brr = arr[0];
int j = c[i];
int cnt = 0;
if (j == 0 || brr[j-1] > i) cnt++;
else cnt--;
if (j == w -1 || brr[j+1] > i) cnt++;
else cnt--;
return cnt;
};
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
h = H;
w = W;
r = R;
c = C;
arr = vector(H, vector<int>(W, 0));
ft = Fenwick(H, W);
for (int i = 0; i < H * W; i++) {
arr[r[i]][c[i]] = i;
ft.inc(r[i], c[i], i);
}
raw.assign(w, 0);
pot.init(w);
for (int i = 0; i < w; i++) {
int x = add(i);
pot.update(i, w-1, x - raw[i]);
raw[i] = x;
}
#ifdef ONPC
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cout << arr[i][j] << " \n"[j == W-1];
#endif
}
int swap_seats(int a, int b) {
ft.inc(r[a], c[a], -a +b);
ft.inc(r[b], c[b], -b +a);
swap(arr[r[a]][c[a]], arr[r[b]][c[b]]);
swap(r[a], r[b]);
swap(c[a], c[b]);
for (int i : {a-1, a, a+1, b-1, b, b+1}) {
if (0 <= i && i <= w-1) {
int x = add(i);
pot.update(i, w-1, x - raw[i]);
raw[i] = x;
}
}
assert(h == 1);
int ans = pot.query();
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... |