Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK: 40000000")
- #define M_PI 3.14159265358979323846
- #define M_PI_2 1.57079632679489661923
- #include <iomanip>
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <complex>
- #include <queue>
- #include <vector>
- #include <tuple>
- #include <set>
- #include <map>
- #include <cmath>
- #include <stdlib.h>
- #include <stdio.h>
- #define INF 1000000000000000099
- #define BLIFT 17
- #define SIZE 1005
- #define GSIZE 2000000
- #define SEP -77
- #define ENCODE 1000000
- #define ENCODE_2 500000
- #define MOD 1000000007
- #define DBG false
- #define lli long long int
- #define mp make_pair
- #define cpxd complex<double>
- #define compCount 1000001
- #define DLOG(x) cout << #x << ": " << x << endl;
- //#undef DLOG(x)
- //#define DLOG(x) ;
- #define int long long int
- #define double long double
- #define EPS 0.000000000001
- using namespace std;
- vector<int> graph[SIZE];
- int least_prime_divisor[SIZE];
- vector<int> primes;
- struct Node {
- Node* next;
- int row;
- int col;
- };
- int mtx[SIZE][SIZE];
- Node* ladder[SIZE][SIZE];
- int answer[SIZE][SIZE];
- int jumpdownrow[SIZE][SIZE]; /// row, col => maximum row going down to goes only by the same number
- vector<Node*> garbage;
- void pritchard() {
- for (int i = 2; i < SIZE; i++) {
- if (least_prime_divisor[i] == 0) {
- primes.push_back(i);
- least_prime_divisor[i] = i;
- }
- for (auto subprime : primes) {
- if (subprime > least_prime_divisor[i]
- || i*subprime >= SIZE) break;
- least_prime_divisor[i * subprime] = subprime;
- }
- }
- }
- int fpow (int base, int exp) {
- if (exp == 0) return 1;
- if (exp == 1) return base;
- if (exp & 1) {
- return base * fpow(base*base, exp >> 1);
- } else {
- return fpow(base*base, exp >> 1);
- }
- }
- void test() {
- int row_amount, col_amount;
- cin >> row_amount >> col_amount;
- int last_row = row_amount-1;
- int last_col = col_amount-1;
- for (int row = 0; row < row_amount; row++) {
- for (int col = 0; col < col_amount; col++) {
- cin >> mtx[row][col];
- }
- }
- for (int row = row_amount-1; row >= 0; row--) {
- for (int col = 0; col < col_amount; col++) {
- if (row == row_amount-1 || mtx[row][col] != mtx[row+1][col]) {
- jumpdownrow[row][col] = row;
- } else {
- jumpdownrow[row][col] = jumpdownrow[row+1][col];
- }
- }
- }
- /*
- cout << endl;
- for (int row = 0; row < row_amount; row++) {
- for (int col = 0; col < col_amount; col++) {
- cout << jumpdownrow[row][col] << " ";
- }
- cout << endl;
- }
- cout << endl;
- //*/
- for (int row = 0; row < row_amount; row++) {
- for (int col = col_amount-1; col >= 0; col--) {
- Node* new_node = new Node;
- new_node->next = nullptr;
- new_node->col = col;
- new_node->row = jumpdownrow[row][col];
- garbage.push_back(new_node);
- answer[row][col] = jumpdownrow[row][col] - row + 1;
- ladder[row][col] = new_node;
- if (col+1 < col_amount && mtx[row][col] == mtx[row][col+1]) {
- new_node->next = ladder[row][col+1];
- answer[row][col] += answer[row][col+1];
- Node* cur_node = ladder[row][col+1];
- int prev_col = col;
- while (cur_node != nullptr && cur_node->row > jumpdownrow[row][col]) {
- answer[row][col] -= (cur_node->row - jumpdownrow[row][col])*(cur_node->col - prev_col);
- cur_node->row = jumpdownrow[row][col];
- prev_col = cur_node->col;
- cur_node = cur_node->next;
- }
- while (ladder[row][col]->next != nullptr
- && ladder[row][col]->row == ladder[row][col]->next->row) {
- Node* next_node = ladder[row][col]->next;
- ladder[row][col] = next_node;
- }
- }
- }
- }
- int total_answer = 0;
- for (int row = 0; row < row_amount; row++) {
- for (int col = col_amount-1; col >= 0; col--) {
- total_answer += answer[row][col];
- }
- }
- cout << total_answer << endl;
- /// garbage collection
- for (auto cur_garbage : garbage) {
- delete cur_garbage;
- }
- garbage.clear();
- }
- void solve() {
- ios::sync_with_stdio(false);
- cout << setprecision(10);
- garbage.reserve(SIZE*SIZE);
- /*
- freopen("distribution.out","w",stdout);
- freopen("distribution.in","r",stdin);
- //*/
- int t;
- cin >> t;
- while (t--) {
- test();
- }
- }
- /**
- 1488
- 3 3
- 3 3 1
- 3 3 1
- 2 2 5
- 3 3
- 7 7 7
- 7 7 7
- 7 7 7
- 4 4
- 7 7 7 7
- 7 7 7 7
- 9 7 7 8
- 8 7 8 9
- */
- #undef int
- int main()
- {
- solve();
- return 0;
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement