/*
_____________________.
|____________________| $$\ $$\ $$\ $$\
|____________________| $$ | \__| $$ | $$ |
|____________________| $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$\ $$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$$\
|____________________| \_$$ _| $$ __$$\ \____$$\ $$ __$$\ $$ _____| $$ __$$\ $$ |$$ __$$\ $$ __$$\\_$$ _| $$ _____|
|____________________| $$ | $$ | \__|$$$$$$$ |$$ | $$ |\$$$$$$\ $$ | \__|$$ |$$ / $$ |$$ | $$ | $$ | \$$$$$$\
$$ |$$\ $$ | $$ __$$ |$$ | $$ | \____$$\ $$ | $$ |$$ | $$ |$$ | $$ | $$ |$$\ \____$$\
\$$$$ |$$ | \$$$$$$$ |$$ | $$ |$$$$$$$ | $$ | $$ |\$$$$$$$ |$$ | $$ | \$$$$ |$$$$$$$ |
へ ♡ \____/ \__| \_______|\__| \__|\_______/ \__| \__| \____$$ |\__| \__| \____/ \_______/
૮ - ՛) $$\ $$ |
/ ⁻ ៸| \$$$$$$ | are human rights :3
乀 (ˍ,ل ل \______/
*/
//#include "candies_ioi.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<pair<T, long long>, null_type,
less<pair<T, long long>>, rb_tree_tag,
tree_order_statistics_node_update>;
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define ull unsigned long long
#define ld long double
#define inf (ll)2e12+4
#define pb push_back
#define se second
#define fi first
#define endl '\n'
#define mp make_pair
#define pll pair<ll,ll>
#define kth_smallest find_by_order
#define num_of_smaller order_of_key
#define fori(x) for(ll i=0;i<x;i++)
#define forj(y) for(ll j=0;j<y;j++)
#define fork(z) for(ll k=0;k<z;k++)
#define DEBUG
#ifdef DEBUG
#define show(x) cerr<<#x<<" is "<<x<<endl;
#define show2(x,y) cerr<<#x<<" is "<<x<<" "<<#y<<" is "<<y<<endl;
#define show3(x,y,z) cerr<<#x<<" is "<<x<<" "<<#y<<" is "<<y<<" "<<#z<<" is "<<z<<endl;
#define show4(x,y,z,a) cerr<<#x<<" is "<<x<<" "<<#y<<" is "<<y<<" "<<#z<<" is "<<z<<" "<<#a<<" is "<<a<<endl;
#define show_vec(a) for(auto &i:a)cerr<<i<<" ";cerr<<endl;
#define skillissue cerr<<"your code is running\n";
#define getchar_unlocked _getchar_nolock // comment before submission
#else
#define show(x)
#define show2(x,y)
#define show3(x,y,z)
#define show4(x,y,z,a)
#define show_vec(a)
#define skillissue
#endif
#include "seats.h"
std::vector<int> r;
struct node{
ll S,E,M,val;
node *l,*r;
node(ll s,ll e){
S=s;E=e;val=0;
M=(S+E)>>1;
if(S==E)return;
l=new node(S,M);
r=new node(M+1,E);
}
void up(ll p,ll v){
if(S==E){
val=v;
return;
}
if(p<=M)l->up(p,v);
else r->up(p,v);
val=l->val+r->val;
}
ll qry(ll x,ll y){
if(x>E or y<S)return 0;
if(x<=S and E<=y){
return val;
}
return l->qry(x,y)+r->qry(x,y);
}
};
const ll maxn=(ll)1e1+5;
node *three=new node(0,maxn), *one=new node(0,maxn);
ll n;
ll ans;
ll posx[maxn];
ll posy[maxn];
vector<vector<ll>>grid;
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
fori(H+2){
grid.pb({});
forj(W+2)grid[i].pb(inf);
}
n=(H*W);
fori(n){
posx[i]=R[i];
}
fori(n){
posy[i]=C[i];
}
fori(H*W){
grid[R[i]+1][C[i]+1]=i;
}
for(ll i=1;i<=H;i++)for(ll j=1;j<=W;j++){
ll tmp=0,tmp1=0;
for(ll x=i-1;x<=i;x++)for(ll y=j-1;y<=j;y++){
// this is topleft
ll prev=0;
for(ll dx=0;dx<=1;dx++){
for(ll dy=0;dy<=1;dy++){
if(grid[x+dx][y+dy]<grid[i][j]){
prev++;
}
}
}
//show3(x,y,prev)
if(prev==2){
tmp1++;
}else if(prev==0){
tmp++;
}
else if(prev==1){
tmp--;
}
else{
tmp1--;
}
}
//show4(i,j,tmp,tmp1);
one->up(grid[i][j],tmp);
three->up(grid[i][j],tmp1);
}
ans=0;
fori(n){
ans+=one->qry(0,i)==4 and three->qry(0,i)==0;
}
//fori(n)cerr<<one->qry(0,i)<<" ";
//cerr<<endl;
//fori(maxn)cerr<<one->minqry(i,i)<<" ";
//cerr<<endl;
//fori(maxn)cerr<<three->minqry(i,i)<<" ";
//cerr<<endl;
/*
fori(H*W){
x->up(i,posx[i]);
y->up(i,posy[i]);
}
n=H*W;
fori(n){
//show2(i+1,(y->maxqry(0,i)-y->minqry(0,i)+1)*(1+x->maxqry(0,i)-x->minqry(0,i)))
ans+=(y->maxqry(0,i)-y->minqry(0,i)+1)*(1+x->maxqry(0,i)-x->minqry(0,i)) == i+1;
}
//show(ans)
*/
}
int swap_seats(int a, int b) {
swap(posx[a],posx[b]);
swap(posy[a],posy[b]);
swap(grid[posx[b]][posy[b]],grid[posx[a]][posy[a]]);
ll i=posx[a]+1,j=posy[a]+1;
ans-=one->qry(0,a)==4 and three->qry(0,a)==0;
ans-=one->qry(0,b)==4 and three->qry(0,b)==0;
ll tmp=0,tmp1=0;
for(ll x=i-1;x<=i;x++)for(ll y=j-1;y<=j;y++){
// this is topleft
ll prev=0;
for(ll dx=0;dx<=1;dx++){
for(ll dy=0;dy<=1;dy++){
if(grid[x+dx][y+dy]<grid[i][j]){
prev++;
}
}
}
//show3(x,y,prev)
if(prev==2){
tmp1++;
}else if(prev==0){
tmp++;
}
else if(prev==1){
tmp--;
}
else{
tmp1--;
}
}
//show4(i,j,tmp,tmp1);
one->up(grid[i][j],tmp);
three->up(grid[i][j],tmp1);
i=posx[b]+1,j=posy[b]+1;
tmp=0,tmp1=0;
for(ll x=i-1;x<=i;x++)for(ll y=j-1;y<=j;y++){
// this is topleft
ll prev=0;
for(ll dx=0;dx<=1;dx++){
for(ll dy=0;dy<=1;dy++){
if(grid[x+dx][y+dy]<grid[i][j]){
prev++;
}
}
}
//show3(x,y,prev)
if(prev==2){
tmp1++;
}else if(prev==0){
tmp++;
}
else if(prev==1){
tmp--;
}
else{
tmp1--;
}
}
//show4(i,j,tmp,tmp1);
one->up(grid[i][j],tmp);
three->up(grid[i][j],tmp1);
//fori(n)cerr<<one->qry(0,i)<<" ";
//cerr<<endl;
ans+=one->qry(0,a)==4 and three->qry(0,a)==0;
ans+=one->qry(0,b)==4 and three->qry(0,b)==0;
return ans;
}