二维 ST 表

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ZYC using
#define AK namespace
#define IOI std
#define ll long long

ZYC AK IOI;

const int N = 260;

inline ll Read()
{
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

int n, m, q;

namespace ST
{
	int st[N][N][25][25];
	void Prework()
	{
		for (int k = 0; k <= 24; k++)
			for (int l = 0; l <= 24; l++)
				if (!k && !l) continue;
				else
					if(!l)
						for (int i = 1; i + (1 << k) - 1 <= n; i++)
							for (int j = 1; j + (1 << l) - 1 <= m; j++)
								st[i][j][k][0] = max(st[i][j][k - 1][0], st[i + (1 << k - 1)][j][k - 1][0]);
					else
						for (int i = 1; i + (1 << k) - 1 <= n; i++)
							for (int j = 1; j + (1 << l) - 1 <= m; j++)
								st[i][j][k][l] = max(st[i][j][k][l - 1], st[i][j + (1 << l - 1)][k][l - 1]);
	}
	int Query(int a, int b, int c, int d)
	{
		int k = log2(c - a + 1), l = log2(d - b + 1);
		return max(max(st[a][b][k][l], st[c - (1 << k) + 1][b][k][l]), 
		       max(st[a][d - (1 << l) + 1][k][l], st[c - (1 << k) + 1][d - (1 << l) + 1][k][l]));
	}
}


int main()
{
	n = Read(), m = Read(), q = Read();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			ST::st[i][j][0][0] = Read();
		
	ST::Prework();
	
	while(q--)
	{
		int a = Read() , b = Read(), c = Read(), d = Read();
		printf ("%d\n", ST::Query(a, b, c, d));
	}
	return 0;
}

posted @ 2021-06-12 11:21  Jayun  阅读(66)  评论(0编辑  收藏  举报