이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ln '\n'
template <class F, class S>
bool chmin(F &u, const S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class S>
bool chmax(F &u, const S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e9;
const int N = 1e6 + 1;
vector <int> R, C, p;
int h, w, n;
struct SegTree{
struct Info{
int mn, cnt, s;
Info(int mn = 0, int cnt = 1, int s = 0) : mn(mn), cnt(cnt), s(s) {}
};
Info T[N * 4];
void merge(Info &rt, const Info &lf, const Info &rg){
rt = Info();
rt.mn = min(lf.mn, rg.mn + lf.s);
rt.cnt = (lf.mn == rt.mn) * lf.cnt + (rt.mn == rg.mn + lf.s) * rg.cnt;
rt.s = lf.s + rg.s;
}
void upd(int v, int l, int r, int p, int x){
if ( l == r ){
T[v] = Info(x, 1, x);
return;
}
int m = (l + r) / 2;
if ( p <= m ) upd(v * 2, l, m, p, x);
else upd(v * 2 + 1, m + 1, r, p, x);
merge(T[v], T[v * 2], T[v * 2 + 1]);
}
void upd(int p, int x){
upd(1, 0, n - 1, p, x);
}
int get(int v, int l, int r, int p){
if ( l == r ) return T[v].mn;
int m = (l + r) / 2;
return p <= m ? get(v * 2, l, m, p) : get(v * 2 + 1, m + 1, r, p);
}
int get(int p){ return get(1, 0, n - 1, p); }
int qry(){ return T[1].cnt; }
} seg;
int z(int u, int v){ return u * w + v; }
int f(int j){
if ( j < 0 || j >= n ) return -1;
int x = j / w, y = j % w;
int cnt = 0;
for ( auto i: {0, -1} ){
for ( auto k: {0, -1} ){
int s = 0, q = 0;
for ( auto a: {0, 1} ){
for ( auto b: {0, 1} ){
int u = x + i + a, v = y + k + b;
if ( u >= 0 && v >= 0 && u < h && v < w ){
s += p[z(u, v)] < p[j];
q += p[z(u, v)] <= p[j];
}
}
}
cnt += (q & 1) - (s & 1);
}
}
return cnt;
}
void upd(int x, int y){
if ( x < 0 || y < 0 || x >= h || y >= w ) return;
int j = z(x, y);
seg.upd(p[j], f(j));
}
void give_initial_chart(int H, int W, std::vector<int> R_, std::vector<int> C_) {
h = H, w = W, R = R_, C = C_, n = h * w;
p.resize(n);
for ( int i = 0; i < h; i++ ){
for ( int j = 0; j < w; j++ ){
int x = z(i, j);
p[z(R[x], C[x])] = x;
}
}
for ( int i = 0; i < n; i++ ){
upd(R[i], C[i]);
}
}
int swap_seats(int a, int b) {
swap(p[z(R[a], C[a])], p[z(R[b], C[b])]);
swap(C[a], C[b]); swap(R[a], R[b]);
for ( auto i: {-1, 0, 1} ){
for ( auto j: {-1, 0, 1} ){
upd(R[a] + i, C[a] + j);
upd(R[b] + i, C[b] + j);
}
}
return seg.qry();
}
# | 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... |