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 <bits/stdc++.h>
#include "seats.h"
using namespace std;
struct Node{
int sum, minVal, cntMin;
Node(int x = 0, int cnt = 1){
sum = minVal = x;
cntMin = cnt;
}
Node operator + (const Node &oth){
Node res;
res.sum = sum + oth.sum;
res.minVal = min(minVal, sum + oth.minVal);
res.cntMin = cntMin * (minVal == res.minVal) + oth.cntMin * (sum + oth.minVal == res.minVal);
return res;
}
};
struct SegmentTree{
int n;
Node d[4000007];
void init(int _n){
n = _n;
}
void update(int id, int l, int r, int pos, int val){
if (pos < l || r < pos)
return;
if (l == r){
d[id].sum += val;
d[id].minVal += val;
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, pos, val);
update(id << 1 | 1, mid + 1, r, pos, val);
d[id] = d[id << 1] + d[id << 1 | 1];
}
void update(int pos, int val){
update(1, 0, n - 1, pos, val);
}
int getCnt(){
return d[1].cntMin;
}
} st;
int h, w, tmp[4], nTmp;
vector<vector<int>> chair;
vector<int> x, y;
bool inSide(int x, int y){
return (0 <= x && x < h && 0 <= y && y < w);
}
void UpdateSquare(int x, int y, int type){
nTmp = 0;
for(int dx = 0; dx < 2; dx++)
for(int dy = 0; dy < 2; dy++)
if (inSide(x + dx, y + dy))
tmp[nTmp++] = chair[x + dx][y + dy];
sort(tmp, tmp + nTmp);
for(int i = 0; i < nTmp; i++){
st.update(tmp[i], type);
type = -type;
}
}
void give_initial_chart(int hh, int ww, vector<int> r, vector<int> c) {
h = hh;
w = ww;
x = r;
y = c;
chair.assign(h, vector<int>(w, 0));
for(int i = 0; i < w * h; i++)
chair[r[i]][c[i]] = i;
st.init(h * w);
for(int i = -1; i < h; i++)
for(int j = -1; j < w; j++)
UpdateSquare(i, j, 1);
}
int swap_seats(int a, int b) {
for(int dx = -1; dx < 1; dx++)
for(int dy = -1; dy < 1; dy++){
UpdateSquare(x[a] + dx, y[a] + dy, -1);
UpdateSquare(x[b] + dx, y[b] + dy, -1);
}
swap(chair[x[a]][y[a]], chair[x[b]][y[b]]);
swap(x[a], x[b]);
swap(y[a], y[b]);
for(int dx = -1; dx < 1; dx++)
for(int dy = -1; dy < 1; dy++){
UpdateSquare(x[a] + dx, y[a] + dy, +1);
UpdateSquare(x[b] + dx, y[b] + dy, +1);
}
return st.getCnt();
}
# | 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... |