372B - Counting Rectangles is Fun - CodeForces Solution


brute force divide and conquer dp *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

#define dbg(x) cerr << #x << " = " << (x) << "\n"
#define bit(n,k) (((n)>>(k))&1)
#define rep(i,a,b) for(long long i = (a); i <= (b); ++i)
#define per(i,a,b) for(long long i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef pair<int, int> i2;
typedef pair<ll, ll> ll2;
typedef long double ld;

const int N = 49;
int rect[N][N][N][N];
int dp[N][N][N][N];

void testcase() {
	
	int n, m, q; cin >> n >> m >> q;
	rep(i, 1, n) {
		rep(j, 1, m) {
			char c; cin >> c;
			if(c == '1') rect[i][j][i][j] = 1;
		}
	}
	rep(h, 1, n) {
		rep(w, 1, m) {
			rep(i, 1, n-h+1) {
				rep(j, 1, m-w+1) {
					rect[i][j][i+h-1][j+w-1] = rect[i][j][i+h-1][j+w-2]+rect[i][j][i+h-2][j+w-1]-rect[i][j][i+h-2][j+w-2]+rect[i+h-1][j+w-1][i+h-1][j+w-1];
				}
			}
		}
	}
	rep(h, 1, n) {
		rep(w, 1, m) {
			rep(i, 1, n-h+1) {
				rep(j, 1, m-w+1) {
					dp[i][j][i+h-1][j+w-1] = (rect[i][j][i+h-1][j+w-1] == 0 ? 1 : 0);
					
					dp[i][j][i+h-1][j+w-1] += dp[i][j][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i][j+1][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i][j][i+h-2][j+w-1];
					
					dp[i][j][i+h-1][j+w-1] -= dp[i][j][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i][j+1][i+h-2][j+w-1];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j+1][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i][j+1][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j][i+h-2][j+w-1];
					
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i][j+1][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j+1][i+h-2][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j+1][i+h-1][j+w-2];
					
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j+1][i+h-2][j+w-2];
				}
			}
		}
	}
	while(q--) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		cout << dp[a][b][c][d] << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int t = 1;
	//cin >> t;
	while(t--) testcase();

	return 0;
}


Comments

Submit
0 Comments
More Questions

2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets