import numpy as np


class MyClass(object):
    def __init__(self, img: np.ndarray):
        w, h = img.shape
        dp = np.zeros((w, h))
        dp[0, 0] = img[0, 0]
        for i in range(1, w):
            dp[i, 0] = img[i, 0] + dp[i-1, 0]

        for j in range(1, h):
            dp[0, j] = img[0, j] + dp[0, j-1]

        for i in range(1, w):
            for j in range(1, h):
                dp[i, j] = dp[i-1, j] + dp[i, j-1] - dp[i-1, j-1]

        self.dp = dp

    def calc_mean(self, x1, y1, x2, y2):
        count = (x2 - x1 + 1) * (y2 - y1 + 1)
        if x1 == 0 and y1 == 0:
            return self.dp[x2, y2] / count

        elif x1 == 0:
            val = self.dp[x2, y2] - self.dp[x2, y1 - 1]
            return val / count

        elif y1 == 0:
            val = self.dp[x2, y2] - self.dp[x1, y2 - 1]
            return val / count

        else:
            val = self.dp[x2, y2] - self.dp[x1, y2-1] - self.dp[x2, y1 -1] + self.dp[x1-1, y1 - 1]
            return val / count



