hdu 2888 二维RMQ模板题

时间:2022-08-30 14:44:35

Check Corners

Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2377    Accepted Submission(s): 859

Problem Description
Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numbers ( 1 <= i <= m, 1 <= j <= n ). Now he selects some sub-matrices, hoping to find the maximum number. Then he finds that there may be more than one maximum number, he also wants
to know the number of them. But soon he find that it is too complex, so he changes his mind, he just want to know whether there is a maximum at the four corners of the sub-matrix, he calls this “Check corners”. It’s a boring job when selecting too many sub-matrices,
so he asks you for help. (For the “Check corners” part: If the sub-matrix has only one row or column just check the two endpoints. If the sub-matrix has only one entry just output “yes”.)
 
Input
There are multiple test cases.

For each test case, the first line contains two integers m, n (1 <= m, n <= 300), which is the size of the row and column of the matrix, respectively. The next m lines with n integers each gives the elements of the matrix which fit in non-negative 32-bit integer.

The next line contains a single integer Q (1 <= Q <= 1,000,000), the number of queries. The next Q lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= m, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner
and lower-right corner of the sub-matrix in question. 

 
Output
For each test case, print Q lines with two numbers on each line, the required maximum integer and the result of the “Check corners” using “yes” or “no”. Separate the two parts with a single space.
 
Sample Input
4 4
4 4 10 7
2 13 9 11
5 7 8 20
13 20 8 2
4
1 1 4 4
1 1 3 3
1 3 3 4
1 1 1 1
 
Sample Output
20 no
13 no
20 yes
4 yes

题意:

每次查询求解一个矩阵中的最大值,并判断是否与这个矩阵的四角相等。

/*
二维RMQ的思路与一维的大致相同,都是借助dp先进行预处理,然后快速查询
hhh-2016-01-30 01:59:55
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
typedef long long ll;
using namespace std; const int maxn = 305;
int dp[maxn][maxn][9][9];
int tmap[maxn][maxn];
int mm[maxn];
void iniRMQ(int n,int m)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j][0][0] = tmap[i][j];
for(int ti = 0; ti <= mm[n]; ti++)
for(int tj = 0; tj <= mm[m]; tj++)
if(ti+tj)
for(int i = 1; i+(1<<ti)-1 <= n; i++)
for(int j = 1; j+(1<<tj)-1 <= m; j++)
{
if(ti)
dp[i][j][ti][tj] =
max(dp[i][j][ti-1][tj],dp[i+(1<<(ti-1))][j][ti-1][tj]);
else
dp[i][j][ti][tj] =
max(dp[i][j][ti][tj-1],dp[i][j+(1<<(tj-1))][ti][tj-1]);
}
} int RMQ(int x1,int y1,int x2,int y2)
{
int k1 = mm[x2-x1+1];
int k2 = mm[y2-y1+1];
x2 = x2 - (1<<k1) +1;
y2 = y2 - (1<<k2) +1;
return
max(max(dp[x1][y1][k1][k2],dp[x1][y2][k1][k2]),
max(dp[x2][y1][k1][k2],dp[x2][y2][k1][k2]));
} int main()
{
int n,m;
mm[0] = -1;
for(int i =1 ; i <= 301; i++)
mm[i] = ((i&(i-1)) == 0)? mm[i-1]+1:mm[i-1];
while(scanf("%d%d",&n,&m)==2)
{
for(int i =1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&tmap[i][j]);
iniRMQ(n,m);
int k;
scanf("%d",&k);
while(k--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = RMQ(x1,y1,x2,y2);
printf("%d ",ans); if(ans == tmap[x1][y1] || ans == tmap[x1][y2]
|| ans == tmap[x2][y1]|| ans == tmap[x2][y2])
printf("yes\n");
else
printf("no\n");
}
}
return 0;
}

  

hdu 2888 二维RMQ模板题的更多相关文章

  1. hdu 2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. poj2019 二维RMQ模板题

    和hdu2888基本上一样的,也是求一个矩阵内的极值 #include<iostream> #include<cstring> #include<cstdio> # ...

  3. hduacm 2888 ----二维rmq

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题  直接用二维rmq 读入数据时比较坑爹  cin 会超时 #include <cstdio& ...

  4. poj2019 二维RMQ裸题

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:8623   Accepted: 4100 Descrip ...

  5. 二维RMQ模板

    int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...

  6. Cornfields POJ - 2019&lpar;二维RMQ板题&rpar;

    就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...

  7. HDU 2888:Check Corners(二维RMQ)

    http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中 ...

  8. POJ 2019 Cornfields &lbrack;二维RMQ&rsqb;

    题目传送门 Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7963   Accepted: 3822 ...

  9. Zeratul的完美区间&lpar;线段树&vert;&vert;RMQ模板题&rpar;

    原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...

随机推荐

  1. el表达式的function标签

    使用el调用Java方法 1:EL表达式语法允许开发人员开发自定义函数,以调用java类的方法. ~示例:${el:method(params)} ~在EL表达式中调用的只能是java类的静态方法. ...

  2. 在 远程桌面 权限不足无法控制 UAC 提示时,可使用 计划任务 绕开系统的 UAC 提示

    就是记录一下,在远程的时候,很可能远程软件没有以管理员身份运行,或者其它原因,操作会被系统阻止,UAC 会进行提示,但是远程软件目前是无法操作的.(以下方法在 Windows 7 中测试通过) 可以通 ...

  3. NoSql数据库使用半年后在设计上面的一些心得 &lpar;转&rpar;

    http://www.cnblogs.com/AllenDang/p/3507821.html NoSql数据库这个概念听闻许久了,也陆续看到很多公司和产品都在使用,优缺点似乎都被分析的清清楚楚.但我 ...

  4. AdapterView&amp&semi;lt&semi;&quest;&amp&semi;gt&semi; arg0&comma; View arg1&comma; int arg2&comma; long arg3參数含义

    arg0:是指父Vjew arg1就是你点击的那个Item的View arg2是position,position是你适配器里面的position arg3是id,通常是第几个项.id是哪个项View ...

  5. Tomcat剖析(一):一个简单的Web服务器

    Tomcat剖析(一):一个简单的Web服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器 ...

  6. 实战绕过某医院的waf

    最近遇到一个注入,我们直接来看吧.还是常规的单引号: 是一个很常规的注入.我们来尝试下获取一些信息: 然后发现是有防火墙的,安全狗.安全狗有很多针对php+mysql的绕过方法,比如这样:/*!uni ...

  7. wait和sleep的区别

    wait是线程永久等待,只有调用notify才能进行唤醒 sleep是等待指定的时间,自动唤醒

  8. Unexpected directive &&num;39&semi;XXX&&num;39&semi; imported by the module &&num;39&semi;AppMoode&&num;39&semi;

    做angular demo报错: Uncaught Error: Unexpected directive 'ScrollSpyDirective' imported by the module 'A ...

  9. vue axios的使用

    详细可以看:https://www.kancloud.cn/yunye/axios/234845 这里介绍日常使用得比较多的get和post: import axios from 'axios' // ...

  10. BZOJ &num;3746&colon; &lbrack;POI2015&rsqb;Czarnoksi&eogon;&zdot;nicy okr&aogon;g&lstrok;ego sto&lstrok;u 动态规划

    转载请注明出处:http://www.cnblogs.com/TSHugh/p/8823423.html 读完题就会发现p=0.1的情况以及n=1.2的情况都可以直接判掉,而p=2的时候也可以直接构造 ...