1. 程式人生 > >HDU2888(二維RMQ/二維線段樹 模板)

HDU2888(二維RMQ/二維線段樹 模板)

Check Corners

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


 

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

 

 

Source

2009 Multi-University Training Contest 9 - Host by HIT


題意:給一個矩陣求給定區間內的最大值並且最大值在四個點上或者四個點上有至少一個與之相同

題解:二維RMQ或者二維線段樹  標準模板


線段樹:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn=300+2;
const int inf=0x3f3f3f3f;
typedef long long ll;
struct Node
{
    int maxw,cnt;
    Node(){}
    Node(int maxw,int cnt):maxw(maxw),cnt(cnt){}
    bool operator > (Node t)
    {
        return maxw>t.maxw;
    }
}tree[maxn<<2][maxn<<2];
int m,n,q,r,mat[maxn][maxn];
Node ans;
void push_upy(int deep,int k)
{
    tree[deep][k].maxw=max(tree[deep][k<<1].maxw,tree[deep][k<<1|1].maxw);
    if(tree[deep][k].maxw==tree[deep][k<<1].maxw)
    tree[deep][k].cnt+=tree[deep][k<<1].cnt;
    if(tree[deep][k].maxw==tree[deep][k<<1|1].maxw)
    tree[deep][k].cnt+=tree[deep][k<<1|1].cnt;
    return ;
}
void push_upx(int deep,int k)
{
    tree[deep][k].maxw=max(tree[deep<<1][k].maxw,tree[deep<<1|1][k].maxw);
    if(tree[deep][k].maxw==tree[deep<<1][k].maxw)
    tree[deep][k].cnt+=tree[deep<<1][k].cnt;
    if(tree[deep][k].maxw==tree[deep<<1|1][k].maxw)
    tree[deep][k].cnt+=tree[deep<<1|1][k].cnt;
    return ;
}
void bulid_y(int ly,int ry,int deep,int k,int flag)
{
    tree[deep][k].maxw=-inf;
    if(ly==ry)
    {
        if(flag)
        {
        scanf("%d",&tree[deep][k].maxw);
        mat[r][ly]=tree[deep][k].maxw;
        tree[deep][k].cnt=1;
        }
        else {push_upx(deep,k);}//更新x方向
        return ;
    }
    int mind=(ly+ry)>>1;
    //bulid_y(ly,mind,deep,k<<1,flag);
    bulid_y(ly,mind,deep,k<<1,flag);
    bulid_y(mind+1,ry,deep,k<<1|1,flag);
    push_upy(deep,k);
    return ;
}
void bulid_x(int lx,int rx,int deep)
{
    if(lx==rx)
    {
        r=lx;
        bulid_y(1,n,deep,1,1);
        return ;
    }
    int mind=(lx+rx)>>1;
    bulid_x(lx,mind,deep<<1);
    bulid_x(mind+1,rx,deep<<1|1);
    bulid_y(1,n,deep,1,0);//同區域y軸方向
    return ;
}
void query_y(int ly,int ry,int Ly,int Ry,int deep,int k)
{
    if(Ly<=ly&&ry<=Ry)
    {
        if(ans.maxw==tree[deep][k].maxw)
        {
            ans.cnt+=tree[deep][k].cnt;
        }
        else if(tree[deep][k]>ans)
        {
            ans=tree[deep][k];
        }
        return;
    }
    int mind=(ly+ry)>>1;
    if(Ly<=mind)
    query_y(ly,mind,Ly,Ry,deep,k<<1);
    if(Ry>mind)
    query_y(mind+1,ry,Ly,Ry,deep,k<<1|1);
    return ;
}
void query_x(int lx,int rx,int Lx,int Rx,int Ly,int Ry,int deep)
{
    if(Lx<=lx&&rx<=Rx)
    {
        query_y(1,n,Ly,Ry,deep,1);
        return;
    }
    int mind=(lx+rx)>>1;
    if(Lx<=mind)
    query_x(lx,mind,Lx,Rx,Ly,Ry,deep<<1);
    if(Rx>mind)
    query_x(mind+1,rx,Lx,Rx,Ly,Ry,deep<<1|1);
    return ;
}
int main()
{
    freopen("data.txt","r",stdin);
    while(~scanf("%d%d",&m,&n))
    {
        clr(tree,0);
        clr(mat,0);
        bulid_x(1,m,1);
        scanf("%d",&q);
        int x1,y1,x2,y2;
        while(q--)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            ans.maxw=0;
            ans.cnt=0;
            query_x(1,m,x1,x2,y1,y2,1);
            int tcn=0;
            tcn+=mat[x1][y1]==ans.maxw;
            tcn+=mat[x2][y1]==ans.maxw;
            tcn+=mat[x1][y2]==ans.maxw;
            tcn+=mat[x2][y2]==ans.maxw;
            printf("%d ",ans.maxw);
            if(tcn>0){puts("yes");}
            else  {puts("no");}
        }
    }
   return 0;
}

RMQ:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
const int maxn=300+2;
const int inf=0x3f3f3f3f;
typedef long long ll;

int m,n,q,dp[maxn][maxn][9][9];
void init_rmq()
{
    int kr=log(m)/log(2);
    int kc=log(n)/log(2);
     for(int i=0;i<=kr;i++)
      for(int j=0;j<=kc;j++)
      {
          if(i==0&&j==0) continue;
          for(int r=1;r+(1<<i)-1<=m;r++)
            for(int c=1;c+(1<<j)-1<=n;c++)
              if(i==0)
                dp[r][c][i][j]=max(dp[r][c][i][j-1],dp[r][c+(1<<(j-1))][i][j-1]);
              else
                dp[r][c][i][j]=max(dp[r][c][i-1][j],dp[r+(1<<(i-1))][c][i-1][j]);
      }
    return ;
}
int query(int x1,int y1,int x2,int y2)
{
    int kr=(int)(log(double(x2-x1+1))/log(2.0));
    int kc=(int)(log(double(y2-y1+1))/log(2.0));
    return max(max(dp[x1][y1][kr][kc],dp[x2-(1<<kr)+1][y1][kr][kc]),
               max(dp[x1][y2-(1<<kc)+1][kr][kc],dp[x2-(1<<kr)+1][y2-(1<<kc)+1][kr][kc]));

}
int main()
{
   // freopen("data.txt","r",stdin);
    while(~scanf("%d%d",&m,&n))
    {
        clr(dp,0);
        for(int i=1;i<=m;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%d",&dp[i][j][0][0]);
            }
        }
        init_rmq();
        scanf("%d",&q);
        int x1,y1,x2,y2;
        while(q--)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int ans=query(x1,y1,x2,y2);
            printf("%d ",ans);
            int tcn=0;
            tcn+=dp[x1][y1][0][0]==ans;
            tcn+=dp[x1][y2][0][0]==ans;
            tcn+=dp[x2][y1][0][0]==ans;
            tcn+=dp[x2][y2][0][0]==ans;
            puts(tcn?"yes":"no");
        }
    }
   return 0;
}