#include<bits/stdc++.h>
#define max(x,y) (x>y?x:y)
#define lowbit(x) (x&(-x))
using namespace std;
int n,m,Max_num;
int ans[600005],now;
struct data{
int x,y,val;
void read(){scanf("%d",&val);}
bool operator < (const data &r) const{return val<r.val;}
}a[505*505];
struct Ques{
int x1,yl,x2,y2,k,num;
void read(){scanf("%d%d%d%d%d",&x1,&yl,&x2,&y2,&k);}
}q[600005],tmp[600005];
int tree[505][505];
void add(int x,int y,int num){
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
tree[i][j]+=num;
}
int search(int x,int y){
int re=0;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
re+=tree[i][j];
return re;
}
void binary_search(int x,int y,int l,int r){
int mid=x+y>>1;
if(x>y || l>r) return;
while(a[now+1].val<=mid && now!=n*n){
++now;
add(a[now].x,a[now].y,1);
}
while(a[now].val>mid && now){
add(a[now].x,a[now].y,-1);
--now;
}
int L=l,R=r;
for(int i=l;i<=r;++i){
int temp=search(q[i].x2,q[i].y2)+search(q[i].x1-1,q[i].yl-1)-search(q[i].x1-1,q[i].y2)-search(q[i].x2,q[i].yl-1);
if(q[i].k<=temp){
ans[q[i].num]=mid;
tmp[L++]=q[i];
}
else tmp[R--]=q[i];
}
memcpy(q+l,tmp+l,sizeof(q[0])*(r-l+1));
binary_search(x,mid-1,l,L-1);
binary_search(mid+1,y,R+1,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
a[(i-1)*n+j].x=i;
a[(i-1)*n+j].y=j;
a[(i-1)*n+j].read();
Max_num=max(Max_num,a[(i-1)*n+j].val);
}
}
for(int i=1;i<=m;++i){
q[i].read();
q[i].num=i;
}
sort(a+1,a+n*n+1);
binary_search(0,Max_num,1,m);
for(int i=1;i<=m;++i) printf("%d\n",ans[i]);
return 0;
}
【bzoj 2738】矩阵乘法(整体二分+树状数组)
最新推荐文章于 2021-06-14 22:54:33 发布