Submission #211115

# Submission time Handle Problem Language Result Execution time Memory
211115 2020-03-19T08:55:23 Z Fasho Bob (COCI14_bob) C++14
120 / 120
196 ms 49528 KB
#include <bits/stdc++.h>
#define N 1005
#define ll long long int 	
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int,int>
#define lli pair<ll,ll>
#define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
#define fast2 freopen ("badhair.gir","r",stdin);freopen ("badhair.cik","w",stdout);
#define mod 1000000007
#define fs(x,y) for(ll i=1;i<=y;i++) cin>>x[i]
#define fo(i,x,y) for(ll i=x;i<=y;i++)
#define INF 1000000000005
#define ull unsigned long long int
using namespace std;

ll n,m,ar[N][N],sum,t,hei[N][N],ilu[N][N],ans[N][N],tut2[N][N];

stack<int> st;

void pre()
{
	fo(i,1,n)
	{
		fo(j,1,m)
		{
			hei[i][j]=hei[i-1][j];
			if(ar[i][j]!=ar[i-1][j])
				hei[i][j]=0;
			hei[i][j]++;
		}
	}
	fo(i,1,n)
	{
		while(st.size())
			st.pop();
		fo(j,1,n)
		{
			while(st.size() && hei[i][st.top()]>=hei[i][j])
				st.pop();
			if(st.size())
				ilu[i][j]=st.top();
			else
				ilu[i][j]=0;
			st.push(j);
		}
	}
	fo(i,1,n)
	{
		fo(j,1,m)
		{
			tut2[i][j]=tut2[i][j-1];
			if(ar[i][j]!=ar[i][j-1])
				tut2[i][j]=j-1;
		}
	}
}


int main()
{
	fast;
	cin>>n>>m;
	fo(i,1,n)
		fo(j,1,m)
			cin>>ar[i][j];

	pre();

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			ll x=max(ilu[i][j],tut2[i][j]);
			ll top=hei[i][j]*(j-x);
			ans[i][j]=top;
			if(ar[i][x]==ar[i][j])
			ans[i][j]+=ans[i][x];
		}
	}
	// cout<<endl;
	// fo(i,1,n)
	// {
	// 	fo(j,1,m)
	// 		cout<<hei[i][j]<<sp;
	// 	cout<<endl;
	// }

	// 	cout<<endl;
	// fo(i,1,n)
	// {
	// 	fo(j,1,m)
	// 		cout<<ilu[i][j]<<sp;
	// 	cout<<endl;
	// }
	// 	cout<<endl;
	// fo(i,1,n)
	// {
	// 	fo(j,1,m)
	// 		cout<<tut2[i][j]<<sp;
	// 	cout<<endl;
	// }
	// 	cout<<endl;
	// fo(i,1,n)
	// {
	// 	fo(j,1,m)
	// 		cout<<ans[i][j]<<sp;
	// 	cout<<endl;
	// }
	// cout<<endl;
	fo(i,1,n)
	fo(j,1,m)
		sum+=ans[i][j];
	cout<<sum;


}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 20600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 20984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 21240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 21240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 46432 KB Output is correct
2 Correct 96 ms 41720 KB Output is correct
3 Correct 94 ms 41720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 196 ms 49528 KB Output is correct
2 Correct 93 ms 41724 KB Output is correct
3 Correct 97 ms 41592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 191 ms 49272 KB Output is correct
2 Correct 94 ms 41648 KB Output is correct
3 Correct 92 ms 41720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 49400 KB Output is correct
2 Correct 95 ms 41720 KB Output is correct
3 Correct 95 ms 41488 KB Output is correct