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;
#define all(x) (x).begin() , (x).end()
const int MAXN = 1e6 + 10;
int n , h , w , ans , A[2][MAXN] , first[2][MAXN] , valid[MAXN] , mn[2][MAXN] , mx[2][MAXN] , MN[2] , MX[2];
vector<int> vec[2][MAXN];
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
h = H; w = W; n = h * w;
for(int i = 0 ; i < n ; i++){
A[0][i] = mn[0][i] = mx[0][i] = R[i];
A[1][i] = mn[1][i] = mx[1][i] = C[i];
vec[0][A[0][i]].push_back(i);
vec[1][A[1][i]].push_back(i);
}
for(int i = 0 ; i < 2 ; i++){
for(int j = 1 ; j < n ; j++){
mn[i][j] = min(mn[i][j] , mn[i][j - 1]);
mx[i][j] = max(mx[i][j] , mx[i][j - 1]);
}
}
for(int i = 0 ; i < n ; i++){
int x = (mx[0][i] - mn[0][i] + 1) , y = (mx[1][i] - mn[1][i] + 1);
if(x * y == i + 1){
valid[i] = 1;
ans++;
}
}
for(int i = 0 ; i < h ; i++) first[0][i] = *min_element(all(vec[0][i]));
for(int i = 0 ; i < w ; i++) first[1][i] = *min_element(all(vec[1][i]));
}
void change(int i , int x , int a , int b){
for(int j = 0 ; j < vec[i][x].size() ; j++){
if(vec[i][x][j] == a) vec[i][x][j] = b;
}
first[i][x] = *min_element(all(vec[i][x]));
}
int swap_seats(int a, int b) {
if(a > b) swap(a , b);
if(h > 1000 || w > 1000){
for(int i = 0 ; i < 2 ; i++){
swap(A[i][a] , A[i][b]);
mn[i][0] = mx[i][0] = A[i][0];
for(int j = max(a , 1) ; j < b ; j++){
mn[i][j] = min(A[i][j] , mn[i][j - 1]);
mx[i][j] = max(A[i][j] , mx[i][j - 1]);
}
}
for(int i = a ; i < b ; i++){
ans -= valid[i]; valid[i] = 0;
int x = (mx[0][i] - mn[0][i] + 1) , y = (mx[1][i] - mn[1][i] + 1);
if(x * y == i + 1){
valid[i] = 1;
ans++;
}
}
return ans;
}
int ans = 0;
for(int i = 0 ; i < 2 ; i++){
if(A[i][a] == A[i][b]) continue;
change(i , A[i][a] , a , b);
change(i , A[i][b] , b , a);
swap(A[i][a] , A[i][b]);
}
vector<int> vec;
for(int i = 0 ; i < h ; i++) vec.push_back(first[0][i]);
for(int i = 0 ; i < w ; i++) vec.push_back(first[1][i]);
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
vec.push_back(n);
MN[0] = MN[1] = MAXN;
MX[0] = MX[1] = -1;
for(int i = 0 ; i + 1 < vec.size() ; i++){
int pos = vec[i];
MN[0] = min(MN[0] , A[0][pos]); MX[0] = max(MX[0] , A[0][pos]);
MN[1] = min(MN[1] , A[1][pos]); MX[1] = max(MX[1] , A[1][pos]);
int x = (MX[0] - MN[0] + 1) , y = (MX[1] - MN[1] + 1);
if(vec[i] < x * y && x * y <= vec[i + 1]) ans++;
}
return ans;
}
Compilation message (stderr)
seats.cpp: In function 'void change(int, int, int, int)':
seats.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int j = 0 ; j < vec[i][x].size() ; j++){
| ~~^~~~~~~~~~~~~~~~~~
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:80:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for(int i = 0 ; i + 1 < 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... |