<p>You are given two integers <code>m</code> and <code>n</code>, which represent the dimensions of a matrix.</p>

<p>You are also given the <code>head</code> of a linked list of integers.</p>

<p>Generate an <code>m x n</code> matrix that contains the integers in the linked list presented in <strong>spiral</strong> order <strong>(clockwise)</strong>, starting from the <strong>top-left</strong> of the matrix. If there are remaining empty spaces, fill them with <code>-1</code>.</p>

<p>Return <em>the generated matrix</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg" style="width: 240px; height: 150px;" />
<pre>
<strong>Input:</strong> m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
<strong>Output:</strong> [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
<strong>Explanation:</strong> The diagram above shows how the values are printed in the matrix.
Note that the remaining spaces in the matrix are filled with -1.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg" style="width: 221px; height: 60px;" />
<pre>
<strong>Input:</strong> m = 1, n = 4, head = [0,1,2]
<strong>Output:</strong> [[0,1,2,-1]]
<strong>Explanation:</strong> The diagram above shows how the values are printed from left to right in the matrix.
The last space in the matrix is set to -1.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li>The number of nodes in the list is in the range <code>[1, m * n]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
union FastPoint {
    struct {int x, y;} p;
    uint64_t fast;

    enum Dir : uint64_t {
        E  = 0x0000000000000001LLU, //++x
        S  = 0x0000000100000000LLU, //++y
        W  = 0xffffffffffffffffLLU, //--x
        N  = 0xffffffff00000000LLU, //--y
        SE = 0x0000000100000001LLU, //++x, ++y
        NW = 0xfffffffeffffffffLLU, //--x, --y
    };

    FastPoint() : fast(0) {}
    FastPoint(int x_, int y_) : p({x_, y_}) {}
    void Print(void) const {std::cout << '(' << p.x << ',' << p.y << ')' << std::endl;}
};

class Solution {

    struct SpiralPos {
        FastPoint pos, min, max; //from tl
        int dir, rem;
        SpiralPos(int m, int n) : pos(), min(0, 1), max(n-1, m-1), dir(n>1 ? 0 : 1), rem(m*n) {}

        bool End(void) const {
            return rem==0;
        }

        inline SpiralPos& operator++(void) {
            static constexpr uint64_t shift[] = {
                FastPoint::Dir::E,
                FastPoint::Dir::S,
                FastPoint::Dir::W,
                FastPoint::Dir::N
            };
            pos.fast += shift[dir];
            //pos.Print();
                    
            switch(dir) {
                case 0: //right
                    if(pos.p.x == max.p.x) ++dir;
                    break;
                case 1: //down
                    if(pos.p.y == max.p.y) ++dir;
                    break;
                case 2: //left
                    if(pos.p.x == min.p.x) ++dir;
                    break;
                case 3: //up
                    if(pos.p.y == min.p.y) {
                        dir = 0;
                        //curl to next
                        min.fast += FastPoint::Dir::SE;
                        max.fast += FastPoint::Dir::NW;
                    }
                    break;
            }
            --rem;
            return *this;
        }
    };

public:
    vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        std::vector<std::vector<int>> ret;
        ret.resize(m);
        for(std::vector<int>& v : ret) v.resize(n, -1);

        SpiralPos p(m, n);
        for(; !p.End(); ++p){            
            if(head == nullptr) break;
            ret[p.pos.p.y][p.pos.p.x] = head->val;
            head = head->next;
        }
        return ret;
    }
};

static const int acceleration_hack = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    return 0;
}();
129800