Cornfields poj2019 two-dimensional rmq

Source: Internet
Author: User
Cornfields Time limit:1000 ms Memory limit:30000kb 64bit Io format:% I64d & % i64usubmit status

Description

FJ has decided to grow his own Corn Hybrid in order to help the cows make the best possible milk. to that end, he's looking to build the cornfield on the flattest piece of land he can find.

FJ has, at great expense, surveyed his square farm of N x n hectares (1 <= n <= 250 ). each hectare has an integer elevation (0 <= elevation <= 250) associated with it.

FJ will present your program with the elevations and a set of K (1 <= k <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation? ". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. help FJ find the best place to put his cornfield.

Input

* Line 1: three space-separated integers: N, B, and K.

* Lines 2 .. n + 1: each line contains n space-separated integers. line 2 represents Row 1; line 3 represents Row 2, etc. the first integer on each line represents column 1; the second integer represents Column 2; etc.

* Lines n + 2 .. N + k + 1: Each line contains two space-separated integers representing a query. the first integer is the top row of the query; the second integer is the left column of the query. the integers are in the range 1 .. n-B + 1.

Output

* Lines 1. K: A single integer per line representing the difference between the Max and the min in each query.

Sample Input

5 3 15 1 2 6 31 3 5 2 77 2 4 6 19 9 8 6 50 6 9 3 91 2

Sample output

5


 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 using namespace std; 7 unsigned char dpi[300][300][9],dpa[300][300][9]; 8 int main() 9 {10     int n,b,k;11     int i,j,ii,jj,x,y;12     unsigned char maxa,mina;13     scanf("%d%d%d",&n,&b,&k);14     memset(dpi,0,sizeof(dpi));15     memset(dpa,0,sizeof(dpa));16     for(i=0; i<n; i++)17         for(j=0; j<n; j++)18             scanf("%d",&x),dpa[i][j][0]=x,dpi[i][j][0]=dpa[i][j][0];19     for(ii=1; ii<9; ii++)20     {21         for(i=0; i+(1<<ii) - 1 <n; i++)22         {23             for(j=0; j+(1<<ii)-1<n; j++)24             {25                dpi[i][j][ii]=min(dpi[i][j][ii-1],dpi[i+(1<<(ii-1))][j][ii-1]);26                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i][j+(1<<(ii-1))][ii-1]);27                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);28                dpa[i][j][ii]=max(dpa[i][j][ii-1],dpa[i+(1<<(ii-1))][j][ii-1]);29                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i][j+(1<<(ii-1))][ii-1]);30                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);31             }32         }33     }34     int kk=log(1.0*b)/log(2.0);35     for(i=0; i<k; i++)36     {37         scanf("%d%d",&x,&y);38         x--,y--;39         maxa=dpa[x][y][kk];40         maxa=max(maxa,dpa[x][y+b-(1<<kk)][kk]);41         maxa=max(maxa,dpa[x+b-(1<<kk)][y][kk]);42         maxa=max(maxa,dpa[x+b-(1<<kk)][y+b-(1<<kk)][kk]);43 44         mina=dpi[x][y][kk];45         mina=min(mina,dpi[x][y+b-(1<<kk)][kk]);46         mina=min(mina,dpi[x+b-(1<<kk)][y][kk]);47         mina=min(mina,dpi[x+b-(1<<kk)][y+b-(1<<kk)][kk]);48         printf("%d\n",maxa-mina);49     }50 }
View code

 

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.