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
- hdu 1754 【線段樹/RMQ】I Hate ItMQ
- hdu 4123 樹形DP+RMQMQ
- poj--2019Cornfields+二維RMQ問題MQ
- HDU 1227 Fast Food(簡單二維dp)AST
- HDU 6274 Master of Sequence(思維+樹狀陣列+二分)AST陣列
- HDU 5119 Happy Matt Friends(簡單二維dp)APP
- RMQMQ
- P2216 [HAOI2007]理想的正方形(二維RMQ)MQ
- Mysql optimize、Analyze、check、repair維護操作MySqlAI
- RMQ模板MQ
- HDU 4622 Reincarnation( 任意區間子串的長度, 字尾陣列+RMQ)陣列MQ
- 淺談RMQMQ
- RMQ求lcaMQ
- Codeforces Round #361 (Div. 2) D RMQ+二分MQ
- RMQ演算法MQ演算法
- 線段樹--RMQMQ
- hdu5289||2015多校聯合第一場1002貪心+RMQMQ
- 二維字首和 & 二維差分
- hdu 4368 樹狀陣列 離線維護陣列
- hdu7462-字串【SAM,二分】字串
- 3339: Rmq ProblemMQ
- $RMQ$問題($ST$表)MQ
- 二維碼管理平臺 生成二維碼
- hdu4400 STL應用 查詢思維題
- LeetCode-2888. 重塑資料:連結LeetCode
- HDU 5884-Sort(佇列+二分)佇列
- Flutter - 生成二維碼與識別二維碼Flutter
- 直播系統搭建,java二維碼 生成二維碼Java
- iOS 花式二維碼生成和二維碼識別iOS
- Android 二維碼掃描和生成二維碼Android
- 二維碼解析
- 二維陣列陣列
- 二維差分
- Java 二維陣列轉一維Java陣列
- RMQ的SSL配置最佳實踐MQ