답안 #320243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
320243 2020-11-08T06:01:11 Z monus1042 Strah (COCI18_strah) C++17
110 / 110
142 ms 4824 KB
// NK
#include <bits/stdc++.h>
using namespace std;
 
typedef pair<int,int> ii;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define pb push_back
#define eb emplace_back
#define pob pop_back
#define psf push_front
#define pof pop_front
#define mkp make_pair
#define mkt make_tuple
#define all(x) x.begin(), x.end()
#define Bolivia_is_nice ios::sync_with_stdio(false), cin.tie(nullptr)
 
const int MAXS = 2010;
char ma[MAXS][MAXS];
ll triacc[MAXS], tri[MAXS];
int helper[MAXS];
int n,m;
 
ll f(ll x){
	return x*(x+1) / 2ll;
}
 
void solve(){
	cin>>n>>m;
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++)
			cin>>ma[i][j];
 
	for (ll i=1; i<MAXS; i++) triacc[i] = triacc[i-1] + i*(i+1) / 2ll, tri[i] = tri[i-1] + i;
 
 
	ll ans = 0;
	for (int i=1; i<=n; i++){
		for (int j=1; j<=m; j++){
			if (ma[i][j] == '.'){
				helper[j]++;
			}else helper[j] = 0;
		}
 
		stack<pair<int,ll>> st; // elem, counter
		st.push(mkp(0,0));
		for (int j=1; j<=m; j++){
			//cout<<helper[j];
			ll aux = 0;
			while (st.top().first > helper[j]){
				aux = st.top().second;
				int h = st.top().first;
				st.pop();
				int bh = st.top().first;
 
				ll diff = tri[h];
				int maxi = max(bh, helper[j]);
				diff -= tri[maxi];
				ll tmp = diff * (triacc[aux]);
				ans = ans + tmp;
 
				st.top().second += aux;
			}
			if (st.top().first < helper[j]){
				st.top().second -= aux;
				st.push(mkp(helper[j], aux+1));
			}else{
				st.top().second++;
			}
		}
		//pop all:
		ll aux = 0;
		while(st.top().first > 0){
			aux = st.top().second;
			int h = st.top().first;
			st.pop();
			int bh = st.top().first;
 
			ll diff = tri[h];
			int maxi = bh;
			diff -= tri[maxi];
			ll tmp = diff * (triacc[aux]);
			ans = ans + tmp;
 
			st.top().second += aux;
		}
		//cout<<'\n';
	}
	cout<<ans<<'\n';
}
 
int main(){
	Bolivia_is_nice;
	int t = 1; //cin>>t;
	while(t--)
	solve();
	
    return 0;
}
/*
  ~/.emacs
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 480 KB Output is correct
2 Correct 1 ms 736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1240 KB Output is correct
2 Correct 4 ms 1240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1348 KB Output is correct
2 Correct 4 ms 1444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1552 KB Output is correct
2 Correct 4 ms 1548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 2600 KB Output is correct
2 Correct 81 ms 3776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 84 ms 3392 KB Output is correct
2 Correct 124 ms 4672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 2504 KB Output is correct
2 Correct 96 ms 4032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 4424 KB Output is correct
2 Correct 107 ms 4544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 4804 KB Output is correct
2 Correct 134 ms 4824 KB Output is correct