Language: C++

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
void read(int &x){
	char ch;bool ok;
	for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1;
	for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;
}
#define rg register
const int maxn=1e5+10;bool flag;
int n,m,x[maxn*4],y[maxn*4],z[maxn*4],mp[4][maxn];
int f[maxn*4],w[maxn*4],tot,ans,ans1,s[2],ss[2];
#define lowbit(i) (i&(-i))
void add(int x){for(rg int i=x;i<=m;i+=lowbit(i))f[i]++;}
int get(int x){int ans=0;for(rg int i=x;i;i-=lowbit(i))ans+=f[i];return ans;}
int main(){
	read(m),n=3;
	for(rg int i=1;i<=n;i++)
		for(rg int j=1;j<=m;j++)
		    x[i+3*j-3]=i&1,y[i+3*j-3]=j&1,z[i+3*j-3]=j;
	for(rg int i=1;i<=n;i++)for(rg int j=1;j<=m;j++)read(mp[i][j]);
	for(rg int i=1;i<=n;i++)
		for(rg int j=1;j<=m;j++){
		    int u=1+z[mp[i][j]]*3-3,v=2+z[mp[i][j]]*3-3,w=3+z[mp[i][j]]*3-3;
			bool ok1=0,ok2=0,ok3=0;
			if(mp[1][j]==u||mp[2][j]==u||mp[3][j]==u)ok1=1;
			if(mp[1][j]==v||mp[2][j]==v||mp[3][j]==v)ok2=1;
			if(mp[1][j]==w||mp[2][j]==w||mp[3][j]==w)ok3=1;
			if(!(ok1&ok2&ok3))flag=1;
			if(((i&1)^x[mp[i][j]])||((j&1)^y[mp[i][j]]))flag=1;
		}
    if(flag){printf("No\n");return 0;}
	for(rg int i=1;i<=m;i+=2)if(mp[1][i]!=1+3*z[mp[1][i]]-3)s[1]^=1;
	for(rg int i=2;i<=m;i+=2)if(mp[1][i]!=1+3*z[mp[1][i]]-3)s[0]^=1;
	for(rg int i=1;i<=m;i+=2)w[++tot]=z[mp[1][i]];
	for(rg int i=1;i<=tot;i++)ans+=i-get(w[i])-1,add(w[i]);
	for(rg int i=1;i<=m;i++)f[i]=0;tot=0;
	for(rg int i=2;i<=m;i+=2)w[++tot]=z[mp[1][i]];
	for(rg int i=1;i<=tot;i++)ans1+=i-get(w[i])-1,add(w[i]);
	s[1]^=(ans1&1),s[0]^=(ans&1);
	else puts(s[0]!=s[1]?"No":"Yes");
}


Compiler output:
a.cc: In function 'void add(int)':
a.cc:17:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   17 | void add(int x){for(rg int i=x;i<=m;i+=lowbit(i))f[i]++;}
      |                            ^
a.cc: In function 'int get(int)':
a.cc:18:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   18 | int get(int x){int ans=0;for(rg int i=x;i;i-=lowbit(i))ans+=f[i];return ans;}
      |                                     ^
a.cc: In function 'int main()':
a.cc:21:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   21 |         for(rg int i=1;i<=n;i++)
      |                    ^
a.cc:22:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   22 |                 for(rg int j=1;j<=m;j++)
      |                            ^
a.cc:24:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |         for(rg int i=1;i<=n;i++)for(rg int j=1;j<=m;j++)read(mp[i][j]);
      |                    ^
a.cc:24:44: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   24 |         for(rg int i=1;i<=n;i++)for(rg int j=1;j<=m;j++)read(mp[i][j]);
      |                                            ^
a.cc:25:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   25 |         for(rg int i=1;i<=n;i++)
      |                    ^
a.cc:26:28: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   26 |                 for(rg int j=1;j<=m;j++){
      |                            ^
a.cc:36:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   36 |         for(rg int i=1;i<=m;i+=2)if(mp[1][i]!=1+3*z[mp[1][i]]-3)s[1]^=1;
      |                    ^
a.cc:37:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   37 |         for(rg int i=2;i<=m;i+=2)if(mp[1][i]!=1+3*z[mp[1][i]]-3)s[0]^=1;
      |                    ^
a.cc:38:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   38 |         for(rg int i=1;i<=m;i+=2)w[++tot]=z[mp[1][i]];
      |                    ^
a.cc:39:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |         for(rg int i=1;i<=tot;i++)ans+=i-get(w[i])-1,add(w[i]);
      |                    ^
a.cc:40:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |         for(rg int i=1;i<=m;i++)f[i]=0;tot=0;
      |                    ^
a.cc:41:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   41 |         for(rg int i=2;i<=m;i+=2)w[++tot]=z[mp[1][i]];
      |                    ^
a.cc:42:20: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |         for(rg int i=1;i<=tot;i++)ans1+=i-get(w[i])-1,add(w[i]);
      |                    ^
a.cc:44:9: error: 'else' without a previous 'if'
   44 |         else puts(s[0]!=s[1]?"No":"Yes");
      |         ^~~~