lalalalalalalaalalla

Untitled

Feb 3rd, 2021
861
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <cmath>
  10. #include <time.h>
  11. #include <random>
  12. #include <string>
  13. #include <cassert>
  14. #include <vector>
  15. #include <ostream>
  16. #include <istream>
  17. #include <stack>
  18. #include <deque>
  19. #include <queue>
  20. #include <functional>
  21. #include <chrono>
  22. #include <stack>
  23. #include <limits>
  24.  
  25. using namespace std;
  26.  
  27. #define int long long
  28. #define pb push_back
  29. #define all(a) (a).begin(), (a).end()
  30. #define pii pair<int, int>
  31. #define ld long double
  32.  
  33. istream& operator>> (istream& in, pii& b) {
  34.     in >> b.first >> b.second;
  35.     return in;
  36. }
  37.  
  38. ostream& operator<< (ostream& out, const pii& b) {
  39.     out << "{" << b.first << ", " << b.second << "}";
  40.     return out;
  41. }
  42.  
  43. template<typename T> ostream& operator<< (ostream& out, const vector<T>& a) {
  44.     for (auto k : a) out << k << " ";
  45.     return out;
  46. }
  47.  
  48. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  49. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  50.  
  51. #ifdef LOCAL
  52.     #define dbg(x) cout << #x << " : " << (x) << endl;
  53.     const long long INF = 1e18;
  54.     // const long long mod = 2600000069;
  55.     // const long long p = 10;
  56. #else
  57.     #define dbg(x) 57
  58.     const long long INF = 1e18;
  59.     // const long long mod = 2600000069;  
  60.     // const long long p = 179;
  61. #endif
  62.  
  63. const ld PI = 4 * atan(1);
  64.  
  65. #define time clock() / (double) CLOCKS_PER_SEC
  66.  
  67. // #pragma GCC optimize("Ofast,no-stack-protector")
  68. // #pragma GCC target("sse,sse2,sse3,sse3,sse4")
  69. // #pragma GCC optimize("unroll-loops")
  70. // #pragma GCC optimize("fast-math")
  71. // #pragma GCC target("avx2")
  72.  
  73. mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
  74.  
  75. const int N = 2010;
  76.  
  77. int n, m, k;
  78. int c[N];
  79. int a[N][N];
  80. int up[N][N], down[N][N], lft[N][N];
  81. int opt[N][N], curans[N][N];
  82.  
  83. int t[8 * N], mod[8 * N];
  84.  
  85. void build() {
  86.     for (int i = 0; i < 8 * N; i++) {
  87.         t[i] = -INF;
  88.         mod[i] = 0;
  89.     }
  90. }
  91.  
  92. void push(int v) {
  93.     if (mod[v]) {
  94.         t[v] += mod[v];
  95.         mod[2 * v + 1] += mod[v];
  96.         mod[2 * v + 2] += mod[v];
  97.         mod[v] = 0;
  98.     }
  99. }
  100.  
  101. void add(int v, int l, int r, int askl, int askr, int x) {
  102.     push(v);
  103.     if (l >= askr || r <= askl) return;
  104.     if (l >= askl && r <= askr) {
  105.         mod[v] += x;
  106.         push(v);
  107.         return;
  108.     }
  109.     int m = (l + r) / 2;
  110.     add(2 * v + 1, l, m, askl, askr, x);
  111.     add(2 * v + 2, m, r, askl, askr, x);
  112.     t[v] = max(t[2 * v + 1], t[2 * v + 2]);
  113. }
  114.  
  115. void setval(int v, int l, int r, int pos, int val) {
  116.     push(v);
  117.     if (l + 1 == r) {
  118.         t[v] = val;
  119.         return;
  120.     }
  121.     int m = (l + r) / 2;
  122.     if (pos < m) setval(2 * v + 1, l, m, pos, val);
  123.     else setval(2 * v + 2, m, r, pos, val);
  124.     t[v] = max(t[2 * v + 1], t[2 * v + 2]);
  125. }
  126.  
  127. int ans = -INF;
  128.  
  129. void D(int v, int l, int r, int pos) {
  130.     push(v);
  131.     if (l + 1 == r) {
  132.         if (l <= pos) cout << t[v] << " ";
  133.         return;
  134.     }
  135.     int m = (l + r) / 2;
  136.     D(2 * v + 1, l, m, pos);
  137.     D(2 * v + 2, m, r, pos);
  138. }
  139.  
  140. signed main() {
  141.     ios_base::sync_with_stdio(0);
  142.     cin.tie(0);
  143.     cout.tie(0);
  144.     #ifndef LOCAL
  145.         freopen("submarine.in", "r", stdin);
  146.         freopen("submarine.out", "w", stdout);
  147.     #endif
  148.     cin >> k;
  149.     for (int i = 0; i < k; i++) {
  150.         cin >> c[i];
  151.     }
  152.     cin >> n >> m;
  153.     for (int i = 0; i < n; i++) {
  154.         for (int j = 0; j < m; j++) {
  155.             char ch;
  156.             cin >> ch;
  157.             a[i][j] = c[ch - 'a'];
  158.         }
  159.     }
  160.     for (int i = 1; i < n; i++) {
  161.         for (int j = 0; j < m; j++) {
  162.             up[i][j] = max(0ll, up[i - 1][j] + a[i - 1][j]);
  163.         }
  164.     }
  165.     for (int i = n - 2; i >= 0; i--) {
  166.         for (int j = 0; j < m; j++) {
  167.             down[i][j] = max(0ll, down[i + 1][j] + a[i + 1][j]);
  168.         }
  169.     }
  170.     for (int j = 1; j < m; j++) {
  171.         for (int i = 0; i < n; i++) {
  172.             lft[i][j] = max(0ll, lft[i][j - 1] + a[i][j - 1]);
  173.         }
  174.     }
  175.     vector<pii> s; // {pos, opt}
  176.     // vector<int> t1, t2;
  177.     for (int i = 0; i < n; i++) {
  178.         s.clear();
  179.         build();
  180.         // t1.assign(m, -INF);
  181.         // t2.assign(m, -INF);
  182.         for (int j = 1; j < m; j++) {
  183.             while (!s.empty() && up[i][j] + down[i][j] >= s.back().second) {
  184.                 int l, r = s.back().first - 1, was = s.back().second;
  185.                 s.pop_back();
  186.                 if (s.empty()) {
  187.                     l = 0;
  188.                 } else {
  189.                     l = s.back().first;
  190.                 }
  191.                 // for (int k = l; k <= r; k++) {
  192.                 //     t1[k] += up[i][j] + down[i][j] - was;
  193.                 // }
  194.                 add(0, 0, m, l, r + 1, up[i][j] + down[i][j] - was);
  195.             }
  196.             s.pb({j, up[i][j] + down[i][j]});
  197.             // t1[j - 1] = up[i][j] + down[i][j];
  198.             // t1[j - 1] += lft[i][j - 1] + up[i][j - 1] + a[i][j - 1];
  199.             setval(0, 0, m, j - 1, up[i][j] + down[i][j] + lft[i][j - 1] + up[i][j - 1] + a[i][j - 1]);
  200.             // for (int k = 0; k < j; k++) {
  201.             //     t1[k] += a[i][j];
  202.             // }
  203.             add(0, 0, m, 0, j, a[i][j]);
  204.             // for (int k = j - 1; k >= 0; k--) {
  205.             //     if (!chkmax(opt[i][k], up[i][j] + down[i][j])) break;
  206.             // }
  207.             // curans[i][j - 1] = lft[i][j - 1] + up[i][j - 1] + a[i][j - 1];
  208.             // for (int k = 0; k < j; k++) {
  209.             //     curans[i][k] += a[i][j];
  210.             //     chkmax(ans, curans[i][k] + opt[i][k]);
  211.             // }
  212.             chkmax(ans, t[0] + mod[0]);
  213.             // cout << i << " " << j << "\n";
  214.             // for (int k = 0; k < j; k++) {
  215.             //     cout << curans[i][k] + opt[i][k] << " ";
  216.             // }
  217.             // cout << "\n";
  218.             // for (int k = 0; k < j; k++) {
  219.             //     cout << t1[k] << " ";
  220.             // }
  221.             // cout << "\n--\n";
  222.             // for (int k = 0; k < j; k++) {
  223.             //     cout << curans[i][k] << " ";
  224.             // }
  225.             // cout << "\n";
  226.             // for (int k = 0; k < j; k++) {
  227.             //     cout << t2[k] << " ";
  228.             // }
  229.             // cout << "\n-----------\n";
  230.         }
  231.     }
  232.     cout << ans << "\n";
  233. }
  234. /*
  235. 2
  236. -10 1
  237. 6 11
  238. aaaaaaaaaaa
  239. aaabaaaaaaa
  240. aaabaaaabaa
  241. abbbbbbbbba
  242. aaaaaaaabaa
  243. aaaaaaaaaaa
  244. -> 13
  245.  
  246. 3
  247. -4 -3 4
  248. 5 5
  249. bbabc
  250. ccaac
  251. accba
  252. baccb
  253. baaaa
  254. -> 16
  255.  
  256. 3
  257. -2 4 0
  258. 5 5
  259. abccb
  260. cccac
  261. cbcba
  262. cccbb
  263. accba
  264. -> 24
  265.  
  266. 4
  267. -1 -5 -3 0
  268. 5 5
  269. bbabc
  270. ccaac
  271. acdba
  272. baccb
  273. baaaa
  274. -> -2
  275. */
  276.  
Advertisement
Add Comment
Please, Sign In to add comment