This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "seats.h"
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e3 + 10;
vector< vector<int> > gr;
struct Segment {
int seg[N];
int sz;
void Build(int _sz){
sz = _sz;
fill(seg, seg + N, 0);
}
void Add(int l, int r, int x){
for(int i = l; i < r; i++)
seg[i] += x;
}
pii Get(){
int mn = *min_element(seg, seg + sz);
int cnt = 0;
for(int i = 0; i < sz; i++)
cnt += (seg[i] == mn);
return {mn, cnt};
}
};
Segment DS;
void Add(int x, int y, int z){
vector<pii> V = {{x, y}, {x, y + 1}, {x + 1, y}, {x + 1, y + 1}};
sort(V.begin(), V.end(), [&](auto A, auto B){ return gr[A.F][A.S] < gr[B.F][B.S]; });
vector<int> vl;
for(auto [i, j] : V) vl.push_back(gr[i][j]);
DS.Add(vl[0], vl[1], z * (+1));
DS.Add(vl[2], vl[3], z * (+1));
if(V[0].F != V[1].F && V[0].S != V[1].S)
DS.Add(vl[1], vl[2], z * (+1));
}
vector<int> pR, pC;
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
pR = R; pC = C;
// r = R;
gr.resize(H + 2, vector<int>(W + 2, H*W));
for(int i = 0; i < H*W; i++)
gr[R[i] + 1][C[i] + 1] = i;
DS.Build(H*W);
for(int i = 0; i <= H; i++)
for(int j = 0; j <= W; j++)
Add(i, j, 1);
}
void Change(int x, int y, int w){
Add(x, y, -1);
Add(x - 1, y, -1);
Add(x, y - 1, -1);
Add(x-1, y-1, -1);
gr[x][y] = w;
Add(x, y, +1);
Add(x - 1, y, +1);
Add(x, y - 1, +1);
Add(x-1, y-1, +1);
}
int swap_seats(int a, int b) {
Change(pR[a] + 1, pC[a] + 1, b);
swap(a, b);
Change(pR[a] + 1, pC[a] + 1, b);
swap(pR[a], pR[b]);
swap(pC[a], pC[b]);
pii res = DS.Get();
return res.F <= 4 ? res.S : 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... |