码迷,mamicode.com
首页 > 其他好文 > 详细

二维 ST 表

时间:2021-06-13 09:59:25      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:printf   i++   for   mat   pre   continue   name   getch   main   

#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;
}

二维 ST 表

标签:printf   i++   for   mat   pre   continue   name   getch   main   

原文地址:https://www.cnblogs.com/GJY-JURUO/p/14877901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!