#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
int const N= 302;
int mat[N][N], n, q, pos[N];
int dMin[9][9][N][N];
void initRMQ(){
int i, j, u, v, a, b, c, d;
for( pos[0]= -1, i= 1; i<= n; ++i )
pos[i]= ( ( i& (i- 1) )== 0 )? pos[i- 1]+ 1: pos[i- 1];
for( i= 0; i<= pos[n]; ++i )
for( j= 0; j<= pos[n]; ++j )
for( u= 1; u<= n+ 1- (1<<i); u++ )
for( v= 1; v<= n+ 1- (1<<j); v++ ){
if( i== 0 && j== 0 ) continue;
if( i== 0 ) dMin[i][j][u][v]= min( dMin[i][j- 1][u][v], dMin[i][j- 1][u][v+ (1<<(j- 1))] );
else dMin[i][j][u][v]= min( dMin[i- 1][j][u][v], dMin[i- 1][j][u+ (1<<(i- 1))][v] );
}
}
int askRMQ( int x1, int y1, int x2, int y2 ){
int x= pos[x2- x1+ 1], y= pos[y2- y1+ 1];
int a= min( dMin[x][y][x1][y1], dMin[x][y][x2- (1<<x)+ 1][y1] );
int b= min( dMin[x][y][x1][y2- (1<<y)+ 1], dMin[x][y][x2- (1<<x)+ 1][y2- (1<<y)+ 1] );
return min(a,b);
}
int main(){
int test;
scanf("%d", &test );
while( test-- ){
scanf("%d",&n );
for( int i= 1; i<= n; ++i )
for( int j= 1; j<= n; ++j ){
scanf("%d", &mat[i][j] );
dMin[0][0][i][j]= mat[i][j];
}
initRMQ();
scanf("%d", &q );
int x1, y1, x2, y2;
for( int i= 0; i< q; ++i ){
scanf("%d%d%d%d", &x1, &y1, &x2, &y2 );
printf("%d\n", askRMQ( x1, y1, x2, y2 ) );
}
}
return 0;
}
分享到:
相关推荐
ZJU/zoj 题库上的部分题源码 本人博客: hi.baidu.com/xiaoxianxi_acm
acm 模板 算法 浙大 zoj zju acm初学者必备 代码
zoj 1140-zju 2433 简单题的部分答案 都是可以正确通过的,简洁易懂
acm 新手必备 浙大 解答 代码库 zoj zju
要为您的条目设置图标,您需要将图标配置添加到每个菜单条目,这些菜单条目指向您希望用于该条目的rEFInd-ZJU/icons下的rEFInd-ZJU/icons 。 这是一个示例配置。 menuentry "Ubuntu" { loader /EFI/ubuntu/...
zoj1090解题报道,还是比较详细的代码 有注释
zju 1025 Wooden Sticks http://acm.zju.edu.cn/show_problem.php?pid=1025
动态规划:http://acm.zju.edu.cn/forum/viewtopic.php?t=69 搜索:http://acm.zju.edu.cn/forum/viewtopic.php?t=67 数论:http://acm.zju.edu.cn/forum/viewtopic.php?t=66 几何:...
ZJU考研机试真题 九度1006ZOJ问题
最近在acm.zju.edu.cn上通过的题目的代码,都是比较有价值的题目
zju1001--1399的数据。全是.in和.out文件。
zoj700代码,供acm爱好者研究学习,但请注意,切勿上交。
zju 1048 Financial Managementhttp://acm.zju.edu.cn/show_problem.php?pid=1048
浙江大学zoj试题及答案详解,有详细思路及代码! c/c++语言版本
浙大ACM网上的题目分类,以及部分题目的题解,包括题目,对于没联网的很适用,即使就有网的也很用的,里面有些题解很祥细……
zju超强代码,大概有一半的题目吧,从别人那里弄的
cpp codes for zju.edu.cn problems
这个是浙江大学操作系统课程讲义,此部分为第2章,配套教材为操作系统的恐龙书
zju电机学作业.pdf
zju动态规划试题选集 ,有ZOJ所有的动态规划题及其代码,很好的!!!