Submission #197062

# Submission time Handle Problem Language Result Execution time Memory
197062 2020-01-18T12:17:25 Z quocnguyen1012 Strah (COCI18_strah) C++14
110 / 110
161 ms 4316 KB
#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

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 time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 1500 KB Output is correct
2 Correct 93 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 2924 KB Output is correct
2 Correct 149 ms 4104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 2040 KB Output is correct
2 Correct 108 ms 3208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 504 KB Output is correct
2 Correct 99 ms 3292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 122 ms 4316 KB Output is correct
2 Correct 161 ms 4244 KB Output is correct