Submission #1222154

#TimeUsernameProblemLanguageResultExecution timeMemory
1222154pasin30055Navigation 2 (JOI21_navigation2)C++20
100 / 100
286 ms876 KiB
#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
        }
      } 
    }
  }
}
#include "Bruno.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 next ____next
#define prev ____prev
#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 {

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

int Go(int sr, int sc, int tr, int tc) {
  if (tr < sr) {
    return 3;
  } else if (tr > sr) {
    return 2;
  } else if (tc > sc) {
    return 0;
  } else if (tc < sc) {
    return 1;
  }
  return 4;
}
std::vector<int> Bruno(int K, std::vector<int> value) {
  vector<int> res(K);
  vector<VI> flags(3, VI(3));
  int nxt = 0;
  REP (i, 3) {
    REP (j, 3) {
      flags[i][j] = value[nxt++];
    }
  }
  int fr, fc;
  int r3, c3;
  VPII nons;
  REP (i, 3) {
    REP (j, 3) {
      if (flags[i][j] == 12) {
        nons.PB({i, j});
      }
    }
  }
  assert(SZ(nons) == 2);
  if (GetAnchor(nons[0], nons[1]) != nons[0]) {
    swap(nons[0], nons[1]);
  }
  VPII wh;
  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;
      if (ni == nons[1].st && nj == nons[1].nd) { continue; }
      wh.PB({ni, nj});
    }
  }
  debug(wh);
  //vector<PII> heh{{0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}};
  REP (goal, K) {
    int r = wh[goal].st;
    int c = wh[goal].nd;
    int val = flags[r][c];
    if (val == 8) { res[goal] = 2; continue; }
    if (val == 9) { res[goal] = 3; continue; }
    if (val == 10) { res[goal] = 0; continue; }
    if (val == 11) { res[goal] = 1; continue; }
    //int tr = -1, tc = -1;
    int nxt = 1;
    FOR (dr, -1, 1) {
      FOR (dc, -1, 1) {
        int nr = (r + dr + 3) % 3;
        int nc = (c + dc + 3) % 3;
        int fail = 0;
        REP (tr, 2) {
          if (nr == nons[tr].st && nc == nons[tr].nd) { fail = 1; }
        }
        if (fail) { continue; }
        if (val == nxt) {
          res[goal] = Go(1, 1, r + dr, c + dc);
//           res[goal] = 4;
//           if (dr == 1) { res[goal] = 2; }
//           if (dr == -1) { res[goal] = 3; }
//           if (dc == 1) { res[goal] = 0; }
//           if (dc == -1) { res[goal] = 1; }
        }
        nxt++;
      }
    }
  }
  debug(value);
  debug(res);
  return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...