fork(1) download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 1234567890
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-8
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. #define dbl long double
  28. using namespace std;
  29. // mylittledoge
  30.  
  31. int main() {
  32. cin.sync_with_stdio(0);
  33. cin.tie(0);
  34. int N,M;
  35. cin >> N >> M;
  36. vector< vector<int> > V(N,vector<int>(M));
  37. for(int i =0; i < N; i++) for(int j =0; j < M; j++) cin >> V[i][j];
  38.  
  39. vector<int> H(M,0);
  40. long long ans =0;
  41. for(int i =0; i < N; i++) {
  42. stack< pair<int,int> > S;
  43. S.push(make_pair(0,-1));
  44. for(int j =0; j < M; j++) {
  45. H[j]++;
  46. if(i > 0 && V[i][j] != V[i-1][j]) H[j] =1;
  47. int q =j;
  48. if(j > 0 && V[i][j] != V[i][j-1]) {
  49. while(S.size() > 1) {
  50. pair<int,int> p =S.top(); S.pop();
  51. ans +=1LL*(j-p.ss)*(j-p.ss+1)/2*1LL*(p.ff-S.top().ff);}
  52. S.pop();
  53. S.push(make_pair(0,j-1));}
  54. while(S.top().ff > H[j]) {
  55. pair<int,int> p =S.top(); S.pop();
  56. q =p.ss;
  57. ans +=1LL*(j-p.ss)*(j-p.ss+1)/2*1LL*(p.ff-max(H[j],S.top().ff));}
  58. if(S.top().ff != H[j]) S.push(make_pair(H[j],q));}
  59. while(S.top().ff > 0) {
  60. pair<int,int> p =S.top(); S.pop();
  61. ans +=1LL*(M-p.ss)*(M-p.ss+1)/2*1LL*(p.ff-S.top().ff);}
  62. }
  63.  
  64. cout << ans << "\n";
  65. return 0;}
  66.  
  67. // look at my code
  68. // my code is amazing
Success #stdin #stdout 0s 3436KB
stdin
5 3
2 2 2
2 2 1
1 1 1
2 1 2
1 2 1
stdout
27