1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <vector>
#include<cstdint>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <tuple>

using namespace std;

uint64_t power(uint64_t a, uint32_t b)
{// calculate a^b mod p
    uint64_t wynik =1;
    while (b>0)
    {
        if(b%2)
            wynik = (wynik*a);
        b = b>>1; //b=b/2
        a = (a*a);
    }
    return wynik;
}

const int32_t T=840; // okres po ktorym sygnalizacja wraca co stanu z zerowej sekundy
const int8_t K =18; // k=0,1,...K-1, max 2^(K-2) krokow indeksowanych w tabeli




vector<bool> przejsciaPoziomo(const vector<vector<string>>& sygnalizacja, int16_t t)
{
    int16_t n=sygnalizacja.size(), m=sygnalizacja[0].size();
    vector<bool> przejscia(m+1,false);
    przejscia[0]=true;
    for (int16_t x=0; x<m; x++)
    {
        for (int16_t y=0; y<n; y++)
            if (sygnalizacja[y][x][t %sygnalizacja[y][x].size()]=='1')
            {
                przejscia[x+1] = true;
                break;
            }
    }
    return przejscia;
}

vector<bool> przejsciaPionowo(const vector<vector<string>>& sygnalizacja, int16_t t)
{
    int16_t n=sygnalizacja.size(), m=sygnalizacja[0].size();
    vector<bool> przejscia(n+1,false);
    przejscia[0]=true;
    for (int16_t y=0; y<n; y++)
    {
        for (int16_t x=0; x<m; x++)
            if (sygnalizacja[y][x][t %sygnalizacja[y][x].size()]=='0')
            {
                przejscia[y+1] = true;
                break;
            }
    }
    return przejscia;
}

vector<int16_t> dokadDojdeWPrzod(vector<bool> przejscia)
{
    vector<int16_t> wPrzod_1d(przejscia.size());
    int16_t n = przejscia.size()-1;
    int16_t last = n;
    for(int16_t ii=n; ii>=0; ii--)
    {
        if(ii==n || przejscia[ii+1]==1)
            wPrzod_1d[ii] = last;
        else
        {
            wPrzod_1d[ii] = ii;
            last=ii;
        }
    }
    return wPrzod_1d;
}

vector<int16_t> dokadDojdeWTyl(vector<bool> przejscia)
{
    vector<int16_t> wTyl_1d(przejscia.size());
    int16_t n = przejscia.size()-1;
    int16_t last = 0;
    for(int16_t ii=0; ii<=n; ii++)
    {
        if(ii==0 || przejscia[ii]==1)
            wTyl_1d[ii] = last;
        else
        {
            wTyl_1d[ii] = ii;
            last=ii;
        }
    }
    return wTyl_1d;
}


void dokadDojde(vector<vector<vector<int16_t>>>& zakresPrzod, // K x T x [n+1 lub m+1]
                vector<vector<vector<int16_t>>>& zakresTyl,
                const vector<vector<string>>& sygnalizacja,
                bool czyPoziomo) // w przod - w kierunku rosnacych indeksow
{
    for(int8_t k=0; k<K; k++)
    {
        zakresPrzod.push_back(vector<vector<int16_t>>(T)); // K x T x 0
        zakresTyl.push_back(vector<vector<int16_t>>(T));
    }

    // wypelnienie zasiegow dla k=0
    for(int16_t t=0; t<T; t++)
    {
        vector<bool> przejscia;
        if(czyPoziomo)
            przejscia = przejsciaPoziomo(sygnalizacja, t);
        else
            przejscia = przejsciaPionowo(sygnalizacja, t);
        zakresPrzod[0][t] = dokadDojdeWPrzod(przejscia);
        zakresTyl[0][t] = dokadDojdeWTyl(przejscia);
    }

    // wypelnienie zasiegow dla k>0
    for(int8_t k=1; k<K; k++)
    {
        for(int16_t t0=0; t0<T; t0++)
        {
            zakresPrzod[k][t0] = vector<int16_t>(zakresPrzod[0][t0].size());
            zakresTyl[k][t0] = vector<int16_t>(zakresTyl[0][t0].size());

            for(int16_t x=0; (size_t)x<zakresPrzod[0][0].size(); x++ )
            {
                if(k==1) // zakres za 1 ture
                {
                    zakresPrzod[1][t0][x] = zakresPrzod[0] [(t0+1)%T] [zakresPrzod[0][t0][x]];
                    zakresTyl[1][t0][x]   =   zakresTyl[0] [(t0+1)%T] [zakresTyl[0][t0][x]];
                }
                else // k>1 - zakres za 2^(k-1) tur
                {
                    int32_t new_t = (t0 + power(2, k-2))%T;
                    zakresPrzod[k][t0][x] = zakresPrzod[k-1] [new_t] [zakresPrzod[k-1][t0][x]];
                    zakresTyl[k][t0][x]   =   zakresTyl[k-1] [new_t] [zakresTyl[k-1][t0][x]];
                }
            }
        }
    }


}

