AtCoder Regular Contest 081 - Flip and Rectangles

We have a board with an H \times W grid.
Each square in the grid is painted in black or white. The square at the i-th row from the top and j-th column from the left is black if the j-th character in S_i is #, and white if that character is ..
Snuke can perform the following operation on the grid any number of times:
 - Select a row or column in the grid, and invert the color of all the squares in that row or column (that is, black squares become white and vice versa).
Then, Snuke draws a rectangle along grid lines. Here, all the squares contained in the rectangle must be painted in black.
Find the maximum possible area of Snuke's rectangle when the operation is performed optimally.

-----Constraints-----
 - 2 \leq H \leq 2000
 - 2 \leq W \leq 2000
 - |S_i| = W
 - S_i consists of # and ..

-----Input-----
Input is given from Standard Input in the following format:
H W
S_1
S_2
:
S_H

-----Output-----
Print the maximum possible area of Snuke's rectangle.

-----Sample Input-----
3 3
..#
##.
.#.

-----Sample Output-----
6

If the first row from the top and the third column from the left are inverted, a 2 \times 3 rectangle can be drawn, as shown below:

Source: atcoder | Difficulty: VERY_HARD


Solutions:
(H, W) = map(int, input().split())
ans = max(H, W)
S = [input() for i in range(H)]
T = [[0] * (W - 1)]
for i in range(H - 1):
	(t, ts) = ([], [])
	for j in range(W - 1):
		r = S[i][j:j + 2] + S[i + 1][j:j + 2]
		t.append(r.count('.') % 2)
		if t[j] == 0:
			ts.append(T[-1][j] + 1)
		else:
			ts.append(0)
	T.append(ts)
for L in T[1:]:
	stack = []
	for (i, l) in enumerate(L + [0]):
		w = -1
		while stack and stack[-1][1] >= l:
			(w, h) = stack.pop()
			ans = max(ans, (h + 1) * (i - w + 1))
		if w != -1:
			stack.append((w, l))
			continue
		stack.append((i, l))
print(ans)


from collections import deque
(H, W) = map(int, input().split())
S = [input() for i in range(H)]
table = [[0] * (W - 1) for i in range(H - 1)]
for i in range(W - 1):
	for j in range(H - 1):
		table[j][i] = (int(S[j][i] == '#') + int(S[j + 1][i] == '#') + int(S[j][i + 1] == '#') + int(S[j + 1][i + 1] == '#') + 1) % 2

def get_rec(L):
	a = len(L)
	arr = L + [0]
	stack = deque()
	ans = -1
	for i in range(a + 1):
		if len(stack) == 0:
			stack.append((arr[i], i))
		elif stack[-1][0] < arr[i]:
			stack.append((arr[i], i))
		elif stack[-1][0] > arr[i]:
			while len(stack) != 0 and stack[-1][0] >= arr[i]:
				(x, y) = stack.pop()
				ans = max((x + 1) * (i - y + 1), ans)
			stack.append((arr[i], y))
	return ans
dp = [[0] * (W - 1) for i in range(H - 1)]
for i in range(W - 1):
	for j in range(H - 1):
		if j == 0:
			dp[0][i] = table[0][i]
			continue
		if table[j][i] == 1:
			dp[j][i] = dp[j - 1][i] + 1
ans = max(H, W)
for j in range(H - 1):
	ans = max(ans, get_rec(dp[j]))
print(ans)


def inpl():
	return [int(i) for i in input().split()]
(H, W) = inpl()
ans = max(H, W)
S = [input() for _ in range(H)]
T = [[0] * (W - 1)]
for i in range(H - 1):
	t = []
	for j in range(W - 1):
		r = S[i][j:j + 2] + S[i + 1][j:j + 2]
		t.append(r.count('.') % 2)
	ts = [T[-1][i] + 1 if not k else 0 for (i, k) in enumerate(t)]
	T.append(ts)
