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
| #include<cstdio> #include<algorithm> #include<cctype> #include<string.h> #include<cmath> #include<vector>
using namespace std; #define ll long long
inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s==t?t=(s=buf)+fread(buf,1,IN_LEN,stdin),(s==t?-1:*s++):*s++); } template<class T> inline void read(T &x) { static bool iosig; static char c; for (iosig=false, c=read(); !isdigit(c); c=read()) { if (c == '-') iosig=true; if (c == -1) return; } for (x=0; isdigit(c); c=read()) x=((x+(x<<2))<<1)+(c^'0'); if (iosig) x=-x; } const int OUT_LEN = 10000000; char obuf[OUT_LEN], *ooh=obuf; inline void print(char c) { if (ooh==obuf+OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh=obuf; *ooh++=c; } template<class T> inline void print(T x) { static int buf[30], cnt; if (x==0) print('0'); else { if (x<0) print('-'), x=-x; for (cnt=0; x; x/=10) buf[++cnt]=x%10+48; while(cnt) print((char)buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
const int N = 100005, M = 5; pair<int,int> A, B, f[M][N]; int n, m, q, ans, cnt[M], tl[N][2], tr[N][2], sl[N][2], sr[N][2], dis[M][N]; bool g[M][N]; char s[N], t[N]; inline void extend(int id, pair<int,int> a, int d, bool e){ int x=a.first, y=a.second, z; if(e){ if(t[y]=='S'){ if(sr[x][0]) f[id][cnt[id]]=make_pair(sr[x][0], y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+sr[x][0]-x; if(sr[x][1]) f[id][cnt[id]]=make_pair(sr[x][1], y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+sr[x][1]-x; if(B.first>x){ if(B.second==y) ans=min(ans, d+B.first-x); if(z=sr[B.first-1][B.second>y]) f[id][cnt[id]]=make_pair(z, y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+z-x; } } else{ if(sl[x][0]) f[id][cnt[id]]=make_pair(sl[x][0], y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+x-sl[x][0]; if(sl[x][1]) f[id][cnt[id]]=make_pair(sl[x][1], y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+x-sl[x][1]; if(B.first<x){ if(B.second==y) ans=min(ans, d+x-B.first); if(z=sl[B.first+1][B.second>y]) f[id][cnt[id]]=make_pair(z, y), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+x-z; } } } else{ if(s[x]=='E'){ if(tr[y][0]) f[id][cnt[id]]=make_pair(x, tr[y][0]), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+tr[y][0]-y; if(tr[y][1]) f[id][cnt[id]]=make_pair(x, tr[y][1]), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+tr[y][1]-y; if(B.second>y){ if(B.first==x) ans=min(ans, d+B.second-y); if(z=tr[B.second-1][B.first>x]) f[id][cnt[id]]=make_pair(x, z), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+z-y; } } else{ if(tl[y][0]) f[id][cnt[id]]=make_pair(x, tl[y][0]), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+y-tl[y][0]; if(tl[y][1]) f[id][cnt[id]]=make_pair(x, tl[y][1]), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+y-tl[y][1]; if(B.second<y){ if(B.first==x) ans=min(ans, d+y-B.second); if(z=tl[B.second+1][B.first>x]) f[id][cnt[id]]=make_pair(x, z), g[id][cnt[id]]=e, dis[id][cnt[id]++]=d+y-z; } } } } int main() { read(n), read(m), read(q); while(isspace(s[1]=read())); for(int i=2; i<=n; ++i) s[i]=read(); while(isspace(t[1]=read())); for(int i=2; i<=m; ++i) t[i]=read();
for(int i=2; i<=n; ++i) sl[i][0]=sl[i-1][0], sl[i][1]=sl[i-1][1], sl[i][s[i-1]=='E']=i-1; for(int i=n; --i;) sr[i][0]=sr[i+1][0], sr[i][1]=sr[i+1][1], sr[i][s[i+1]=='E']=i+1; for(int i=2; i<=m; ++i) tl[i][0]=tl[i-1][0], tl[i][1]=tl[i-1][1], tl[i][t[i-1]=='S']=i-1; for(int i=m; --i;) tr[i][0]=tr[i+1][0], tr[i][1]=tr[i+1][1], tr[i][t[i+1]=='S']=i+1; while(q--){ read(A.first), read(A.second), read(B.first), read(B.second); memset(cnt, 0, sizeof cnt), ans=1e9, extend(0, A, 0, 0), extend(0, A, 0, 1); for(int i=0; i<4; ++i){ for(int j=0; j<cnt[i]; ++j) extend(i+1, f[i][j], dis[i][j], g[i][j]^1); } print(ans==1e9?-1:ans), print('\n'); } return flush(), 0; }
|