HDU 2888 Check Corners(二維RMQ)
Check Corners
Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3063 Accepted Submission(s): 1100
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.
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
Source
題意:
給出一個矩陣,給你幾個詢問,每個詢問問你子矩陣中的最大值,若最大值在角上,yes,否則no。
POINT:
二維的RMQ,之前沒做過。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
const int N = 300+3;
int n,m;
int a[N][N];
int dp[N][N][9][9];
void prermq()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
dp[i][j][0][0]=a[i][j];
}
}
for(int a=0;1<<a<=n;a++)
{
for(int b=0;1<<b<=m;b++)
{
if(a==b&&b==0) continue;
for(int i=1;i-1+(1<<a)<=n;i++)
{
for(int j=1;j-1+(1<<b)<=m;j++)
{
if(a==0)
{
dp[i][j][a][b]=max(dp[i][j][a][b-1],dp[i][j+(1<<(b-1))][a][b-1]);
}
else
dp[i][j][a][b]=max(dp[i][j][a-1][b],dp[i+(1<<(a-1))][j][a-1][b]);
}
}
}
}
}
int query(int r1,int c1,int r2,int c2)
{
int k1=(int)log2((double)(r2-r1+1));
int k2=(int)log2((double)(c2-c1+1));
int a=dp[r1][c1][k1][k2];
int b=dp[r1][c2+1-(1<<k2)][k1][k2];
int c=dp[r2+1-(1<<k1)][c1][k1][k2];
int d=dp[r2+1-(1<<k1)][c2+1-(1<<k2)][k1][k2];
return max(max(a,b),max(c,d));
}
int ans;
bool f(int x,int y)
{
if(a[x][y]==ans) return 1;
return 0;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(dp,0,sizeof dp);
memset(a,0,sizeof a);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
prermq();
int q;
scanf("%d",&q);
while(q--)
{
int r1,c1,r2,c2;
scanf("%d %d %d %d",&r1,&c1,&r2,&c2);
ans=query(r1,c1,r2,c2);
printf("%d ",ans);
if(f(r1,c1)||f(r2,c1)||f(r1,c2)||f(r2,c2))
{
printf("yes\n");
}
else printf("no\n");
}
}
}
相關文章
- HDU 3486 Interviewe(RMQ+二分)ViewMQ
- HDU Find the hotel(RMQ)MQ
- HDU 3183 A Magic Lamp (RMQ)LAMPMQ
- poj--2019Cornfields+二維RMQ問題MQ
- HDU – 4811 – Ball (思維)
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- RMQMQ
- RMQ模板MQ
- hdu 6446 Tree and Permutation(dfs+思維)
- RMQ求lcaMQ
- 淺談RMQMQ
- kebab HDU - 2883(思維建圖,最大流)
- LeetCode-2888. 重塑資料:連結LeetCode
- 線段樹--RMQMQ
- hdu7462-字串【SAM,二分】字串
- WebLogic EJBTaglibDescriptor XXE漏洞(CVE-2019-2888)分析Web
- $RMQ$問題($ST$表)MQ
- onclick="return check()" 和 onclick="check()" 區別
- 二維字首和 & 二維差分
- HDU4725 The Shortest Path in Nya Graph【Dijkstra+思維】
- POJ3264 Balanced Lineup【RMQ】MQ
- hdu 2111 Saving HDU (DP)
- 二維碼管理平臺 生成二維碼
- Check if String is HappyAPP
- 二維碼
- 【hdu】口算訓練/二分/質因數分解
- HDU - 1061 Rightmost Digit(二分快速冪板題)Git
- Flutter - 生成二維碼與識別二維碼Flutter
- RMQ問題的各種解法MQ
- RMQ的SSL配置最佳實踐MQ
- RMQ_第一彈_Sparse TableMQ
- POJ 3264 Balanced Lineup【RMQ問題】MQ
- 直播系統搭建,java二維碼 生成二維碼Java
- check_document_position
- CHECK_INTERFACE作用
- Android 二維碼相關(二)Android
- 二維差分
- 二維碼解析