Advertisement
K_Y_M_bl_C

Untitled

Dec 12th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.15 KB | None | 0 0
  1. /*
  2. ID: wtfalex2
  3. LANG: C++14
  4. PROB: numtri
  5. */
  6. #define _CRT_SECURE_NO_WARNINGS
  7. #pragma comment(linker, "/STACK:256000000")
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <cmath>
  12. #include <vector>
  13. #include <ctime>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include <queue>
  18. #include <deque>
  19. #include <cassert>
  20. #include <cstdlib>
  21. #include <bitset>
  22. #include <algorithm>
  23. #include <string>
  24. #include <list>
  25. #include <fstream>
  26. #include <cstring>
  27. #include <climits>
  28. #include <stack>
  29. #include <unordered_map>
  30. #include <unordered_set>
  31.  
  32. using namespace std;
  33.  
  34. typedef unsigned long long ull;
  35. typedef long long ll;
  36. typedef pair<int, int> pii;
  37. typedef vector<int> vi;
  38. typedef pair<string, int> psi;
  39. typedef vector<string> vs;
  40. typedef pair<ll, ll> pll;
  41. typedef vector<ll> vll;
  42. typedef vector<char> vc;
  43. typedef vector<pii> vpii;
  44. typedef vector<pair<ll, ll> > vpll;
  45. typedef long double ld;
  46.  
  47. #define forn(i, n) for (int i = 0; i < (int)n; i++)
  48. #define for1(i, n) for (int i = 1; i <= (int)n; i++)
  49. #define forq(i, s, t) for (int i = s; i <= t; i++)
  50. #define ford(i, s, t) for (int i = s; i >= t; i--)
  51. #define mk make_pair
  52. #define inb push_back
  53. #define outb pop_back
  54. #define all(v) v.begin(), v.end()
  55. #define X first
  56. #define Y second
  57. #define TIME clock() * 1.0 / CLOCKS_PER_SEC
  58. #define sqr(x) (x) * (x)
  59. #define y1 amdknkgsdaasdwapgnpikn
  60. //
  61. #define mp make_pair
  62. #define pb push_back
  63. #define XX first
  64. #define YY second
  65. //
  66.  
  67. const ld EPS = 1e-9;
  68. const ld pi = acos(-1.0);
  69.  
  70. const int MAXN = (int)2e5 + 7;
  71. const ll INF = (ll)(1ll << 31ll) - 1ll;
  72. const ll LINF = (ll)7e18;
  73. const ll MOD = (ll)1e9 + 7;
  74. const int CHASH = (ll)239017;
  75. const ld DINF = (ld)1000000000000000.0;
  76.  
  77. void mkfile();
  78. int solve();
  79.  
  80. int main()
  81. {
  82. #define TASK "numtri"
  83. #ifdef _DEBUG
  84.     mkfile();
  85.     freopen("input.txt", "r", stdin);
  86.     freopen("output.txt", "w", stdout);
  87.     freopen("test.txt", "w", stderr);
  88.     ld tstart = TIME;
  89.     srand(2299);
  90. #else
  91.     //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  92.     //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout), freopen("test.txt", "w", stderr);
  93.     srand(time(0));
  94. #endif
  95.     solve();
  96. #ifdef _DEBUG
  97.     ld tend = TIME;
  98.     cerr << tend - tstart << " s.\n";
  99. #endif
  100.     return 0;
  101. }
  102.  
  103. void mkfile()
  104. {
  105.     freopen("input.txt", "a+", stdout);
  106.     srand(time(0));
  107.     return;
  108. }
  109.  
  110. template <class T = int> inline T readInt();
  111.  
  112. static const int buf_size = 4096;
  113.  
  114. inline int getChar() {
  115.     static char buf[buf_size];
  116.     static int len = 0, pos = 0;
  117.     if (pos == len)
  118.         pos = 0, len = fread(buf, 1, buf_size, stdin);
  119.     if (pos == len)
  120.         return -1;
  121.     return buf[pos++];
  122. }
  123.  
  124. inline int readChar() {
  125.     int c = getChar();
  126.     while (c <= 32)
  127.         c = getChar();
  128.     return c;
  129. }
  130.  
  131. template <class T>
  132. inline T readInt() {
  133.     int s = 1, c = readChar();
  134.     T x = 0;
  135.     //if (c == '-')
  136.     //    s = -1, c = getChar();
  137.     while ('0' <= c && c <= '9')
  138.         x = x * 10 + c - '0', c = getChar();
  139.     return s == 1 ? x : -x;
  140. }
  141.  
  142. /** Write */
  143.  
  144. static int write_pos = 0;
  145. static char write_buf[buf_size];
  146.  
  147. inline void writeChar(int x) {
  148.     if (write_pos == buf_size)
  149.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  150.     write_buf[write_pos++] = x;
  151. }
  152.  
  153. template <class T = int>
  154. inline void writeInt(T x, char end) {
  155.     //if (x < 0)
  156.     //    writeChar('-'), x = -x;
  157.  
  158.     char s[24];
  159.     int n = 0;
  160.     while (x || !n)
  161.         s[n++] = '0' + x % 10, x /= 10;
  162.     while (n--)
  163.         writeChar(s[n]);
  164.     if (end)
  165.         writeChar(end);
  166. }
  167.  
  168. inline void writeWord(const char *s) {
  169.     while (*s)
  170.         writeChar(*s++);
  171. }
  172.  
  173. struct Flusher {
  174.     ~Flusher() {
  175.         if (write_pos)
  176.             fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  177.     }
  178. } flusher;
  179.  
  180. int n, m;
  181. char a[1000007];
  182. int prerow[26][1000007], precol[26][1000007], sufrow[26][1000007], sufcol[26][1000007];
  183.  
  184. int solve()
  185. {
  186.     n = readInt();
  187.     m = readInt();
  188.     forn(i, n)
  189.     {
  190.         forn(j, m)
  191.             a[i * m + j] = readChar();
  192.     }
  193.     forn(i, n)
  194.         forn(k, 26)
  195.         {
  196.             prerow[k][i * m] = -1, sufrow[k][i * m + m - 1] = -1;
  197.         }
  198.     forn(i, m)
  199.         forn(k, 26)
  200.         {
  201.             precol[k][0 * m + i] = -1, sufcol[k][(n - 1) * m + i] = -1;
  202.         }
  203.     forn(i, n)
  204.     {
  205.         prerow[a[i * m + 0] - 'a'][i * m + 0] = 0;
  206.         for1(j, m - 1)
  207.         {
  208.             forn(k, 26)
  209.                 prerow[k][i * m + j] = prerow[k][i * m + j - 1];
  210.             prerow[a[i * m + j] - 'a'][i * m + j] = j;
  211.         }
  212.     }
  213.     forn(j, m)
  214.     {
  215.         precol[a[0 * m + j] - 'a'][0 * m + j] = 0;
  216.         for1(i, n - 1)
  217.         {
  218.             forn(k, 26)
  219.                 precol[k][i * m + j] = precol[k][(i - 1) * m + j];
  220.             precol[a[i * m + j] - 'a'][i * m + j] = i;
  221.         }
  222.     }
  223.     forn(i, n)
  224.     {
  225.         sufrow[a[i * m + m - 1] - 'a'][i * m + m - 1] = m - 1;
  226.         for (int j = m - 2; j >= 0; --j)
  227.         {
  228.             forn(k, 26)
  229.                 sufrow[k][i * m + j] = sufrow[k][i * m + j + 1];
  230.             sufrow[a[i * m + j] - 'a'][i * m + j] = j;
  231.         }
  232.     }
  233.     forn(j, m)
  234.     {
  235.         sufcol[a[(n - 1) * m + j] - 'a'][(n - 1) * m + j] = n - 1;
  236.         for (int i = n - 2; i >= 0; --i)
  237.         {
  238.             forn(k, 26)
  239.                 sufcol[k][i * m + j] = sufcol[k][(i + 1) * m + j];
  240.             sufcol[a[i * m + j] - 'a'][i * m + j] = i;
  241.         }
  242.     }
  243.     int Q;
  244.     Q = readInt();
  245.     int x, y, c, ptop, dst, minr;
  246.     while (Q--)
  247.     {
  248.         x = readInt();
  249.         y = readInt();
  250.         --x, --y;
  251.         minr = INF;
  252.         pair<pii, pii> ans = mk(mk(-1, -1), mk(-1, -1));
  253.         //L U
  254.         {
  255.             if (y - 1 >= 0 && x - 1 >= 0)
  256.             {
  257.                 c = a[x * m + y - 1] - 'a';
  258.                 forn(k, 26)
  259.                 {
  260.                     if (k != c)
  261.                     {
  262.                         ptop = precol[k][(x - 1) * m + y];
  263.                         if (ptop != -1)
  264.                         {
  265.                             dst = abs(ptop - x) + 1;
  266.                             if (dst < minr)
  267.                                 minr = dst, ans = mk(mk(x, y - 1), mk(ptop, y));
  268.                         }
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.         //L D
  274.         {
  275.             if (y - 1 >= 0 && x + 1 < n)
  276.             {
  277.                 c = a[x * m + y - 1] - 'a';
  278.                 forn(k, 26)
  279.                 {
  280.                     if (k != c)
  281.                     {
  282.                         ptop = sufcol[k][(x + 1) * m + y];
  283.                         if (ptop != -1)
  284.                         {
  285.                             dst = abs(ptop - x) + 1;
  286.                             if (dst < minr)
  287.                                 minr = dst, ans = mk(mk(x, y - 1), mk(ptop, y));
  288.                         }
  289.                     }
  290.                 }
  291.             }
  292.         }
  293.         //R U
  294.         {
  295.             if (y + 1 < m && x - 1 >= 0)
  296.             {
  297.                 c = a[x * m + y + 1] - 'a';
  298.                 forn(k, 26)
  299.                 {
  300.                     if (k != c)
  301.                     {
  302.                         ptop = precol[k][(x - 1) * m + y];
  303.                         if (ptop != -1)
  304.                         {
  305.                             dst = abs(ptop - x) + 1;
  306.                             if (dst < minr)
  307.                                 minr = dst, ans = mk(mk(x, y + 1), mk(ptop, y));
  308.                         }
  309.                     }
  310.                 }
  311.             }
  312.         }
  313.         //R D
  314.         {
  315.             if (y + 1 < m && x + 1 < n)
  316.             {
  317.                 c = a[x * m + y + 1] - 'a';
  318.                 forn(k, 26)
  319.                 {
  320.                     if (k != c)
  321.                     {
  322.                         ptop = sufcol[k][(x + 1) * m + y];
  323.                         if (ptop != -1)
  324.                         {
  325.                             dst = abs(ptop - x) + 1;
  326.                             if (dst < minr)
  327.                                 minr = dst, ans = mk(mk(x, y + 1), mk(ptop, y));
  328.                         }
  329.                     }
  330.                 }
  331.             }
  332.         }
  333.         //D L
  334.         {
  335.             if (x + 1 < n && y - 1 >= 0)
  336.             {
  337.                 c = a[(x + 1) * m + y] - 'a';
  338.                 forn(k, 26)
  339.                 {
  340.                     if (k != c)
  341.                     {
  342.                         ptop = prerow[k][x * m + y - 1];
  343.                         if (ptop != -1)
  344.                         {
  345.                             dst = abs(ptop - y) + 1;
  346.                             if (dst < minr)
  347.                                 minr = dst, ans = mk(mk(x + 1, y), mk(x, ptop));
  348.                         }
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.         //D R
  354.         {
  355.             if (x + 1 < n && y + 1 < m)
  356.             {
  357.                 c = a[(x + 1) * m + y] - 'a';
  358.                 forn(k, 26)
  359.                 {
  360.                     if (k != c)
  361.                     {
  362.                         ptop = sufrow[k][x * m + y + 1];
  363.                         if (ptop != -1)
  364.                         {
  365.                             dst = abs(ptop - y) + 1;
  366.                             if (dst < minr)
  367.                                 minr = dst, ans = mk(mk(x + 1, y), mk(x, ptop));
  368.                         }
  369.                     }
  370.                 }
  371.             }
  372.         }
  373.         //U L
  374.         {
  375.             if (y - 1 >= 0 && x - 1 >= 0)
  376.             {
  377.                 c = a[(x - 1) * m + y] - 'a';
  378.                 forn(k, 26)
  379.                 {
  380.                     if (k != c)
  381.                     {
  382.                         ptop = prerow[k][x * m + y - 1];
  383.                         if (ptop != -1)
  384.                         {
  385.                             dst = abs(ptop - y) + 1;
  386.                             if (dst < minr)
  387.                                 minr = dst, ans = mk(mk(x - 1, y), mk(x, ptop));
  388.                         }
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.         //U R
  394.         {
  395.             if (y + 1 < m && x - 1 >= 0)
  396.             {
  397.                 c = a[(x - 1) * m + y] - 'a';
  398.                 forn(k, 26)
  399.                 {
  400.                     if (k != c)
  401.                     {
  402.                         int ptop = sufrow[k][x * m + y + 1];
  403.                         if (ptop != -1)
  404.                         {
  405.                             dst = abs(ptop - y) + 1;
  406.                             if (dst < minr)
  407.                                 minr = dst, ans = mk(mk(x - 1, y), mk(x, ptop));
  408.                         }
  409.                     }
  410.                 }
  411.             }
  412.         }
  413.         if (ans.X.X == -1)
  414.             writeChar('-'), writeChar('1'), writeChar('\n');
  415.         else
  416.             writeInt(ans.X.X + 1, ' '), writeInt(ans.X.Y + 1, ' '), writeInt(ans.Y.X + 1, ' '), writeInt(ans.Y.Y + 1, '\n');
  417.     }
  418.     return 0;
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement