Submission #163722

# Submission time Handle Problem Language Result Execution time Memory
163722 2019-11-14T19:27:04 Z beso123 Bob (COCI14_bob) C++14
120 / 120
208 ms 28280 KB
#include<bits/stdc++.h>
using namespace std;
const int N=1e3 + 100;
const int mod=1e9 + 7;
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second 
#define mp make_pair
int grid[N][N], ans[N][N], len[N][N];
signed main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int n, m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) cin>>grid[i][j];
	}
	for(int i=1;i<=n;i++)
	{
		int prev=m, type=grid[i][m];
		for(int j=m;j>=1;j--)
		{
			if(type!=grid[i][j])
			{
				prev=j;type=grid[i][j];
			}
			len[i][j]=prev - j+1;
		}
	}
	int output=0;
	for(int j=1;j<=m;j++)
	{
		stack<int> st;int curt=grid[1][j], prev=1;
		for(int i=1;i<=n;i++)
		{
			while(!st.empty()&&((curt==grid[i][j]&&len[st.top()][j]>len[i][j])||curt!=grid[i][j]))
			{
				st.pop();
			}
			if(grid[i][j]!=curt)
			{
				curt=grid[i][j];prev=i;
			}
			if(!st.empty())
			{
				ans[i][j]=len[i][j]*(i-st.top()) + ans[st.top()][j];
			}
			else
			{
				ans[i][j]=len[i][j]*(i-prev + 1);
			}
			st.push(i);output+=ans[i][j];
		}
	}
	cout<<output;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 12152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 12280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 12252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 26388 KB Output is correct
2 Correct 125 ms 28280 KB Output is correct
3 Correct 122 ms 28152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 26124 KB Output is correct
2 Correct 124 ms 28100 KB Output is correct
3 Correct 120 ms 28128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 205 ms 26188 KB Output is correct
2 Correct 121 ms 28124 KB Output is correct
3 Correct 135 ms 28056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 26212 KB Output is correct
2 Correct 124 ms 28152 KB Output is correct
3 Correct 127 ms 28280 KB Output is correct