#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair <int,int> ii;
const int N = 1e3 + 9;
const int oo = 1e10;
int n,m,a[N][N],high[N][N],l[N],r[N],res = 0;
void solve(int a[],int h[],bool kt)
{
stack <int> s;
h[0] = -oo;
s.push(0);
for (int i = 1;i <= m;i++)
{
while (h[i] < h[s.top()] && a[i] == a[s.top()]) s.pop();
l[i] = s.top() + 1;
s.push(i);
}
while (s.size()) s.pop();
s.push(m + 1);
h[m + 1] = -oo;
for (int i = m;i >= 1;i--)
{
while (h[i] <= h[s.top()] && a[i] == a[s.top()]) s.pop();
r[i] = s.top() - 1;
s.push(i);
}
int lim_r;
int tmp = res;
for (int i = 1;i <= m;i++)
{
// if (kt) tmp = res;
tmp = res;
res += (i - l[i] + 1) * (r[i] - i + 1);
if (a[r[i] + 1] != a[i] || r[i] == m) lim_r = 1;
else lim_r = h[r[i] + 1] + 1;
res += (h[i] - lim_r) * (r[i] - i + 1) + (lim_r - 1) * (r[i] - i + 1);
// if (kt) cout<<i<<" "<<l[i]<<" "<<r[i]<<" "<<lim_r<<" "<<res - tmp<<endl;
}
// cout<<res-tmp<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("test.inp","r",stdin);
cin>>n>>m;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
cin>>a[i][j];
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (a[i][j] != a[i-1][j]) high[i][j] = 1;
else high[i][j] = high[i-1][j] + 1;
for (int i = 1;i <= n;i++)
solve(a[i],high[i],(i == 3));
cout<<res;
return 0;
}
Compilation message
bob.cpp: In function 'void solve(long long int*, long long int*, bool)':
bob.cpp:29:9: warning: variable 'tmp' set but not used [-Wunused-but-set-variable]
29 | int tmp = res;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
8232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
8228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
8164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
8144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
107 ms |
16172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
119 ms |
16056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
16092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
123 ms |
16128 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |