Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. struct SparseTable
  2. {
  3.     int st[2][MAXN][MAXN][7][7];
  4.     void build()
  5.     {
  6.         for (int i = 0; i < n; ++i)
  7.             for (int j = 0; j < m; ++j)
  8.                 st[0][i][j][0][0] = a[i][j] - 'a' + 1;
  9.         for (int i1 = n - 1, i = 0; i1 >= 0; --i1, ++i)
  10.             for (int j1 = m - 1, j = 0; j1 >= 0; --j1, ++j)
  11.                 st[1][i][j][0][0] = a[i1][j1] - 'a' + 1;
  12.         for (int id = 0; id < 2; ++id)
  13.         {
  14.             for (int i = 0; i < n; ++i)
  15.             {
  16.                 for (int j = 0; j < m; ++j)
  17.                 {
  18.                     for (int lg = 1; lg <= lgs[n]; ++lg)
  19.                     {
  20.                         st[id][i][j][0][lg] = (mul(st[id][i][j][0][lg - 1], q[1 << (lg - 1)]) + st[id][i][j + (1 << (lg - 1))][0][lg - 1]) % HMOD;
  21.                     }
  22.                 }
  23.             }
  24.             for (int lg = 1; lg <= lgs[n]; ++lg)
  25.             {
  26.                 for (int i = 0; i < n; ++i)
  27.                 {
  28.                     for (int lg1 = 1; lg1 <= lgs[m]; ++lg1)
  29.                     {
  30.                         for (int j = 0; j < m; ++j)
  31.                         {
  32.                             st[id][i][j][lg][lg1] = (mul(st[id][i][j][lg - 1][lg1], p[1 << (lg - 1)]) + st[id][i + (1 << (lg - 1))][j][lg - 1][lg1]) % HMOD;
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     int check(int x1, int y1, int x2, int y2)
  40.     {
  41.         int nx1 = n - 1 - x2, ny1 = m - 1 - y2, nx2 = n - 1 - x1, ny2 = m - 1 - y1;
  42.         int ans = 0;
  43.         int k1 = lgs[x2 - x1 + 1];
  44.         int k2 = lgs[y2 - y1 + 1];
  45.         ans += st[0][x1][y1][k1][k2] == st[1][nx1][ny1][k1][k2];
  46.         ans += st[0][x2 - (1 << k1) + 1][y1][k1][k2] == st[1][nx2 - (1 << k1) + 1][ny1][k1][k2];
  47.         ans += st[0][x1][y2 - (1 << k2) + 1][k1][k2] == st[1][nx1][ny2 - (1 << k2) + 1][k1][k2];
  48.         ans += st[0][x2 - (1 << k1) + 1][y2 - (1 << k2) + 1][k1][k2] == st[1][nx2 - (1 << k1) + 1][ny2 - (1 << k2) + 1][k1][k2];
  49.         return ans == 4;
  50.     }
  51. } T;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement