[Noi.ac] lined up

Problem Description

Opening soon, W is a small military training.

Their class a total of n * m bits students, lined up so when instructors so that they stood in a row n and m columns. Small W found that if a person's height in the row where he is the highest, is the highest in that column, then he will be very conspicuous. As the parade too boring, small W begin to calculate how many people are very conspicuous.

This is clearly not beat him, so he wanted to calculate how many people are x where he ranked high in his column where y is the first high. To cope with the busy small W instructor, so he did not have time to forget, so he hired did not you help him military training in math. He may have a lot to ask, you need to quickly answer all inquiries.

Input Format

The first row of three integers n, m, q.

Next n lines of m integers, hi represents an integer of i-th row j-th column, j denotes the i-th row of the person's height and column j. Each integer guaranteed [1, n * m] occurs and only once.

Next q lines of two integers x, y, represents a group of query.

Output Format

Q total output lines, each an integer that represents the answer to the inquiry i-th group.

Sample input

3 3 2
1 8 9
3 2 7
6 5 4
3 3
2 2

Sample Output

3
2

data range

n,m≤1000,q≤5∗10^5

Resolve

Violence team each row, each column is sorted, we can count on each point it directly in the row and column number on how much bigger than it is. Then you know it the row, column on the rankings. Simply record it, then you can directly answer inquiries.

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#define N 1002
using namespace std;
struct node{
    int h,x,y,id;
}a[N*N],b[N];
int n,m,q,i,j,sum[N][N];
int read()
{
    char c=getchar();
    int w=0;
    while(c<'0'||c>'9') c=getchar();
    while(c<='9'&&c>='0'){
        w=w*10+c-'0';
        c=getchar();
    }
    return w;
}
int id(int x,int y)
{
    return (x-1)*m+y;
}
int my_comp(const node &x,const node &y)
{
    return x.h>y.h;
}
int main()
{
    n=read();m=read();q=read();
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            int p=id(i,j);
            a[p].h=read();
            a[p].id=p;
        }
    }
    for(i=1;i<=n;i++){
        int tmp=0;
        for(j=1;j<=m;j++) b[++tmp]=a[id(i,j)];
        sort(b+1,b+m+1,my_comp);
        for(j=1;j<=m;j++) a[b[j].id].x=j;
    }
    for(i=1;i<=m;i++){
        int tmp=0;
        for(j=1;j<=n;j++) b[++tmp]=a[id(j,i)];
        sort(b+1,b+n+1,my_comp);
        for(j=1;j<=n;j++) a[b[j].id].y=j;
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            int p=id(i,j);
            sum[a[p].x][a[p].y]++;
        }
    }
    for(i=1;i<=q;i++){
        int x=read(),y=read();
        printf("%d\n",sum[x][y]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/12305687.html