This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/******************************
Author: jhnah917(Justice_Hui)
g++ -std=c++17 -DLOCAL
******************************/
#ifndef LOCAL
#include "seats.h"
#endif
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define IDX(v, x) (lower_bound(all(v), x) - v.begin())
using namespace std;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 0x3f3f3f3f;
struct Node{
int X1, X2, Y1, Y2;
Node() : Node(INF, -INF, INF, -INF) {}
Node(int X1, int X2, int Y1, int Y2) : X1(X1), Y1(Y1), X2(X2), Y2(Y2) {}
void set(int x, int y){ X1 = X2 = x; Y1 = Y2 = y; }
bool in(const Node &t){
return X1 <= t.X1 && t.X2 <= X2 && Y1 <= t.Y1 && t.Y2 <= Y2;
}
bool check(int sz){
return (X2-X1+1) * (Y2-Y1+1) == sz;
}
};
Node Merge(const Node &l, const Node &r){
return {min(l.X1, r.X1), max(l.X2, r.X2), min(l.Y1, r.Y1), max(l.Y2, r.Y2)};
}
int N, M, K;
vector<int> X, Y;
vector<vector<int>> V;
vector<Node> Prefix;
int ans_sub4;
void give_initial_chart(int H, int W, vector<int> _R, vector<int> _C){
N = H; M = W; K = N * M;
swap(X, _R); swap(Y, _C);
V = vector<vector<int>>(N, vector<int>(M));
for(int i=0; i<N*M; i++) V[X[i]][Y[i]] = i;
Prefix.resize(N*M);
for(int i=0; i<N*M; i++) Prefix[i].set(X[i], Y[i]);
for(int i=1; i<N*M; i++) Prefix[i] = Merge(Prefix[i-1], Prefix[i]);
for(int i=0; i<N*M; i++) if(Prefix[i].check(i+1)) ans_sub4++;
}
// Subtask 1+2+4 = 17pts
int swap_seats(int a, int b){
if(a > b) swap(a, b);
for(int i=a; i<b; i++) if(Prefix[i].check(i+1)) ans_sub4--;
swap(V[X[a]][Y[a]], V[X[b]][Y[b]]);
swap(X[a], X[b]); swap(Y[a], Y[b]);
for(int i=a; i<b; i++){
Prefix[i].set(X[i], Y[i]);
if(i) Prefix[i] = Merge(Prefix[i-1], Prefix[i]);
if(Prefix[i].check(i+1)) ans_sub4++;
}
return ans_sub4;
}
#ifdef LOCAL
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int H, W, Q;
cin >> H >> W >> Q;
vector<int> R(H*W), C(H*W);
for(int i=0; i<H*W; i++) cin >> R[i] >> C[i];
vector<int> A(Q), B(Q);
for(int i=0; i<Q; i++) cin >> A[i] >> B[i];
give_initial_chart(H, W, R, C);
for(int i=0; i<Q; i++){
cout << swap_seats(A[i], B[i]) << "\n";
}
return 0;
}
#endif
/*
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 constructor 'Node::Node(int, int, int, int)':
seats.cpp:23:17: warning: 'Node::Y1' will be initialized after [-Wreorder]
23 | int X1, X2, Y1, Y2;
| ^~
seats.cpp:23:13: warning: 'int Node::X2' [-Wreorder]
23 | int X1, X2, Y1, Y2;
| ^~
seats.cpp:25:5: warning: when initialized here [-Wreorder]
25 | Node(int X1, int X2, int Y1, int Y2) : X1(X1), Y1(Y1), X2(X2), Y2(Y2) {}
| ^~~~
# | 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... |