Advertisement
K_Y_M_bl_C

Untitled

May 14th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.51 KB | None | 0 0
  1. /*
  2. ID: wtfalex2
  3. LANG: C++14
  4. PROB: castle
  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)4e5 + 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 "ufo"
  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. bool equal(double a, double b)
  111. {
  112.     return abs(a - b) < EPS;
  113. }
  114.  
  115. const int MM = (int)4e8 + 7;
  116.  
  117. int n, m, p, r, k;
  118. vector<vi> a;
  119. vector<vi> dp;
  120. vector<vi> t[2];
  121. int ans;
  122.  
  123. void buildRow(int id, int v, int tl, int tr)
  124. {
  125.     if (tl == tr)
  126.         t[0][id][v] = a[id][tl];
  127.     else
  128.     {
  129.         int tm = tl + tr >> 1;
  130.         buildRow(id, (v << 1) + 1, tl, tm);
  131.         buildRow(id, (v << 1) + 2, tm + 1, tr);
  132.         t[0][id][v] = max(t[0][id][(v << 1) + 1], t[0][id][(v << 1) + 2]);
  133.     }
  134. }
  135.  
  136. void buildCol(int id, int v, int tl, int tr)
  137. {
  138.     if (tl == tr)
  139.         t[1][v][id] = a[tl][id];
  140.     else
  141.     {
  142.         int tm = tl + tr >> 1;
  143.         buildCol(id, (v << 1) + 1, tl, tm);
  144.         buildCol(id, (v << 1) + 2, tm + 1, tr);
  145.         t[1][v][id] = max(t[1][(v << 1) + 1][id], t[1][(v << 1) + 2][id]);
  146.     }
  147. }
  148.  
  149. int findLRow(int id, int v, int tl, int tr, int pos, int val)
  150. {
  151.     if (tr < pos || t[0][id][v] < val)
  152.         return -1;
  153.     if (tl == tr)
  154.         return tl;
  155.     int tm = tl + tr >> 1;
  156.     int ql = findLRow(id, (v << 1) + 1, tl, tm, pos, val);
  157.     if (ql == -1)
  158.         return findLRow(id, (v << 1) + 2, tm + 1, tr, pos, val);
  159.     return ql;
  160. }
  161.  
  162. int findRRow(int id, int v, int tl, int tr, int pos, int val)
  163. {
  164.     if (tl > pos || t[0][id][v] < val)
  165.         return -1;
  166.     if (tl == tr)
  167.         return tl;
  168.     int tm = tl + tr >> 1;
  169.     int qr = findRRow(id, (v << 1) + 2, tm + 1, tr, pos, val);
  170.     if (qr == -1)
  171.         return findRRow(id, (v << 1) + 1, tl, tm, pos, val);
  172.     return qr;
  173. }
  174.  
  175. int findUCol(int id, int v, int tl, int tr, int pos, int val)
  176. {
  177.     if (tr < pos || t[1][v][id] < val)
  178.         return -1;
  179.     if (tl == tr)
  180.         return tl;
  181.     int tm = tl + tr >> 1;
  182.     int ql = findUCol(id, (v << 1) + 1, tl, tm, pos, val);
  183.     if (ql == -1)
  184.         return findUCol(id, (v << 1) + 2, tm + 1, tr, pos, val);
  185.     return ql;
  186. }
  187.  
  188. int findDCol(int id, int v, int tl, int tr, int pos, int val)
  189. {
  190.     if (tl > pos || t[1][v][id] < val)
  191.         return -1;
  192.     if (tl == tr)
  193.         return tl;
  194.     int tm = tl + tr >> 1;
  195.     int qr = findDCol(id, (v << 1) + 2, tm + 1, tr, pos, val);
  196.     if (qr == -1)
  197.         return findDCol(id, (v << 1) + 1, tl, tm, pos, val);
  198.     return qr;
  199. }
  200.  
  201. void updateRow(int id, int v, int tl, int tr, int pos)
  202. {
  203.     if (tl > pos || tr < pos)
  204.         return;
  205.     if (tl == tr)
  206.     {
  207.         --t[0][id][v];
  208.         return;
  209.     }
  210.     int tm = tl + tr >> 1;
  211.     updateRow(id, (v << 1) + 1, tl, tm, pos);
  212.     updateRow(id, (v << 1) + 2, tm + 1, tr, pos);
  213.     t[0][id][v] = max(t[0][id][(v << 1) + 1], t[0][id][(v << 1) + 2]);
  214. }
  215.  
  216. void updateCol(int id, int v, int tl, int tr, int pos)
  217. {
  218.     if (tl > pos || tr < pos)
  219.         return;
  220.     if (tl == tr)
  221.     {
  222.         --t[1][v][id];
  223.         return;
  224.     }
  225.     int tm = tl + tr >> 1;
  226.     updateCol(id, (v << 1) + 1, tl, tm, pos);
  227.     updateCol(id, (v << 1) + 2, tm + 1, tr, pos);
  228.     t[1][v][id] = max(t[1][(v << 1) + 1][id], t[1][(v << 1) + 2][id]);
  229. }
  230.  
  231. void getRow(int id, int v, int tl, int tr)
  232. {
  233.     if (tl == tr)
  234.         a[id][tl] = t[0][id][v];
  235.     else
  236.     {
  237.         int tm = tl + tr >> 1;
  238.         getRow(id, (v << 1) + 1, tl, tm);
  239.         getRow(id, (v << 1) + 2, tm + 1, tr);
  240.     }
  241. }
  242.  
  243. int solve()
  244. {
  245.     scanf("%d %d %d %d %d", &n, &m, &r, &k, &p);
  246.     a.resize(n);
  247.     t[0].resize(n);
  248.     t[1].resize(4 * n);
  249.     forn(i, 4 * n)
  250.         t[1][i].resize(m);
  251.     forn(i, n)
  252.     {
  253.         t[0][i].resize(4 * m);
  254.         a[i].resize(m);
  255.         forn(j, m)
  256.             scanf("%d", &a[i][j]);
  257.     }
  258.     forn(i, n)
  259.         buildRow(i, 0, 0, m - 1);
  260.     forn(i, m)
  261.         buildCol(i, 0, 0, n - 1);
  262.     forn(IT, k)
  263.     {
  264.         char c;
  265.         int x, h;
  266.         scanf(" %c %d %d", &c, &x, &h);
  267.         --x;
  268.         if (c == 'N')
  269.         {
  270.             int i = 0;
  271.             int pos = -2;
  272.             while (i < r && pos != -1)
  273.             {
  274.                 pos = findUCol(x, 0, 0, n - 1, pos + 1, h);
  275.                 if (pos != -1)
  276.                 {
  277.                     updateCol(x, 0, 0, n - 1, pos);
  278.                     updateRow(pos, 0, 0, m - 1, x);
  279.                 }
  280.                 ++i;
  281.             }
  282.         }
  283.         if (c == 'S')
  284.         {
  285.             int i = 0;
  286.             int pos = n;
  287.             while (i < r && pos != -1)
  288.             {
  289.                 pos = findDCol(x, 0, 0, n - 1, pos - 1, h);
  290.                 if (pos != -1)
  291.                 {
  292.                     updateCol(x, 0, 0, n - 1, pos);
  293.                     updateRow(pos, 0, 0, m - 1, x);
  294.                 }
  295.                 ++i;
  296.             }
  297.         }
  298.         if (c == 'W')
  299.         {
  300.             int i = 0;
  301.             int pos = -2;
  302.             while (i < r && pos != -1)
  303.             {
  304.                 pos = findLRow(x, 0, 0, m - 1, pos + 1, h);
  305.                 if (pos != -1)
  306.                 {
  307.                     updateRow(x, 0, 0, m - 1, pos);
  308.                     updateCol(pos, 0, 0, n - 1, x);
  309.                 }
  310.                 ++i;
  311.             }
  312.         }
  313.         if (c == 'E')
  314.         {
  315.             int i = 0;
  316.             int pos = m;
  317.             while (i < r && pos != -1)
  318.             {
  319.                 pos = findRRow(x, 0, 0, m - 1, pos - 1, h);
  320.                 if (pos != -1)
  321.                 {
  322.                     updateRow(x, 0, 0, m - 1, pos);
  323.                     updateCol(pos, 0, 0, n - 1, x);
  324.                 }
  325.                 ++i;
  326.             }
  327.         }
  328.     }
  329.     forn(i, n)
  330.         getRow(i, 0, 0, m - 1);
  331.     dp.resize(n);
  332.     forn(i, n)
  333.     {
  334.         dp[i].resize(m);
  335.         forn(j, m)
  336.         {
  337.             dp[i][j] = a[i][j];
  338.             if (i && j)
  339.                 dp[i][j] = (((((dp[i][j] + dp[i - 1][j]) % MM + dp[i][j - 1]) % MM) - dp[i - 1][j - 1]) % MM + MM) % MM;
  340.             if (i && !j)
  341.                 dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MM;
  342.             if (!i && j)
  343.                 dp[i][j] = (dp[i][j] + dp[i][j - 1]) % MM;
  344.             if (i >= p && j >= p)
  345.                 ans = max(ans, (((((((((dp[i][j] + dp[i - p][j - p]) % MM) - dp[i - p][j]) % MM) + MM) % MM) - dp[i][j - p]) % MM) + MM) % MM);
  346.             if (i >= p && j == p - 1)
  347.                 ans = max(ans, ((dp[i][j] - dp[i - p][j]) % MM + MM) % MM);
  348.             if (i == p - 1 && j >= p)
  349.                 ans = max(ans, ((dp[i][j] - dp[i][j - p]) % MM + MM) % MM);
  350.             if (i == p - 1 && j == p - 1)
  351.                 ans = max(ans, dp[i][j]);
  352.         }
  353.     }
  354.     printf("%d", ans);
  355.     return 0;
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement