danielvitor23

Eyb0ss

Oct 21st, 2020 (edited)
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. /*
  2.     Author: [UFC-QXD] Daniel Vitor Pereira Rodrigues <[email protected]>
  3.     Problem: Eyb0ss
  4.     Link: https://codeforces.com/gym/101915/problem/L
  5.     Origin: ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2017)
  6. */
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. typedef long long i64;
  11. typedef pair<int, int> ii;
  12. typedef pair<i64, i64> ll;
  13. typedef vector<int> vi;
  14. typedef vector<i64> vi64;
  15. typedef vector<ii> vii;
  16. typedef vector<ll> vll;
  17. typedef vector<vi> vvi;
  18.  
  19. const double eps = 1e-9;
  20.  
  21. #define eq(a, b) (abs(a - b) < eps)
  22. #define lt(a, b) ((a + eps) < b)
  23. #define gt(a, b) (a > (b + eps))
  24. #define le(a, b) (a < (b + eps))
  25. #define ge(a, b) ((a + eps) > b)
  26.  
  27. #define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
  28. #define all(x) (x).begin(), (x).end()
  29. #define rall(x) (x).rbegin(), (x).rend()
  30. #define ms(a, x) memset(a, x, sizeof(a))
  31. #define len(x) (int)(x).size()
  32. #define pb push_back
  33. #define eb emplace_back
  34. #define fi first
  35. #define se second
  36.  
  37. const int dtx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
  38. const int dty[] = {-1, 1,  0, 0, 1, -1, -1,  1};
  39. const int dtxc[] = {1,  1, 2,  2, -1, -1, -2, -2};
  40. const int dtyc[] = {2, -2, 1, -1,  2, -2,  1, -1};
  41.  
  42. const double pi = acos(-1.0);
  43. const int inf = 0x3f3f3f3f;
  44. const int maxn = 5e2+5;
  45. const int mod = 1e9+7;
  46.  
  47. int tc;
  48.  
  49. int n;
  50. i64 ans;
  51. vvi ar;
  52.  
  53. i64 query(bool t) {
  54.     i64 query_answer = 0LL;
  55.     for (int r1 = 1; r1 <= n; ++r1) {
  56.         vi col(n+1, (t ? inf : -inf));
  57.  
  58.         for (int r2 = r1; r2 <= n; ++r2) {
  59.             for (int i = 1; i <= n; ++i) {
  60.                 if (t) col[i] = min(col[i], ar[r2][i]);
  61.                 else col[i] = max(col[i], ar[r2][i]);
  62.             }
  63.  
  64.             vi left(n+1, 0), right(n+1, n+1);
  65.  
  66.             stack<int> st;
  67.             for (int i = 1; i <= n; ++i) {
  68.                 while (!st.empty() and (t ? col[i] <= col[st.top()]: col[i] >= col[st.top()])) {
  69.                     right[st.top()] = i;
  70.                     st.pop();
  71.                 }
  72.                 st.push(i);
  73.             }
  74.  
  75.             while (!st.empty()) st.pop();
  76.  
  77.             for (int i = n; i >= 1; --i) {
  78.                 while (!st.empty() and (t ? col[i] < col[st.top()]: col[i] > col[st.top()])) {
  79.                     left[st.top()] = i;
  80.                     st.pop();
  81.                 }
  82.                 st.push(i);
  83.             }
  84.  
  85.             for (int i = 1; i <= n; ++i) {
  86.                 query_answer += 1LL * col[i] * (i - left[i]) * (right[i] - i);
  87.             }
  88.         }
  89.     }
  90.     return query_answer;
  91. }
  92.  
  93. int main() {
  94.     fastIO();
  95.     cin >> tc;
  96.     while (tc--) {
  97.         cin >> n;
  98.         ar = vvi(n+1, vi(n+1));
  99.  
  100.         for (int i = 1; i <= n; ++i) {
  101.             for (int j = 1; j <= n; ++j) {
  102.                 cin >> ar[i][j];
  103.             }
  104.         }
  105.  
  106.         ans = query(0);
  107.  
  108.         ans -= query(1);
  109.  
  110.         cout << ans << '\n';
  111.     }
  112.     return 0;
  113. }
  114.  
Add Comment
Please, Sign In to add comment