for iT in T[1:]:
	stack = []
	for (i, l) in enumerate(iT + [0]):
		dw = -1
		while stack and stack[-1][1] >= l:
			(dw, dh) = stack.pop()
			ans = max(ans, (dh + 1) * (i - dw + 1))
		if dw != -1:
			stack.append((dw, l))
			continue
		stack.append((i, l))
print(ans)


import sys

def input():
	return sys.stdin.readline()[:-1]
(H, W) = map(int, input().split())
s = [input() for _ in range(H)]
ans = max(H, W)

def max_rect(a):
	res = 0
	stack = [a[0]]
	for i in range(1, W - 1):
		new_pos = i
		while stack and stack[-1] % 10000 >= a[i]:
			(pos, hght) = (stack[-1] // 10000, stack[-1] % 10000)
			res = max(res, (i - pos + 1) * (hght + 1))
			new_pos = pos
			stack.pop()
		stack.append(new_pos * 10000 + a[i])
	while stack:
		(pos, hght) = (stack[-1] // 10000, stack[-1] % 10000)
		res = max(res, (W - pos) * (hght + 1))
		stack.pop()
	return res
dp = [[0 for _ in range(W - 1)] for _ in range(H - 1)]
for j in range(W - 1):
	if not (s[0][j] == s[1][j]) ^ (s[0][j + 1] == s[1][j + 1]):
		dp[0][j] = 1
ans = max(ans, max_rect(dp[0]))
for i in range(1, H - 1):
	for j in range(W - 1):
		if not (s[i][j] == s[i + 1][j]) ^ (s[i][j + 1] == s[i + 1][j + 1]):
			dp[i][j] = dp[i - 1][j] + 1
	ans = max(ans, max_rect(dp[i]))
print(ans)


import sys
input = sys.stdin.readline
from collections import deque
(h, w) = map(int, input().split())
s = []
for i in range(h):
	q = input().rstrip()
	s.append(q)
chk = [[0] * (w - 1) for i in range(h - 1)]
for i in range(w - 1):
	for j in range(h - 1):
		chk[j][i] = 1 - int((s[j][i] == '#') ^ (s[j + 1][i] == '#') ^ (s[j][i + 1] == '#') ^ (s[j + 1][i + 1] == '#'))

def f(a):
	a += [0]
	stack = deque()
	ans = -1
	for i in range(w):
		if stack == deque():
			stack.append((a[i], i))
		elif stack[-1][0] < a[i]:
			stack.append((a[i], i))
		elif stack[-1][0] > a[i]:
			while stack and stack[-1][0] >= a[i]:
				(x, y) = stack.pop()
				ans = max((x + 1) * (i - y + 1), ans)
			stack.append((a[i], y))
	return ans
dp = [[0] * (w - 1) for i in range(h - 1)]
for i in range(w - 1):
	dp[0][i] = chk[0][i]
for i in range(1, h - 1):
	for j in range(w - 1):
		if chk[i][j]:
			dp[i][j] = dp[i - 1][j] + 1
ans = max(h, w)
for i in range(h - 1):
	ans = max(ans, f(dp[i]))
print(ans)


mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.readline

	def largest_rectangle_histogram(A):
		A.append(0)
		N = len(A)
		ret = 0
		st = [-1]
		left = [0] * N
		for i in range(N):
			while A[st[-1]] >= A[i]:
				ret = max(ret, (A[st[-1]] + 1) * (i - left[st[-1]]))
				st.pop()
				if not st:
					break
			if st:
				left[i] = st[-1]
			else:
				left[i] = -1
			st.append(i)
		return ret

	def largest_rectangle_grid(grid, ok=1, ng=0):
		H = len(grid)
		W = len(grid[0])
		hist = [[0] * W for _ in range(H)]
		for h in range(H):
			for w in range(W):
				if grid[h][w] == ok:
					hist[h][w] = hist[h - 1][w] + 1
		ret = 0
		for h in range(H):
			ret = max(ret, largest_rectangle_histogram(hist[h]))
		return ret
	(H, W) = list(map(int, input().split()))
	grid = []
	for _ in range(H):
		grid.append(input().rstrip('\n'))
	corner = [[0] * (W - 1) for _ in range(H - 1)]
	for h in range(H - 1):
		for w in range(W - 1):
			cnt = 0
			if grid[h][w] == '#':
				cnt += 1
			if grid[h + 1][w] == '#':
				cnt += 1
			if grid[h][w + 1] == '#':
				cnt += 1
			if grid[h + 1][w + 1] == '#':
				cnt += 1
			if cnt % 2 == 0:
				corner[h][w] = 1
	print(max(largest_rectangle_grid(corner), H, W))

def __starting_point():
	main()
__starting_point()


(H, W) = map(int, input().split())
S = [input() for _ in range(H)]
ans = max(H, W)
f = [1] * W
for i in range(1, H):
	f = [f[j] + 1 if (S[i - 1][j:j + 2] + S[i][j:j + 2]).count('#') in [0, 2, 4] else 1 for j in range(W - 1)] + [0]
	stk = []
	for (j, v) in enumerate(f):
		while len(stk) > 0 and f[stk[-1]] >= v:
			ans = max(ans, f[stk.pop()] * (j - (stk[-1] if len(stk) > 0 else -1)))
		stk.append(j)
print(ans)


import sys
input = sys.stdin.readline
p2D = lambda x: print(*x, sep='\n')

def MI():
	return map(int, sys.stdin.readline().split())

def main():
	(h, w) = MI()
	s = [[c == '#' for c in input()[:-1]] for _ in range(h)]
	t = [[-1] * (w - 1) for _ in range(h - 1)]
	for i in range(h - 1):
		si = s[i]
		si1 = s[i + 1]
		t[i] = [1 - (sum(si[j:j + 2]) + sum(si1[j:j + 2])) % 2 for j in range(w - 1)]
	ti = t[0]
	for i in range(1, h - 1):
		ti1 = ti
		ti = t[i]
		for j in range(w - 1):
			if ti[j]:
				ti[j] = ti1[j] + 1
	ans = 0
	for i in range(h - 1):
		jtol = [0] * (w - 1)
		jtor = [0] * (w - 1)
		ti = t[i]
		stack = [[-1, 0]]
		for j in range(w - 1):
			tij = ti[j]
			while stack[-1][0] >= tij:
				stack.pop()
			jtol[j] = stack[-1][1]
			stack.append([tij, j + 1])
		stack = [[-1, w - 1]]
		for j in range(w - 2, -1, -1):
			tij = ti[j]
			while stack[-1][0] >= tij:
				stack.pop()
			jtor[j] = stack[-1][1]
			stack.append([tij, j])
		for j in range(w - 1):
			tmp = (jtor[j] - jtol[j] + 1) * (ti[j] + 1)
			if tmp > ans:
				ans = tmp
	print(max(ans, h, w))
main()


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
(H, W) = map(int, readline().split())
grid = read().replace(b'\r', b'').replace(b'\n', b'')
(H, W)
cnt = [0] * (H * W)
for x in range(H * W):
	if grid[x] == ord('#'):
		cnt[x] ^= 1
		if x % W:
			cnt[x - 1] ^= 1
		if x >= W:
			cnt[x - W] ^= 1
		if x % W and x >= W:
			cnt[x - W - 1] ^= 1
max_area = 0
height = [1] * (W - 1)
for i in range(H - 1):
	for j in range(W - 1):
		if cnt[W * i + j]:
			height[j] = 1
		else:
			height[j] += 1
	stack = []
	for (j, h) in enumerate(height + [0]):
		while stack:
			prev_h = height[stack[-1]]
			if prev_h < h:
				break
			stack.pop()
			left_end = -1 if not stack else stack[-1]
			area = (j - left_end) * prev_h
			if max_area < area:
				max_area = area
		stack.append(j)
if max_area < max(W, H):
	max_area = max(W, H)
print(max_area)