#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> #include <assert.h> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 16000 #define SS 1100000 int n,m,q; string s[SS]; const int Z=840; int typ[Z]; #define right right_ bool down[SZ][9][9]; bool right[SZ][9][9]; int ri[Z][SZ],li[Z][SZ]; inline bool can_right(int x,int t) { assert(0<=x&&x+1<m); for(int i=2;i<=8;++i) if(right[x][i][t%i]) return 1; return 0; } inline bool can_down(int x,int t) { assert(0<=x&&x+1<n); for(int i=2;i<=8;++i) if(down[x][i][t%i]) return 1; return 0; } inline int walk(int t,int x,int u,int*r) { if(t!=u||x==-1) return -1; return r[x]; } const int B=20,U=14; int wkr[Z/B][U][SZ]; int wkl[Z/B][U][SZ]; void bz() { for(int i=0;i<Z;i+=B) for(int j=0;j<max(n,m);++j) { int t=typ[i],x=j; for(int s=i;s<i+B;++s) x=walk(t,x,typ[s],ri[s]); wkr[i/B][0][j]=x; t=typ[i],x=j; for(int s=i;s<i+B;++s) x=walk(t,x,typ[s],li[s]); wkl[i/B][0][j]=x; } for(int r=1;r<U;++r) for(int i=0;i<Z;i+=B) { int ii=(i+((ll)B<<(r-1)))%Z; for(int j=0;j<max(n,m);++j) wkr[i/B][r][j]=walk(typ[i],wkr[i/B][r-1][j],typ[ii],wkr[ii/B][r-1]), wkl[i/B][r][j]=walk(typ[i],wkl[i/B][r-1][j],typ[ii],wkl[ii/B][r-1]); } } void init(int t) { assert(Z%B==0); int x=0,y=0; while(x!=n-1&&y!=m-1) { int l=s[x*m+y].size(); if(s[x*m+y][t%l]=='0') ++x; else ++y; } if(x==n-1) typ[t]=1; else typ[t]=0; //downward rays if(typ[t]) { int L=0; while(L<m) { int R=L; while(R!=m-1&&can_right(R,t)) ++R; for(int i=L;i<=R;++i) ri[t][i]=R,li[t][i]=L; L=R+1; } } else { int L=0; while(L<n) { int R=L; while(R!=n-1&&can_down(R,t)) ++R; for(int i=L;i<=R;++i) ri[t][i]=R,li[t][i]=L; L=R+1; } } } char ch,BB[1<<20],*S=BB,*T=BB; #define getc() (S==T&&(T=(S=BB)+fread(BB,1,1<<20,stdin),S==T)?0:*S++) #define isd(c) (c>='0'&&c<='9') int aa,bb;int F(){ while(ch=getc(),!isd(ch)&&ch!='-');ch=='-'?aa=bb=0:(aa=ch-'0',bb=1); while(ch=getc(),isd(ch))aa=aa*10+ch-'0';return bb?aa:-aa; } #define gi F() #define BUFSIZE 500000 namespace fob {char b[BUFSIZE]={},*f=b,*g=b+BUFSIZE-2;} #define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0) #define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0) struct foce {~foce() {pob; fflush(stdout);}} _foce; namespace ib {char b[100];} inline void pint(int x) { if(x==0) {pc(48); return;} char *s=ib::b; while(x) *(++s)=x%10, x/=10; while(s!=ib::b) pc((*(s--))+48); } int main() { // cerr<<sizeof(wk)/1024.0/1024.0<<"M\n"; scanf("%d%d%d",&n,&m,&q);++n,++m; for(int i=0;i+1<n;++i) for(int j=0;j+1<m;++j) { char buf[10]; scanf("%s",buf); s[i*m+j]=buf; } for(int i=0;i+1<n;++i) for(int j=0;j+1<m;++j) { int l=s[i*m+j].size(); for(int k=0;k<l;++k) if(s[i*m+j][k]=='0') down[i][l][k]=1; else right[j][l][k]=1; } cerr<<clock()<<"ms?\n"; for(int i=0;i<Z;++i) init(i); // for(int i=0;i<10;++i) { // cout<<i<<": "<<typ[i]<<"|"; // for(int j=0;j<7;++j) cout<<ri[i][j]<<","; // cout<<"\b \n"; // } bz(); cerr<<clock()<<"ms?\n"; int mxd=0; while(q--) { int t,a,b,c,d,p,q; t=gi,a=gi,b=gi,c=gi,d=gi; // scanf("%d%d%d%d%d",&t,&a,&b,&c,&d); int t_=t; if(!typ[t%Z]) { //downwards p=a, q=c; } else { p=b, q=d; } // cerr<<typ[t%Z]<<"? "<<p<<","<<q<<"\n"; int ty=typ[t%Z]; if(p<=q) { #define bad(r) ((r)!=-1&&(r)<q) while(bad(p)&&t%B!=0) { p=walk(ty,p,typ[t%Z],ri[t%Z]); if(!bad(p)) break; ++t; } if(bad(p)) { assert(t%B==0); for(int j=U-1;j>=0;--j) { int pp=walk(ty,p,typ[t%Z],wkr[t%Z/B][j]); if(bad(pp)) t+=B<<j,p=pp; } } while(bad(p)) { p=walk(ty,p,typ[t%Z],ri[t%Z]); if(!bad(p)) break; ++t; } #undef bad } else { #define bad(r) ((r)!=-1&&(r)>q) while(bad(p)&&t%B!=0) { p=walk(ty,p,typ[t%Z],li[t%Z]); if(!bad(p)) break; ++t; } if(bad(p)) { assert(t%B==0); for(int j=U-1;j>=0;--j) { int pp=walk(ty,p,typ[t%Z],wkl[t%Z/B][j]); if(bad(pp)) t+=B<<j,p=pp; } } while(bad(p)) { p=walk(ty,p,typ[t%Z],li[t%Z]); if(!bad(p)) break; ++t; } #undef bad } mxd=max(mxd,t-t_); pint(t); pc(10); // printf("%d\n",t); } cerr<<clock()<<"ms?\n"; cerr<<"mxd="<<mxd<<"\n"; }
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 | #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> #include <iomanip> #include <assert.h> using namespace std; #define pb push_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; typedef double ld; typedef vector<int> vi; #define fi first #define se second #define fe first #define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);} #define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);} #define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);} #define es(x,e) (int e=fst[x];e;e=nxt[e]) #define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e]) #define SZ 16000 #define SS 1100000 int n,m,q; string s[SS]; const int Z=840; int typ[Z]; #define right right_ bool down[SZ][9][9]; bool right[SZ][9][9]; int ri[Z][SZ],li[Z][SZ]; inline bool can_right(int x,int t) { assert(0<=x&&x+1<m); for(int i=2;i<=8;++i) if(right[x][i][t%i]) return 1; return 0; } inline bool can_down(int x,int t) { assert(0<=x&&x+1<n); for(int i=2;i<=8;++i) if(down[x][i][t%i]) return 1; return 0; } inline int walk(int t,int x,int u,int*r) { if(t!=u||x==-1) return -1; return r[x]; } const int B=20,U=14; int wkr[Z/B][U][SZ]; int wkl[Z/B][U][SZ]; void bz() { for(int i=0;i<Z;i+=B) for(int j=0;j<max(n,m);++j) { int t=typ[i],x=j; for(int s=i;s<i+B;++s) x=walk(t,x,typ[s],ri[s]); wkr[i/B][0][j]=x; t=typ[i],x=j; for(int s=i;s<i+B;++s) x=walk(t,x,typ[s],li[s]); wkl[i/B][0][j]=x; } for(int r=1;r<U;++r) for(int i=0;i<Z;i+=B) { int ii=(i+((ll)B<<(r-1)))%Z; for(int j=0;j<max(n,m);++j) wkr[i/B][r][j]=walk(typ[i],wkr[i/B][r-1][j],typ[ii],wkr[ii/B][r-1]), wkl[i/B][r][j]=walk(typ[i],wkl[i/B][r-1][j],typ[ii],wkl[ii/B][r-1]); } } void init(int t) { assert(Z%B==0); int x=0,y=0; while(x!=n-1&&y!=m-1) { int l=s[x*m+y].size(); if(s[x*m+y][t%l]=='0') ++x; else ++y; } if(x==n-1) typ[t]=1; else typ[t]=0; //downward rays if(typ[t]) { int L=0; while(L<m) { int R=L; while(R!=m-1&&can_right(R,t)) ++R; for(int i=L;i<=R;++i) ri[t][i]=R,li[t][i]=L; L=R+1; } } else { int L=0; while(L<n) { int R=L; while(R!=n-1&&can_down(R,t)) ++R; for(int i=L;i<=R;++i) ri[t][i]=R,li[t][i]=L; L=R+1; } } } char ch,BB[1<<20],*S=BB,*T=BB; #define getc() (S==T&&(T=(S=BB)+fread(BB,1,1<<20,stdin),S==T)?0:*S++) #define isd(c) (c>='0'&&c<='9') int aa,bb;int F(){ while(ch=getc(),!isd(ch)&&ch!='-');ch=='-'?aa=bb=0:(aa=ch-'0',bb=1); while(ch=getc(),isd(ch))aa=aa*10+ch-'0';return bb?aa:-aa; } #define gi F() #define BUFSIZE 500000 namespace fob {char b[BUFSIZE]={},*f=b,*g=b+BUFSIZE-2;} #define pob (fwrite(fob::b,sizeof(char),fob::f-fob::b,stdout),fob::f=fob::b,0) #define pc(x) (*(fob::f++)=(x),(fob::f==fob::g)?pob:0) struct foce {~foce() {pob; fflush(stdout);}} _foce; namespace ib {char b[100];} inline void pint(int x) { if(x==0) {pc(48); return;} char *s=ib::b; while(x) *(++s)=x%10, x/=10; while(s!=ib::b) pc((*(s--))+48); } int main() { // cerr<<sizeof(wk)/1024.0/1024.0<<"M\n"; scanf("%d%d%d",&n,&m,&q);++n,++m; for(int i=0;i+1<n;++i) for(int j=0;j+1<m;++j) { char buf[10]; scanf("%s",buf); s[i*m+j]=buf; } for(int i=0;i+1<n;++i) for(int j=0;j+1<m;++j) { int l=s[i*m+j].size(); for(int k=0;k<l;++k) if(s[i*m+j][k]=='0') down[i][l][k]=1; else right[j][l][k]=1; } cerr<<clock()<<"ms?\n"; for(int i=0;i<Z;++i) init(i); // for(int i=0;i<10;++i) { // cout<<i<<": "<<typ[i]<<"|"; // for(int j=0;j<7;++j) cout<<ri[i][j]<<","; // cout<<"\b \n"; // } bz(); cerr<<clock()<<"ms?\n"; int mxd=0; while(q--) { int t,a,b,c,d,p,q; t=gi,a=gi,b=gi,c=gi,d=gi; // scanf("%d%d%d%d%d",&t,&a,&b,&c,&d); int t_=t; if(!typ[t%Z]) { //downwards p=a, q=c; } else { p=b, q=d; } // cerr<<typ[t%Z]<<"? "<<p<<","<<q<<"\n"; int ty=typ[t%Z]; if(p<=q) { #define bad(r) ((r)!=-1&&(r)<q) while(bad(p)&&t%B!=0) { p=walk(ty,p,typ[t%Z],ri[t%Z]); if(!bad(p)) break; ++t; } if(bad(p)) { assert(t%B==0); for(int j=U-1;j>=0;--j) { int pp=walk(ty,p,typ[t%Z],wkr[t%Z/B][j]); if(bad(pp)) t+=B<<j,p=pp; } } while(bad(p)) { p=walk(ty,p,typ[t%Z],ri[t%Z]); if(!bad(p)) break; ++t; } #undef bad } else { #define bad(r) ((r)!=-1&&(r)>q) while(bad(p)&&t%B!=0) { p=walk(ty,p,typ[t%Z],li[t%Z]); if(!bad(p)) break; ++t; } if(bad(p)) { assert(t%B==0); for(int j=U-1;j>=0;--j) { int pp=walk(ty,p,typ[t%Z],wkl[t%Z/B][j]); if(bad(pp)) t+=B<<j,p=pp; } } while(bad(p)) { p=walk(ty,p,typ[t%Z],li[t%Z]); if(!bad(p)) break; ++t; } #undef bad } mxd=max(mxd,t-t_); pint(t); pc(10); // printf("%d\n",t); } cerr<<clock()<<"ms?\n"; cerr<<"mxd="<<mxd<<"\n"; } |