Advertisement
ATSTNG

Problem D

Feb 16th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  1. #pragma comment(linker, "/STACK: 40000000")
  2.  
  3. #define M_PI 3.14159265358979323846
  4. #define M_PI_2 1.57079632679489661923
  5.  
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <string>
  10. #include <complex>
  11. #include <queue>
  12. #include <vector>
  13. #include <tuple>
  14. #include <set>
  15. #include <map>
  16. #include <cmath>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19.  
  20. #define INF 1000000000000000099
  21. #define BLIFT 17
  22. #define SIZE 1005
  23. #define GSIZE 2000000
  24. #define SEP -77
  25. #define ENCODE 1000000
  26. #define ENCODE_2 500000
  27. #define MOD 1000000007
  28.  
  29. #define DBG false
  30. #define lli long long int
  31. #define mp make_pair
  32. #define cpxd complex<double>
  33. #define compCount 1000001
  34.  
  35. #define DLOG(x) cout << #x << ": " << x << endl;
  36. //#undef DLOG(x)
  37. //#define DLOG(x) ;
  38.  
  39. #define int long long int
  40. #define double long double
  41.  
  42. #define EPS 0.000000000001
  43.  
  44. using namespace std;
  45.  
  46. vector<int> graph[SIZE];
  47.  
  48. int least_prime_divisor[SIZE];
  49. vector<int> primes;
  50.  
  51. struct Node {
  52.     Node* next;
  53.     int row;
  54.     int col;
  55. };
  56.  
  57. int mtx[SIZE][SIZE];
  58. Node* ladder[SIZE][SIZE];
  59. int answer[SIZE][SIZE];
  60. int jumpdownrow[SIZE][SIZE]; /// row, col => maximum row going down to goes only by the same number
  61.  
  62. vector<Node*> garbage;
  63.  
  64. void pritchard() {
  65.     for (int i = 2; i < SIZE; i++) {
  66.         if (least_prime_divisor[i] == 0) {
  67.             primes.push_back(i);
  68.             least_prime_divisor[i] = i;
  69.         }
  70.  
  71.         for (auto subprime : primes) {
  72.             if (subprime > least_prime_divisor[i]
  73.              || i*subprime >= SIZE) break;
  74.  
  75.             least_prime_divisor[i * subprime] = subprime;
  76.         }
  77.     }
  78. }
  79.  
  80. int fpow (int base, int exp) {
  81.     if (exp == 0) return 1;
  82.     if (exp == 1) return base;
  83.  
  84.     if (exp & 1) {
  85.         return base * fpow(base*base, exp >> 1);
  86.     } else {
  87.         return fpow(base*base, exp >> 1);
  88.     }
  89. }
  90.  
  91. void test() {
  92.     int row_amount, col_amount;
  93.     cin >> row_amount >> col_amount;
  94.  
  95.     int last_row = row_amount-1;
  96.     int last_col = col_amount-1;
  97.  
  98.     for (int row = 0; row < row_amount; row++) {
  99.         for (int col = 0; col < col_amount; col++) {
  100.             cin >> mtx[row][col];
  101.         }
  102.     }
  103.  
  104.     for (int row = row_amount-1; row >= 0; row--) {
  105.         for (int col = 0; col < col_amount; col++) {
  106.             if (row == row_amount-1 || mtx[row][col] != mtx[row+1][col]) {
  107.                 jumpdownrow[row][col] = row;
  108.             } else {
  109.                 jumpdownrow[row][col] = jumpdownrow[row+1][col];
  110.             }
  111.         }
  112.     }
  113.  
  114.     /*
  115.     cout << endl;
  116.     for (int row = 0; row < row_amount; row++) {
  117.         for (int col = 0; col < col_amount; col++) {
  118.             cout << jumpdownrow[row][col] << " ";
  119.         }
  120.         cout << endl;
  121.     }
  122.     cout << endl;
  123.     //*/
  124.  
  125.     for (int row = 0; row < row_amount; row++) {
  126.         for (int col = col_amount-1; col >= 0; col--) {
  127.             Node* new_node = new Node;
  128.             new_node->next = nullptr;
  129.             new_node->col = col;
  130.             new_node->row = jumpdownrow[row][col];
  131.  
  132.             garbage.push_back(new_node);
  133.  
  134.             answer[row][col] = jumpdownrow[row][col] - row + 1;
  135.             ladder[row][col] = new_node;
  136.  
  137.             if (col+1 < col_amount && mtx[row][col] == mtx[row][col+1]) {
  138.                 new_node->next = ladder[row][col+1];
  139.                 answer[row][col] += answer[row][col+1];
  140.  
  141.                 Node* cur_node = ladder[row][col+1];
  142.                 int prev_col = col;
  143.                 while (cur_node != nullptr && cur_node->row > jumpdownrow[row][col]) {
  144.                     answer[row][col] -= (cur_node->row - jumpdownrow[row][col])*(cur_node->col - prev_col);
  145.                     cur_node->row = jumpdownrow[row][col];
  146.                     prev_col = cur_node->col;
  147.                     cur_node = cur_node->next;
  148.                 }
  149.  
  150.                 while (ladder[row][col]->next != nullptr
  151.                     && ladder[row][col]->row == ladder[row][col]->next->row) {
  152.                     Node* next_node = ladder[row][col]->next;
  153.                     ladder[row][col] = next_node;
  154.                 }
  155.             }
  156.         }
  157.     }
  158.  
  159.     int total_answer = 0;
  160.     for (int row = 0; row < row_amount; row++) {
  161.         for (int col = col_amount-1; col >= 0; col--) {
  162.             total_answer += answer[row][col];
  163.         }
  164.     }
  165.     cout << total_answer << endl;
  166.  
  167.     /// garbage collection
  168.     for (auto cur_garbage : garbage) {
  169.         delete cur_garbage;
  170.     }
  171.     garbage.clear();
  172.  
  173. }
  174.  
  175. void solve() {
  176.     ios::sync_with_stdio(false);
  177.     cout << setprecision(10);
  178.  
  179.     garbage.reserve(SIZE*SIZE);
  180.  
  181.     /*
  182.     freopen("distribution.out","w",stdout);
  183.     freopen("distribution.in","r",stdin);
  184.     //*/
  185.  
  186.     int t;
  187.     cin >> t;
  188.  
  189.     while (t--) {
  190.         test();
  191.     }
  192.  
  193. }
  194.  
  195. /**
  196.  
  197. 1488
  198. 3 3
  199. 3 3 1
  200. 3 3 1
  201. 2 2 5
  202. 3 3
  203. 7 7 7
  204. 7 7 7
  205. 7 7 7
  206. 4 4
  207. 7 7 7 7
  208. 7 7 7 7
  209. 9 7 7 8
  210. 8 7 8 9
  211.  
  212.  
  213.  */
  214.  
  215. #undef int
  216. int main()
  217. {
  218.     solve();
  219.  
  220.     return 0;
  221. }
  222.  
  223.  
  224. /*
  225.  
  226. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement