이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "seats.h"
#define F first
#define S second
#define PB push_back
#define sz(s) (int(s.size()))
#define bit(n, k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1e9 + 10, maxn = 1e6 + 10;
int SZ;
int mn[4 * maxn], lz[4 * maxn], cnt[4 * maxn];
void build(int l = 0, int r = SZ, int id = 1){
mn[id] = 0;
lz[id] = 0;
cnt[id] = r-l;
if(r-l == 1)
return;
int mid = (l+r) >> 1;
build(l, mid, 2*id);
build(mid, r, 2*id+1);
}
void shift(int l, int r, int id){
mn[id]+= lz[id];
if(r-l > 1)
lz[2*id]+= lz[id], lz[2*id+1]+= lz[id];
lz[id] = 0;
}
void mrg(int id){
mn[id] = min(mn[2*id], mn[2*id+1]);
cnt[id] = 0;
if(mn[id] == mn[2*id])
cnt[id]+= cnt[2*id];
if(mn[id] == mn[2*id+1])
cnt[id]+= cnt[2*id+1];
}
void add(int f, int s, int x, int l = 0, int r = SZ, int id = 1){
shift(l, r, id);
if(f >= s || l >= r || s <= l || r <= f)
return;
if(f <= l && r <= s){
lz[id] = x;
shift(l, r, id);
return;
}
int mid = (l+r) >> 1;
add(f, s, x, l, mid, 2*id);
add(f, s, x, mid, r, 2*id+1);
mrg(id);
}
int ASK(int pos, int l = 0, int r = SZ, int id = 1){
shift(l, r, id);
if(r-l == 1)
return mn[id];
int mid = (l+r) >> 1;
if(pos < mid)
return ASK(pos, l, mid, 2*id);
else
return ASK(pos, mid, r, 2*id+1);
}
vector<int> A, B;
vector< vector<int> > val;
int N, M;
int what(int x, int y){
if(x < 0 || y < 0 || x >= N || y >= M)
return inf;
return val[x][y];
}
int what(pii p){
return what(p.F, p.S);
}
void chng(int x, int y, int w){
vector<pii> vec = { {x, y}, {x-1, y}, {x, y-1}, {x-1, y-1} };
sort(vec.begin(), vec.end(), [](pii a, pii b){ return what(a) < what(b); });
add(what(vec[0]), what(vec[1]), w);
add(what(vec[2]), what(vec[3]), w);
}
void all(int x, int y, int w){
chng(x, y, w);
chng(x+1, y, w);
chng(x, y+1, w);
chng(x+1, y+1, w);
}
void give_initial_chart(int N, int M, vector<int> A, vector<int> B){
::N = N, ::M = M, ::A = A, ::B = B;
SZ = N * M;
build();
val.resize(N);
for(int i = 0; i < N; i++)
val[i].resize(M);
for(int i = 0; i < N * M; i++)
val[A[i]][B[i]] = i;
for(int i = 0; i <= N; i++){
for(int j = 0; j <= M; j++)
chng(i, j, 1);
}
}
int swap_seats(int a, int b){
pii pa = {A[a], B[a]}, pb = {A[b], B[b]};
swap(A[a], A[b]);
swap(B[a], B[b]);
all(pa.F, pa.S, -1);
val[pa.F][pa.S] = b;
all(pa.F, pa.S, 1);
all(pb.F, pb.S, -1);
val[pb.F][pb.S] = a;
all(pb.F, pb.S, 1);
return mn[1] == 4 ? cnt[1] : 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... |