Submission #639575

# Submission time Handle Problem Language Result Execution time Memory
639575 2022-09-10T16:34:30 Z CaoHuuKhuongDuy Bob (COCI14_bob) C++14
120 / 120
136 ms 25844 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long 
typedef pair <int,int> ii;
const int N = 1e3 + 9;
const int oo = 1e10;
int n,m,a[N][N],high[N][N],l[N],r[N],res = 0;
void solve(int a[],int h[],bool kt)
{
    stack <int> s;
    h[0] = -oo;
    s.push(0);
    for (int i = 1;i <= m;i++)
      {
        while (h[i] < h[s.top()] && a[i] == a[s.top()]) s.pop();
        l[i] = s.top() + 1;
        s.push(i);
      }
    while (s.size()) s.pop();
    s.push(m + 1);
    h[m + 1] = -oo;
    for (int i = m;i >= 1;i--)
      {
        while (h[i] <= h[s.top()] && a[i] == a[s.top()]) s.pop();
        r[i] = s.top() - 1;
        s.push(i);
      }
    for (int i = 1;i <= m;i++)
      res += (i - l[i] + 1) * (r[i] - i + 1) * h[i] ;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // freopen("test.inp","r",stdin);
    // freopen("check1.out","w",stdout);
    cin>>n>>m;
    for (int i = 1;i <= n;i++)
      for (int j = 1;j <= m;j++)
        cin>>a[i][j];
    for (int i = 1;i <= n;i++)
      for (int j = 1;j <= m;j++)
        if (a[i][j] != a[i-1][j]) high[i][j] = 1;
        else high[i][j] = high[i-1][j] + 1;
    for (int i = 1;i <= n;i++)
      solve(a[i],high[i],(i == 2));
    cout<<res;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 9188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 9304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 9300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 22704 KB Output is correct
2 Correct 65 ms 17968 KB Output is correct
3 Correct 63 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 25844 KB Output is correct
2 Correct 77 ms 18088 KB Output is correct
3 Correct 105 ms 18076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 25652 KB Output is correct
2 Correct 71 ms 18040 KB Output is correct
3 Correct 61 ms 17992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 25788 KB Output is correct
2 Correct 80 ms 18060 KB Output is correct
3 Correct 102 ms 17976 KB Output is correct