HDU2888 Check Corners

Source: Internet
Author: User
Tags integer numbers

Description

Paul draw a big m*n matrix A last month, whose entries ai,j is 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 is more than one maximum number, he also wants to know the number of them. But soon he-find that it's too complex, so he changes his mind, he just want to know whether there are a maximum at the FO ur corners of the Sub-matrix, he calls this "Check corners". It's a boring job when selecting too many sub-matrices, so he asks your for help. (For the "Check Corners" part:if The Sub-matrix have only one row or column just Check the one and the endpoints. If the Sub-matrix have only one entry just output "yes".) 

Input

There is multiple test cases.

For each test case, the first line contains-integers m, n (1 <= m, n <=), which is the size of the row a nd 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 & lt;= C2 <= N), which is 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 the numbers in each line, the required maximum integer and the result of the "C Heck corners "using" yes "or" no ". Separate the parts with a single space. 

Sample Input

4 4 9, 7 8, 8 241, 1 4, 1 3, 3 3, 1 1 1 

Sample Output

no13 no20 yes4 Yes 

Source

multi-university Training Contest 9-host by hits

Two-dimensional rmq exercises.

One-dimensional rmq two-point maintenance of an interval, while the two-dimensional RMQ maintains the maximum value of an interval by maintaining a rectangular interval of four divisions, with the same basic principle

Understanding is not very thorough, the code basically by copy.

The memory limit range is very small, the array is slightly larger on the MLE

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 intf[302][302][9][9];9 //F[i][j][x][y] Record with (I,J) as the upper-left corner, (i+ (1<<x), j+ (1<<y)) to the bottom right corner of the rectangle within the maximum valueTen intmp[302][302]; One intn,m; A voidinit () { -     inti,j; -     intk,l; the      for(i=1; i<=n;i++) -        for(j=1; j<=m;j++) -f[i][j][0][0]=Mp[i][j]; -     intKn= (int) (Log (Double) n)/log (2.0)); +     intKm= (int) (Log (Double) m)/log (2.0)); -      for(i=0; i<=kn;i++) +        for(j=0; j<=km;j++){ A           if(i==0&& j==0)Continue; at            for(k=1; k + (1<<i)-1<=n;k++) -                for(l=1; L + (1&LT;&LT;J)-1<=m;l++){ -                   if(!i)//i==0 && j!=0 -F[k][l][i][j]=max (f[k][l][i][j-1],f[k][l+ (1<< (J-1))][i][j-1]); -                   Else //i!=0 -F[k][l][i][j]=max (f[k][l][i-1][j],f[k+ (1<< (I-1))][l][i-1][j]); in               } -         } to     return; + } - intRMQ (intX1,intY1,intX2,inty2) { the     intKn= (int) (Log (Double(x2-x1+1))/log (2.0)); *     intKm= (int) (Log (Double(y2-y1+1))/log (2.0)); $     intA=max (f[x1][y1][kn][km],f[x2-(1&LT;&LT;KN) +1][y1][kn][km]);Panax Notoginseng     intB=max (f[x1][y2-(1&LT;&LT;KM) +1][kn][km],f[x2-(1&LT;&LT;KN) +1][y2-(1&LT;&LT;KM) +1][kn][km]); -     returnMax (A, b); the } + intMain () { A     inti,j; the     intK; +      while(SCANF ("%d%d", &n,&m)!=-1){ -          $          for(i=1; i<=n;i++) $            for(j=1; j<=m;j++) -scanf"%d",&mp[i][j]); - init (); the         intX1,x2,y1,y2; -scanf"%d",&k);Wuyi          while(k--){ thescanf"%d%d%d%d",&x1,&y1,&x2,&y2); -             intans=RMQ (x1,y1,x2,y2); Wuprintf"%d", ans); -             if(Ans==mp[x1][y1] | | ans==mp[x2][y2] | | ans==mp[x1][y2 | | | ans==Mp[x2][y1]) AboutPuts"Yes"); $             ElsePuts"No"); -         } -     } -     return 0; A}

HDU2888 Check Corners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.