template <typename t>
t odl(t a, t b) // odleglosc miedzy a i b
{
    return abs(a-b);
}



int32_t czasPrzejscia(const vector<vector<vector<int16_t>>>& zakres,
                      int32_t t0, int16_t x0, int16_t xEnd)
// t0 = t0 %T !!!, x jest x-em lub y-em w zaleznosci od kierunku
{
    if (odl(x0, zakres[0][t0][x0]) >= odl(x0, xEnd))
        return 0; // dojscie natychmiast
    int32_t cumuTime = 0, dt;
    int8_t k=0;
    for ( ; (size_t)k<zakres.size(); k++)
        if ( odl(x0, zakres[k][t0][x0]) >= odl(x0, xEnd))
            break;

    if ((size_t)k==zakres.size())
    {
        k--;
        while ( odl(x0, zakres[k][t0][x0]) < odl(x0, xEnd)) // nie starczylo tablicy
        {
            dt = power(2, k-1);
            cumuTime+=dt;
            t0 = (t0+dt)%T;
            x0 = zakres[k][t0][x0];
        }
    }


    for(; k>=0; k--)
    {
        if ( odl(x0, zakres[k][t0][x0]) <= odl(x0, xEnd))
        {
            if(k>0)
                dt = power(2, k-1);
            else
                dt=0;
            cumuTime+=dt;
            t0 = (t0+dt)%T;
            x0 = min(zakres[k][t0][x0], xEnd);
        }
        if (x0==xEnd)
            break;
    }
    return cumuTime;
}



int main()
{
    int16_t m,n;
    int32_t ileTestow;
    cin>> m>> n>> ileTestow;

    vector<vector<string>> sygnalizacja; // n wierszy, m kolumn
    for (int ii=0; ii<n; ii++)
    {
        sygnalizacja.push_back(vector<string>(m));
        for(int jj=0; jj<m; jj++)
            cin>>sygnalizacja[ii][jj];
    }
    //* koniec wczytywania danych *//

    // Tablice [k x t x (x lub y)] - jak daleko w 2^(k-1) krokach dojde startujac w chwili t z pozycji (y,x)
    vector<vector<vector<int16_t>>> prawo, lewo, gora, dol;
    dokadDojde(prawo, lewo, sygnalizacja, true);
    dokadDojde(dol, gora, sygnalizacja, false);

    // odpowiedzi na zapytania
    for(int32_t test=0; test<ileTestow; test++)
    {
        int64_t t0, t1, t2, t;
        int16_t x0,y0,xEnd,yEnd;
        cin>> t0>> y0>> x0>> yEnd>> xEnd;

        if (x0<=xEnd)
        {
            t1 = czasPrzejscia(prawo, t0 %T, x0, xEnd); // idziemy w prawo - szukamy najkrotszego czasu na zrownanie x0 z xEnd - czas poczatkowy = t0 %T
        }
        else
        {
            t1 = czasPrzejscia(lewo, t0 %T, x0, xEnd); // idziemy w lewo
        }
        if (y0<=yEnd)
        {
            t2 = czasPrzejscia(dol, t0 %T, y0, yEnd); // idziemy w doł - szukamy najkrotszego czasu na zrownanie y0 z yEnd
        }
        else
        {
            t2 = czasPrzejscia(gora, t0 %T, y0, yEnd); // idziemy w górę
        }
        t = t0 + max(t1,t2);
        cout<<t<<endl;

    }



    return 0;
}