#include "Anna.h"
#include <vector>
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
namespace {
int FunctionExample(int r, int c, int K) {
return (r + c) % K + 1;
}
PII GetAnchor(PII A, PII B) {
VPII vecs{{0, 1}, {1, -1}, {1, 0}, {1, 1}};
for (auto v : vecs) {
if ((A.st + v.st - B.st) % 3 == 0 && (A.nd + v.nd - B.nd) % 3 == 0) {
return A;
}
}
return B;
}
} // namespace
void Anna(int N, int K, std::vector<int> R, std::vector<int> C) {
vector<vector<int>> rems(3, VI(3));
REP (i, K) {
rems[R[i] % 3][C[i] % 3] = 1;
}
VPII nons;
REP (i, 3) {
REP (j, 3) {
if (rems[i][j] == 0) {
nons.PB({i, j});
}
}
}
assert(SZ(nons) >= 2);
nons.resize(2);
if (GetAnchor(nons[0], nons[1]) != nons[0]) {
swap(nons[0], nons[1]);
}
vector<VI> who(3, VI(3, -1));
int nxt = 0;
REP (i, 3) {
REP (j, 3) {
if (i == 0 && j == 0) { continue; }
int ni = (nons[0].st + i) % 3;
int nj = (nons[0].nd + j) % 3;
PII p{ni, nj};
if (p == nons[1]) { continue; }
who[ni][nj] = nxt++;
}
}
debug(who);
for (int r = 0; r < N; r++) {
for (int c = 0; c < N; c++) {
if (r % 3 == nons[0].st && c % 3 == nons[0].nd) {
SetFlag(r, c, 12);
} else if (r % 3 == nons[1].st && c % 3 == nons[1].nd) {
SetFlag(r, c, 12);
// } else if (r % 3 == 0 && c % 3 == 1) {
// int val = 8;
// for (int i = 0; i < K; i++) {
// if (R[i] == r && C[i] == c) {
// val = i + 1;
// }
// }
// SetFlag(r, c, val);
// //SetFlag(r, c, 1);
} else {
int goal = who[r % 3][c % 3];
int dr = R[goal] - r;
int dc = C[goal] - c;
if (dr >= 2) {
SetFlag(r, c, 8);
} else if (dr <= -2) {
SetFlag(r, c, 9);
} else if (dc >= 2) {
SetFlag(r, c, 10);
} else if (dc <= -2) {
SetFlag(r, c, 11);
} else {
nxt = 1;
FOR (di, -1, 1) {
FOR (dj, -1, 1) {
int nr = (r + di + 3) % 3;
int nc = (c + dj + 3) % 3;
int fail = 0;
REP (tr, 2) {
if (nr == nons[tr].st && nc == nons[tr].nd) {
fail = 1;
}
}
if (fail) {
assert(di != dr || dj != dc);
continue;
}
if (di == dr && dj == dc) {
SetFlag(r, c, nxt);
} else {
nxt++;
}
}
}
//SetFlag(r, c, 1 + (dr + 1) + 3 * (dc + 1); // TODO
}
}
}
}
}
I2luY2x1ZGUgIkFubmEuaCIKI2luY2x1ZGUgPHZlY3Rvcj4KCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIE1QIG1ha2VfcGFpcgojZGVmaW5lIFBCIHB1c2hfYmFjawojZGVmaW5lIHN0IGZpcnN0CiNkZWZpbmUgbmQgc2Vjb25kCiNkZWZpbmUgcmQgdGhpcmQKI2RlZmluZSBGT1IoaSwgYSwgYikgZm9yKGludCBpID0oYSk7IGkgPD0oYik7ICsraSkKI2RlZmluZSBSRShpLCBuKSBGT1IoaSwgMSwgbikKI2RlZmluZSBGT1JEKGksIGEsIGIpIGZvcihpbnQgaSA9IChhKTsgaSA+PSAoYik7IC0taSkKI2RlZmluZSBSRVAoaSwgbikgZm9yKGludCBpID0gMDtpIDwobik7ICsraSkKI2RlZmluZSBWQVIodiwgaSkgX190eXBlb2YoaSkgdj0oaSkK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T0gMCAmJiBqID09IDApIHsgY29udGludWU7IH0KICAgICAgaW50IG5pID0gKG5vbnNbMF0uc3QgKyBpKSAlIDM7CiAgICAgIGludCBuaiA9IChub25zWzBdLm5kICsgaikgJSAzOwogICAgICBQSUkgcHtuaSwgbmp9OwogICAgICBpZiAocCA9PSBub25zWzFdKSB7IGNvbnRpbnVlOyB9CiAgICAgIHdob1tuaV1bbmpdID0gbnh0Kys7CiAgICB9CiAgfQogIGRlYnVnKHdobyk7CiAgZm9yIChpbnQgciA9IDA7IHIgPCBOOyByKyspIHsKICAgIGZvciAoaW50IGMgPSAwOyBjIDwgTjsgYysrKSB7CiAgICAgIGlmIChyICUgMyA9PSBub25zWzBdLnN0ICYmIGMgJSAzID09IG5vbnNbMF0ubmQpIHsKICAgICAgICBTZXRGbGFnKHIsIGMsIDEyKTsKICAgICAgfSBlbHNlIGlmIChyICUgMyA9PSBub25zWzFdLnN0ICYmIGMgJSAzID09IG5vbnNbMV0ubmQpIHsKICAgICAgICBTZXRGbGFnKHIsIGMsIDEyKTsKLy8gICAgICAgfSBlbHNlIGlmIChyICUgMyA9PSAwICYmIGMgJSAzID09IDEpIHsKLy8gICAgICAgICBpbnQgdmFsID0gODsKLy8gICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IEs7IGkrKykgewovLyAgICAgICAgICAgaWYgKFJbaV0gPT0gciAmJiBDW2ldID09IGMpIHsKLy8gICAgICAgICAgICAgdmFsID0gaSArIDE7Ci8vICAgICAgICAgICB9Ci8vICAgICAgICAgfQovLyAgICAgICAgIFNldEZsYWcociwgYywgdmFsKTsKLy8gICAgICAgICAvL1NldEZsYWcociwgYywgMSk7CiAgICAgIH0gZWxzZSB7CiAgICAgICAgaW50IGdvYWwgPSB3aG9bciAlIDNdW2MgJSAzXTsKICAgICAgICBpbnQgZHIgPSBSW2dvYWxdIC0gcjsKICAgICAgICBpbnQgZGMgPSBDW2dvYWxdIC0gYzsKICAgICAgICBpZiAoZHIgPj0gMikgewogICAgICAgICAgU2V0RmxhZyhyLCBjLCA4KTsKICAgICAgICB9IGVsc2UgaWYgKGRyIDw9IC0yKSB7CiAgICAgICAgICBTZXRGbGFnKHIsIGMsIDkpOwogICAgICAgIH0gZWxzZSBpZiAoZGMgPj0gMikgewogICAgICAgICAgU2V0RmxhZyhyLCBjLCAxMCk7CiAgICAgICAgfSBlbHNlIGlmIChkYyA8PSAtMikgewogICAgICAgICAgU2V0RmxhZyhyLCBjLCAxMSk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgIG54dCA9IDE7CiAgICAgICAgICBGT1IgKGRpLCAtMSwgMSkgewogICAgICAgICAgICBGT1IgKGRqLCAtMSwgMSkgewogICAgICAgICAgICAgIGludCBuciA9IChyICsgZGkgKyAzKSAlIDM7CiAgICAgICAgICAgICAgaW50IG5jID0gKGMgKyBkaiArIDMpICUgMzsKICAgICAgICAgICAgICBpbnQgZmFpbCA9IDA7CiAgICAgICAgICAgICAgUkVQICh0ciwgMikgewogICAgICAgICAgICAgICAgaWYgKG5yID09IG5vbnNbdHJdLnN0ICYmIG5jID09IG5vbnNbdHJdLm5kKSB7CiAgICAgICAgICAgICAgICAgIGZhaWwgPSAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAoZmFpbCkgewogICAgICAgICAgICAgICAgYXNzZXJ0KGRpICE9IGRyIHx8IGRqICE9IGRjKTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgICBpZiAoZGkgPT0gZHIgJiYgZGogPT0gZGMpIHsKICAgICAgICAgICAgICAgIFNldEZsYWcociwgYywgbnh0KTsKICAgICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbnh0Kys7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgICAvL1NldEZsYWcociwgYywgMSArIChkciArIDEpICsgMyAqIChkYyArIDEpOyAvLyBUT0RPCiAgICAgICAgfQogICAgICB9IAogICAgfQogIH0KfQo=