Submission #163809

# Submission time Handle Problem Language Result Execution time Memory
163809 2019-11-15T12:55:30 Z MohamedAhmed04 Strah (COCI18_strah) C++17
110 / 110
392 ms 67380 KB
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
 
using namespace std ;
 
const int MAX = 2005 ;
const int inf = 1e9 ;
 
char arr[MAX][MAX] ;
long long arr2[MAX][MAX] , calc[MAX][MAX] , lg[MAX];
pair<int , int>Min[12][MAX] ;
int n , m ;
long long ans = 0ll ;
 
pair<int , int>tree[4 * MAX] ;
 
void preprocess(int i)
{
	lg[1] = 0 ;
	for(int j = 2 ; j <= m ; ++j)
		lg[j] = lg[j/2] + 1 ;
	for(int ii = 0 ; ii < 12 ; ++ii)
	{
		for(int j = 0 ; j <= m ; ++j)
			Min[ii][j] = {inf , j} ;
	}
	for(int j = 1 ; j <= m ; ++j)
		Min[0][j] = {arr2[i][j] , j} ;
	for(int j = 1 ; (1 << j) <= m ; ++j)
	{
		for(int ii = 1 ; (ii + (1 << j) - 1) <= m ; ++ii)
			Min[j][ii] = min(Min[j-1][ii] , Min[j-1][ii + (1 << (j-1))]) ;
	}
}
 
pair<int , int>RminQ(int l , int r)
{
	int j = lg[r-l+1] ;
	return min(Min[j][l] , Min[j][r - (1 << j) + 1]) ;
}
 
long long divide_and_conquer(int l , int r , int prv , int i)
{
	if(l > r)
		return 0 ;
	pair<int , int>p = RminQ(l , r) ;
 	long long a = calc[p.first][r-l+1] - calc[prv][r-l+1] ;
	return (a + divide_and_conquer(l , p.second-1 , p.first , i) + divide_and_conquer(p.second+1 , r , p.first , i)) ;
}
long long solve(int i)
{
	preprocess(i) ;
	return divide_and_conquer(1 , m , 0 , i) ;
}
 
int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m ;
	for(int i = 1 ; i <= n ; ++i)
	{
		for(int j = 1 ; j <= m ; ++j)
			cin>>arr[i][j] ;
	}
	calc[1][0] = 0 ;
	for(long long j = 1 ; j <= m ; ++j)
		calc[1][j] = calc[1][j-1] + (j * (j+1ll) / 2ll) ;
	for(long long i = 2 ; i <= n ; ++i)
	{
		for(int j = 1 ; j <= m ; ++j)
			calc[i][j] = calc[i-1][j] + calc[1][j] * i ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		for(int j = 1 ; j <= m ; ++j)
		{
			if(arr[i][j] == '.')
				arr2[i][j] = arr2[i-1][j] + 1 ;
		}
		ans += solve(i) ;
	}
	return cout<<ans<<"\n" , 0 ;
}		
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 508 KB Output is correct
# 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 13 ms 4860 KB Output is correct
2 Correct 12 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 4856 KB Output is correct
2 Correct 12 ms 4776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 4856 KB Output is correct
2 Correct 12 ms 4828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 25336 KB Output is correct
2 Correct 239 ms 51040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 250 ms 44920 KB Output is correct
2 Correct 369 ms 65184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 29048 KB Output is correct
2 Correct 263 ms 54384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 22776 KB Output is correct
2 Correct 283 ms 63228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 340 ms 67380 KB Output is correct
2 Correct 392 ms 67260 KB Output is correct