POJ 2019

一道水题,然而A的过程实在曲折,中间WA了几次,MLE几次,RUNTIMEERROR几次,确实有点搞心态。
这道题总结如下:

  • 卡内存
  • Sparse Table的二维RMQ,一定一定重视细节,必须开始重视代码一次测试的正确率,总是有潜在的bug在实战非常浪费时间
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;

const int maxn= 253;

unsigned char mn[maxn][maxn][9][9], mx[maxn][maxn][9][9];
int mm[maxn];

void InitRMQ(int n)
{
	int ele;
	for (int i= 1; i<= n; ++i){
		for (int j= 1; j<= n; ++j){
			scanf("%d", &ele);
			mn[i][j][0][0]= mx[i][j][0][0]= ele;
		}
	}
	for (int ii= 0; ii<= mm[n]; ++ii){
		for (int jj= 0; jj<= mm[n]; ++jj){
			if (!ii && !jj){
				continue;
			}
			for (int i= 1; i+(1<<ii)-1<= n; ++i){
				for (int j= 1; j+(1<<jj)-1<= n; ++j){
					if (ii){
						mn[i][j][ii][jj]= min(mn[i][j][ii-1][jj], mn[i+(1<<(ii-1))][j][ii-1][jj]);
						mx[i][j][ii][jj]= max(mx[i][j][ii-1][jj], mx[i+(1<<(ii-1))][j][ii-1][jj]);
					}
					else{
						mn[i][j][ii][jj]= min(mn[i][j][ii][jj-1], mn[i][j+(1<<(jj-1))][ii][jj-1]);
						mx[i][j][ii][jj]= max(mx[i][j][ii][jj-1], mx[i][j+(1<<(jj-1))][ii][jj-1]);
					}
				}
			}
		}
	}
}
inline int Query(int x, int y, int b)
{
	int k= mm[b];
	int x1= x+b-(1<<k), y1= y+b-(1<<k);
	int v_mx, v_mn;
	v_mx= max(max(mx[x][y][k][k], mx[x1][y][k][k]), max(mx[x][y1][k][k], mx[x1][y1][k][k]));
	v_mn= min(min(mn[x][y][k][k], mn[x1][y][k][k]), min(mn[x][y1][k][k], mn[x1][y1][k][k]));

	return v_mx-v_mn;
}

int main(int argc, char const *argv[])
{
	int n, b, k;
	scanf("%d %d %d", &n, &b, &k);

	mm[0]= -1;
	for (int i= 1; i<= n; ++i){
		mm[i]= i & (i-1) ? mm[i-1] : mm[i-1]+1;
	}
	InitRMQ(n);

	while (k--){
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%d\n", Query(l, r, b));
	}
	return 0;
}
posted @ 2021-05-14 19:56  IdiotNe  阅读(48)  评论(0编辑  收藏  举报