#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#include <utility>
using namespace std;
int n, m, grid[1001][1001] = {}, nl[1001][1001] = {}, nu[1001][1001] = {}, dp[1001][1001] = {};
map<int, int> mps[1001];
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> grid[i][j]; nl[i][j] = 1; nu[i][j] = 1;
if (grid[i][j] == grid[i][j-1]){
nl[i][j] += nl[i][j-1];
}
if (grid[i][j] == grid[i-1][j]){
nu[i][j] += nu[i-1][j];
}
}
}
for (int r = 1; r <= n; r++){
dp[r][1] = nu[r][1]; mps[r][dp[r][1]] = 1;
}
for (int c = 2; c <= m; c++){
for (int r = 1; r <= n; r++){
if (grid[r][c] != grid[r][c-1]){
mps[r].clear();
}
dp[r][c] = nu[r][c] * nl[r][c];
if (mps[r].size() > 0){
map<int, int>::iterator lb = mps[r].lower_bound(nu[r][c]);
if (lb != mps[r].begin()){
lb--; dp[r][c] -= nl[r][lb->second] * nu[r][c]; dp[r][c] += dp[r][lb->second];
}
}
mps[r][nu[r][c]] = c; mps[r].erase(mps[r].upper_bound(nu[r][c]), mps[r].end());
}
}
long ans = 0;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
ans += dp[i][j];
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
8832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
9208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
9080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
16888 KB |
Output is correct |
2 |
Correct |
131 ms |
18424 KB |
Output is correct |
3 |
Correct |
116 ms |
18040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
16632 KB |
Output is correct |
2 |
Correct |
143 ms |
18168 KB |
Output is correct |
3 |
Correct |
113 ms |
18296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
16632 KB |
Output is correct |
2 |
Correct |
116 ms |
18040 KB |
Output is correct |
3 |
Correct |
115 ms |
18044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
224 ms |
16760 KB |
Output is correct |
2 |
Correct |
144 ms |
18680 KB |
Output is correct |
3 |
Correct |
115 ms |
18040 KB |
Output is correct |