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 = 1e6 + 10;
vector< vector<int> > gr;
int fl;
int ps[N];
struct Segment {
pii seg[N << 2];
int lz[N << 2];
int sz;
void Apply(int id, int x){
seg[id].F += x;
lz[id] += x;
}
void Shift(int id){
Apply(id << 1, lz[id]);
Apply(id<<1|1, lz[id]);
lz[id] = 0;
}
pii Merge(pii A, pii B){
int mn = min(A.F, B.F);
int cnt = 0;
if(mn == A.F) cnt += A.S;
if(mn == B.F) cnt += B.S;
return {mn, cnt};
}
void Build(int id, int L, int R){
lz[id] = 0;
if(L + 1 == R){
seg[id] = {ps[L], 1};
return ;
}
int mid = (L + R) >> 1;
Build(id << 1, L, mid);
Build(id<<1|1, mid, R);
seg[id] = Merge(seg[id << 1], seg[id << 1 | 1]);
}
void Build(int _sz){
sz = _sz;
fl = 1;
for(int i = 1; i < N; i++)
ps[i] += ps[i - 1];
Build(1, 0, sz);
}
void Add(int l, int r, int x, int id = 1, int L = 0, int R = 0){
if(id == 1){
if(!fl){
ps[l] += x;
ps[r] -= x;
return;
}
R = sz;
}
if(r <= L || R <= l) return ;
if(l <= L && R <= r){
Apply(id, x);
return;
}
Shift(id);
int mid = (L + R) >> 1;
Add(l, r, x, id << 1, L, mid);
Add(l, r, x, id<<1|1, mid, R);
seg[id] = Merge(seg[id<<1], seg[id << 1 | 1]);
}
pii Get(){
return seg[1];
// 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;
for(int i = 0; i <= H; i++)
for(int j = 0; j <= W; j++)
Add(i, j, 1);
DS.Build(H*W);
}
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... |