Submission #95406

# Submission time Handle Problem Language Result Execution time Memory
95406 2019-01-31T16:11:25 Z dalgerok Strah (COCI18_strah) C++14
110 / 110
232 ms 55368 KB
#include<bits/stdc++.h>
using namespace std;



const int N = 2005;



int n, m, h[N][N], le[N], ri[N];
long long dp[N][N];
char a[N][N];
vector < int > st;

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    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 <= m; i++){
        h[1][i] = (a[1][i] == '.');
    }
    for(int i = 2; i <= n; i++){
        for(int j = 1; j <= m; j++){
            h[i][j] = (a[i][j] == '#' ? 0 : h[i - 1][j] + 1);
        }
    }
    for(int i = 1; i <= n; i++){
        st.clear();
        for(int j = 1; j <= m; j++){
            while(!st.empty() && h[i][st.back()] > h[i][j]){
                st.pop_back();
            }
            if(st.empty()){
                le[j] = 0;
            }
            else{
                le[j] = st.back();
            }
            st.push_back(j);
        }
        st.clear();
        for(int j = m; j >= 1; j--){
            while(!st.empty() && h[i][st.back()] >= h[i][j]){
                st.pop_back();
            }
            if(st.empty()){
                ri[j] = m + 1;
            }
            else{
                ri[j] = st.back();
            }
            st.push_back(j);
        }
        for(int j = 1; j <= m; j++){
            dp[h[i][j]][ri[j] - j] += 1;
            dp[h[i][j]][j - le[j]] += 1;
            dp[h[i][j]][ri[j] - le[j]] -= 1;
            dp[h[i][j]][0] -= 1;
        }
    }
    for(int i = 1; i <= n + 1; i++){
        for(int j = 1; j <= m + 1; j++){
            dp[i][j] += dp[i][j - 1];
        }
    }
    for(int i = n; i >= 1; i--){
        for(int j = m; j >= 1; j--){
            dp[i][j] += dp[i + 1][j] + dp[i][j + 1] - dp[i + 1][j + 1];
        }
    }
    long long ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ans += 1LL * dp[i][j] * i * j;
        }
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4472 KB Output is correct
2 Correct 8 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 4472 KB Output is correct
2 Correct 9 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4472 KB Output is correct
2 Correct 9 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 21152 KB Output is correct
2 Correct 146 ms 41408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 36844 KB Output is correct
2 Correct 215 ms 53468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 23672 KB Output is correct
2 Correct 155 ms 44124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 21928 KB Output is correct
2 Correct 169 ms 51248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 55368 KB Output is correct
2 Correct 232 ms 55288 KB Output is correct