Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: wtfalex2
- LANG: C++14
- PROB: numtri
- */
- #define _CRT_SECURE_NO_WARNINGS
- #pragma comment(linker, "/STACK:256000000")
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <map>
- #include <set>
- #include <string>
- #include <queue>
- #include <deque>
- #include <cassert>
- #include <cstdlib>
- #include <bitset>
- #include <algorithm>
- #include <string>
- #include <list>
- #include <fstream>
- #include <cstring>
- #include <climits>
- #include <stack>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef pair<string, int> psi;
- typedef vector<string> vs;
- typedef pair<ll, ll> pll;
- typedef vector<ll> vll;
- typedef vector<char> vc;
- typedef vector<pii> vpii;
- typedef vector<pair<ll, ll> > vpll;
- typedef long double ld;
- #define forn(i, n) for (int i = 0; i < (int)n; i++)
- #define for1(i, n) for (int i = 1; i <= (int)n; i++)
- #define forq(i, s, t) for (int i = s; i <= t; i++)
- #define ford(i, s, t) for (int i = s; i >= t; i--)
- #define mk make_pair
- #define inb push_back
- #define outb pop_back
- #define all(v) v.begin(), v.end()
- #define X first
- #define Y second
- #define TIME clock() * 1.0 / CLOCKS_PER_SEC
- #define sqr(x) (x) * (x)
- #define y1 amdknkgsdaasdwapgnpikn
- //
- #define mp make_pair
- #define pb push_back
- #define XX first
- #define YY second
- //
- const ld EPS = 1e-9;
- const ld pi = acos(-1.0);
- const int MAXN = (int)2e5 + 7;
- const ll INF = (ll)(1ll << 31ll) - 1ll;
- const ll LINF = (ll)7e18;
- const ll MOD = (ll)1e9 + 7;
- const int CHASH = (ll)239017;
- const ld DINF = (ld)1000000000000000.0;
- void mkfile();
- int solve();
- int main()
- {
- #define TASK "numtri"
- #ifdef _DEBUG
- mkfile();
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- freopen("test.txt", "w", stderr);
- ld tstart = TIME;
- srand(2299);
- #else
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout), freopen("test.txt", "w", stderr);
- srand(time(0));
- #endif
- solve();
- #ifdef _DEBUG
- ld tend = TIME;
- cerr << tend - tstart << " s.\n";
- #endif
- return 0;
- }
- void mkfile()
- {
- freopen("input.txt", "a+", stdout);
- srand(time(0));
- return;
- }
- template <class T = int> inline T readInt();
- static const int buf_size = 4096;
- inline int getChar() {
- static char buf[buf_size];
- static int len = 0, pos = 0;
- if (pos == len)
- pos = 0, len = fread(buf, 1, buf_size, stdin);
- if (pos == len)
- return -1;
- return buf[pos++];
- }
- inline int readChar() {
- int c = getChar();
- while (c <= 32)
- c = getChar();
- return c;
- }
- template <class T>
- inline T readInt() {
- int s = 1, c = readChar();
- T x = 0;
- //if (c == '-')
- // s = -1, c = getChar();
- while ('0' <= c && c <= '9')
- x = x * 10 + c - '0', c = getChar();
- return s == 1 ? x : -x;
- }
- /** Write */
- static int write_pos = 0;
- static char write_buf[buf_size];
- inline void writeChar(int x) {
- if (write_pos == buf_size)
- fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
- write_buf[write_pos++] = x;
- }
- template <class T = int>
- inline void writeInt(T x, char end) {
- //if (x < 0)
- // writeChar('-'), x = -x;
- char s[24];
- int n = 0;
- while (x || !n)
- s[n++] = '0' + x % 10, x /= 10;
- while (n--)
- writeChar(s[n]);
- if (end)
- writeChar(end);
- }
- inline void writeWord(const char *s) {
- while (*s)
- writeChar(*s++);
- }
- struct Flusher {
- ~Flusher() {
- if (write_pos)
- fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
- }
- } flusher;
- int n, m;
- char a[1000007];
- int prerow[26][1000007], precol[26][1000007], sufrow[26][1000007], sufcol[26][1000007];
- int solve()
- {
- n = readInt();
- m = readInt();
- forn(i, n)
- {
- forn(j, m)
- a[i * m + j] = readChar();
- }
- forn(i, n)
- forn(k, 26)
- {
- prerow[k][i * m] = -1, sufrow[k][i * m + m - 1] = -1;
- }
- forn(i, m)
- forn(k, 26)
- {
- precol[k][0 * m + i] = -1, sufcol[k][(n - 1) * m + i] = -1;
- }
- forn(i, n)
- {
- prerow[a[i * m + 0] - 'a'][i * m + 0] = 0;
- for1(j, m - 1)
- {
- forn(k, 26)
- prerow[k][i * m + j] = prerow[k][i * m + j - 1];
- prerow[a[i * m + j] - 'a'][i * m + j] = j;
- }
- }
- forn(j, m)
- {
- precol[a[0 * m + j] - 'a'][0 * m + j] = 0;
- for1(i, n - 1)
- {
- forn(k, 26)
- precol[k][i * m + j] = precol[k][(i - 1) * m + j];
- precol[a[i * m + j] - 'a'][i * m + j] = i;
- }
- }
- forn(i, n)
- {
- sufrow[a[i * m + m - 1] - 'a'][i * m + m - 1] = m - 1;
- for (int j = m - 2; j >= 0; --j)
- {
- forn(k, 26)
- sufrow[k][i * m + j] = sufrow[k][i * m + j + 1];
- sufrow[a[i * m + j] - 'a'][i * m + j] = j;
- }
- }
- forn(j, m)
- {
- sufcol[a[(n - 1) * m + j] - 'a'][(n - 1) * m + j] = n - 1;
- for (int i = n - 2; i >= 0; --i)
- {
- forn(k, 26)
- sufcol[k][i * m + j] = sufcol[k][(i + 1) * m + j];
- sufcol[a[i * m + j] - 'a'][i * m + j] = i;
- }
- }
- int Q;
- Q = readInt();
- int x, y, c, ptop, dst, minr;
- while (Q--)
- {
- x = readInt();
- y = readInt();
- --x, --y;
- minr = INF;
- pair<pii, pii> ans = mk(mk(-1, -1), mk(-1, -1));
- //L U
- {
- if (y - 1 >= 0 && x - 1 >= 0)
- {
- c = a[x * m + y - 1] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = precol[k][(x - 1) * m + y];
- if (ptop != -1)
- {
- dst = abs(ptop - x) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x, y - 1), mk(ptop, y));
- }
- }
- }
- }
- }
- //L D
- {
- if (y - 1 >= 0 && x + 1 < n)
- {
- c = a[x * m + y - 1] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = sufcol[k][(x + 1) * m + y];
- if (ptop != -1)
- {
- dst = abs(ptop - x) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x, y - 1), mk(ptop, y));
- }
- }
- }
- }
- }
- //R U
- {
- if (y + 1 < m && x - 1 >= 0)
- {
- c = a[x * m + y + 1] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = precol[k][(x - 1) * m + y];
- if (ptop != -1)
- {
- dst = abs(ptop - x) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x, y + 1), mk(ptop, y));
- }
- }
- }
- }
- }
- //R D
- {
- if (y + 1 < m && x + 1 < n)
- {
- c = a[x * m + y + 1] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = sufcol[k][(x + 1) * m + y];
- if (ptop != -1)
- {
- dst = abs(ptop - x) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x, y + 1), mk(ptop, y));
- }
- }
- }
- }
- }
- //D L
- {
- if (x + 1 < n && y - 1 >= 0)
- {
- c = a[(x + 1) * m + y] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = prerow[k][x * m + y - 1];
- if (ptop != -1)
- {
- dst = abs(ptop - y) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x + 1, y), mk(x, ptop));
- }
- }
- }
- }
- }
- //D R
- {
- if (x + 1 < n && y + 1 < m)
- {
- c = a[(x + 1) * m + y] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = sufrow[k][x * m + y + 1];
- if (ptop != -1)
- {
- dst = abs(ptop - y) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x + 1, y), mk(x, ptop));
- }
- }
- }
- }
- }
- //U L
- {
- if (y - 1 >= 0 && x - 1 >= 0)
- {
- c = a[(x - 1) * m + y] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- ptop = prerow[k][x * m + y - 1];
- if (ptop != -1)
- {
- dst = abs(ptop - y) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x - 1, y), mk(x, ptop));
- }
- }
- }
- }
- }
- //U R
- {
- if (y + 1 < m && x - 1 >= 0)
- {
- c = a[(x - 1) * m + y] - 'a';
- forn(k, 26)
- {
- if (k != c)
- {
- int ptop = sufrow[k][x * m + y + 1];
- if (ptop != -1)
- {
- dst = abs(ptop - y) + 1;
- if (dst < minr)
- minr = dst, ans = mk(mk(x - 1, y), mk(x, ptop));
- }
- }
- }
- }
- }
- if (ans.X.X == -1)
- writeChar('-'), writeChar('1'), writeChar('\n');
- else
- writeInt(ans.X.X + 1, ' '), writeInt(ans.X.Y + 1, ' '), writeInt(ans.Y.X + 1, ' '), writeInt(ans.Y.Y + 1, '\n');
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement