Submission #197062

#TimeUsernameProblemLanguageResultExecution timeMemory
197062quocnguyen1012Strah (COCI18_strah)C++14
110 / 110
161 ms4316 KiB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back

using namespace std;
typedef long long ll;

const int maxn = 2005;

int N, M;
string str;
int L[maxn], R[maxn], h[maxn];

ll sumto(int x)
{
  return 1ll * x * (x + 1) / 2;
}

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  if (fopen("A.INP", "r")){
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  }
  cin >> N >> M;
  ll res = 0;
  for (int i = 1; i <= N; ++i){
    cin >> str; str = " " + str;
    for (int j = 1; j <= M; ++j){
      if (str[j] == '.') h[j]++;
      else h[j] = 0;
    }
    vector<int> st;
    for (int j = 1; j <= M; ++j){
      while (st.size() && h[st.back()] >= h[j]) st.pop_back();
      if (st.empty()) L[j] = 0;
      else L[j] = st.back();
      st.pb(j);
    }
    st.clear();
    for (int j = M; j >= 1; --j){
      while (st.size() && h[st.back()] > h[j]) st.pop_back();
      if (st.empty()) R[j] = M + 1;
      else R[j] = st.back();
      st.pb(j);
    }
    for (int j = 1; j <= M; ++j){
      int r = R[j] - j;
      int l = j - L[j];
      int tmp = r * sumto(l) + l * sumto(r) - 1ll * l * r;
      res += sumto(h[j]) * tmp;
      //cerr << i << ' ' << j << ' ' << sumto(h[j]) * tmp << '\n';
    }
  }
  cout << res;
}

Compilation message (stderr)

strah.cpp: In function 'int main()':
strah.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.INP", "r", stdin);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
strah.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     freopen("A.OUT", "w", stdout);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...