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>
using namespace std;
std::vector<int> r;
const int SZ = 1000100;
pair<int, int> t[SZ*4];
int ob[SZ*4];
vector <vector<int> > arr;
void build(int v, int l, int r){
if (l == r){
t[v].second = 1;
return;
}
int mid = (l+r)>>1;
build(v+v, l, mid);
build(v+v+1, mid+1, r);
t[v].second = t[v+v].second + t[v+v+1].second;
}
inline void push(int v){
if (ob[v] == 0)
return;
t[v+v].first += ob[v];
t[v+v+1].first += ob[v];
ob[v+v] += ob[v];
ob[v+v+1] += ob[v];
ob[v] = 0;
}
inline void update(int v, int l, int r, int tl, int tr, int x){
if (l > r || tl > r || l > tr || tl > tr)
return;
if (tl <= l && r <= tr){
t[v].first += x;
ob[v] += x;
return;
}
int mid = (l+r)>>1;
push(v);
update(v+v, l, mid, tl, tr, x);
update(v+v+1, mid+1, r, tl, tr, x);
if (t[v+v].first < t[v+v+1].first){
t[v] = t[v+v];
} else
if (t[v+v].first > t[v+v+1].first){
t[v] = t[v+v+1];
} else {
t[v] = {t[v+v].first, t[v+v].second + t[v+v+1].second};
}
}
int inf;
vector <int> R, C;
inline void upd(int x, int y, int add){
update(1, 1, inf, arr[x][y] + 1, min({arr[x+1][y], arr[x][y+1], arr[x+1][y+1]}), add);
update(1, 1, inf, arr[x+1][y] + 1, min({arr[x][y], arr[x][y+1], arr[x+1][y+1]}), add);
update(1, 1, inf, arr[x][y+1] + 1, min({arr[x][y], arr[x+1][y], arr[x+1][y+1]}), add);
update(1, 1, inf, arr[x+1][y+1] + 1, min({arr[x][y], arr[x+1][y], arr[x][y+1]}), add);
update(1, 1, inf, max({arr[x+1][y], arr[x][y+1], arr[x+1][y+1]}) + 1, arr[x][y], add);
update(1, 1, inf, max({arr[x][y], arr[x][y+1], arr[x+1][y+1]}) + 1, arr[x+1][y], add);
update(1, 1, inf, max({arr[x][y], arr[x+1][y], arr[x+1][y+1]}) + 1, arr[x][y+1], add);
update(1, 1, inf, max({arr[x][y], arr[x+1][y], arr[x][y+1]}) + 1, arr[x+1][y+1], add);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
::R = R;
::C = C;
inf = H*W;
arr.resize(H+2, vector<int>(W+2, inf));
for (int i = 0; i < H*W; i++){
arr[R[i]+1][C[i]+1] = i;
}
build(1, 1, inf);
for (int i = 0; i + 1 < arr.size(); i++){
for (int j = 0; j + 1 < arr[i].size(); j++){
upd(i, j, 1);
}
}
}
int swap_seats(int a, int b) {
int x1 = R[a]+1, y1 = C[a]+1, x2 = R[b]+1, y2 = C[b]+1;
swap(R[a], R[b]);
swap(C[a], C[b]);
vector <pair<int, int> > vec;
vec.push_back({x1-1, y1-1});
vec.push_back({x1-1, y1});
vec.push_back({x1, y1-1});
vec.push_back({x1, y1});
vec.push_back({x2-1, y2-1});
vec.push_back({x2-1, y2});
vec.push_back({x2, y2-1});
vec.push_back({x2, y2});
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < vec.size(); i++){
int x = vec[i].first, y = vec[i].second;
upd(x, y, -1);
}
swap(arr[x1][y1], arr[x2][y2]);
for (int i = 0; i < vec.size(); i++){
int x = vec[i].first, y = vec[i].second;
upd(x, y, 1);
}
return t[1].second;
}
/**
0 3 4
1 2 5
2 3 2
0 0
1 0
1 1
0 1
0 2
1 2
0 5
0 5
*/
Compilation message (stderr)
seats.cpp: In function 'void give_initial_chart(int, int, std::vector<int>, std::vector<int>)':
seats.cpp:83:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < arr.size(); i++){
~~~~~~^~~~~~~~~~~~
seats.cpp:84:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j + 1 < arr[i].size(); j++){
~~~~~~^~~~~~~~~~~~~~~
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:107:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++){
~~^~~~~~~~~~~~
seats.cpp:112:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i++){
~~^~~~~~~~~~~~
# | 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... |