Codeforces Gym 100741G Yet Another Median Task 二分乱搞

来源:互联网 发布:vscode下载插件 编辑:程序博客网 时间:2024/06/05 08:20

G. Yet Another Median Task
time limit per test
2.0 s
memory limit per test
64 MB
input
standard input
output
standard output

You're given a matrix a with n lines and n columns. We define the median of an array the element from the middle position of sorted array. If there are two such positions (n is even), get the element whose position is minimal.

Answer q queries of form: what's the median element of a submatrix (x1, y1, x2, y2). Formally, we consider all elements a[i][j] such as x1 <= i <= x2 and y1 <= j <= y2 and add them into a temporary array. Then, get the median of the array.

Input

First line of the input contains numbers n (1 ≤ n ≤ 800) and q (1 ≤ q ≤ 1000). Next n lines contain nnumbers, describing the 1-based matrix a. All elements from a can fit into an int type. Next q lines contain numbers x1, y1, x2, y2, describing a query (1 ≤ x1 ≤ x2 ≤ n , 1 ≤ y1 ≤ y2 ≤ n)

Output

Output q lines, each line to answer a query.

Examples
input
2 41 22 21 1 2 11 1 1 21 1 2 22 2 2 2
output
1122



给你一个矩阵,q次询问,每次问你子矩阵拉成一维数组排序后的中位数是多少。


二分答案,每次二分把子矩阵扫一遍,看比当前二分数字小的数有多少个。

这样乱搞也能过,真心佩服自己啊~ (小小自恋一把


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=805,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);  int a[maxn][maxn];int p[maxn*maxn];int main() {int n,i,j,k,l,r,u,d,q;ll m=-llinf,w=llinf;scanf("%d%d",&n,&q);for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {scanf("%d",&a[i][j]);m=max(m,(ll)a[i][j]);w=min(w,(ll)a[i][j]);}}for (i=1;i<=q;i++) {scanf("%d%d%d%d",&u,&l,&d,&r);int lc=w,rc=m,mid,tot=(r-l+1)*(d-u+1),ans;if (tot%2) tot/=2; else tot=tot/2-1;while (lc<=rc) {int cnt=0;mid=(lc+rc)/2;for (j=u;j<=d;j++) {for (k=l;k<=r;k++) {if (a[j][k]<mid) cnt++;}}if (cnt<=tot) lc=mid+1,ans=mid; else {rc=mid-1;}    }    printf("%d\n",ans);}return 0;}


原创粉丝点击