Submission #848401

# Submission time Handle Problem Language Result Execution time Memory
848401 2023-09-12T12:29:23 Z Charizard2021 Strah (COCI18_strah) C++17
110 / 110
232 ms 8404 KB
#include<bits/stdc++.h>
using namespace std;
long long x(long long k){
    return k * (k + 1)/2;
}
int main(){
    long long n, m;
    cin >> n >> m;
    char grid[n][m];
    for(long long i = 0; i < n; i++){
        for(long long j = 0; j < m; j++){
            cin >> grid[i][j];
        }
    }
    vector<long long> pref(m, 0);
    long long ans = 0;
    for(long long i = 0; i < n; i++){
        for(long long j = 0; j < m; j++){
            if(grid[i][j] == '.'){
                pref[j]++;
            }
            else{
                pref[j] = 0;
            }
        }
        vector<long long> v(m, -1);
        vector<long long> v2(m, m);
        stack<long long> s;
        for(long long j = 0; j < m; j++){
            while(!s.empty() && pref[j] <= pref[s.top()]){
                v2[s.top()] = j;
                s.pop();
            }
            s.push(j);
        }
        while(!s.empty()){
            s.pop();
        }
        for(long long j = m - 1; j >= 0; j--){
            while(!s.empty() && pref[j] < pref[s.top()]){
                v[s.top()] = j;
                s.pop();
            }
            s.push(j);
        }
        for(long long j = 0; j < m; j++){
            long long val = v[j] + 1;
            long long val2 = v2[j] - 1;
            long long length1 = j - val + 1;
            long long length2 = val2 - j + 1;
            ans += (((x(val2 + 1) - x(j)) * length1 - (x(j) - x(val - 1)) * length2) * (x(pref[j])));
        }
    }
    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 5 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 6 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 5 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1368 KB Output is correct
2 Correct 137 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 146 ms 2860 KB Output is correct
2 Correct 213 ms 7444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 1924 KB Output is correct
2 Correct 146 ms 5552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 600 KB Output is correct
2 Correct 165 ms 6084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 4184 KB Output is correct
2 Correct 232 ms 8404 KB Output is correct