传送门
题意:
思路: 算是个整体二分的板子啦,不过这个是二维的矩阵,我们只需要把一位树状数组改成二维的,让后动态维护单点加,区间查询前缀和即可。
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
#define lowbit(x) ((x)&(-x))
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n,m;
int a[N],tot,ans[N];
int tr[510][510];
struct Node
{int x1,y1,val,op,x2,y2,id;
}q[N],q1[N],q2[N];void add(int x,int y,int c)
{for(int i=x;i<=n;i+=lowbit(i))for(int j=y;j<=n;j+=lowbit(j))tr[i][j]+=c;
}int sum(int x,int y)
{int ans=0;for(int i=x;i;i-=lowbit(i))for(int j=y;j;j-=lowbit(j))ans+=tr[i][j];return ans;
}int get_sub(int x1,int y1,int x2,int y2)
{int ans=0;ans+=sum(x2,y2);ans+=sum(x1-1,y1-1);ans-=sum(x1-1,y2);ans-=sum(x2,y1-1);return ans;
}void solve(int l,int r,int left,int right)
{if(l>r||left>right) return;if(l==r){for(int i=left;i<=right;i++) if(q[i].op==2) ans[q[i].id]=a[l];return;}int mid=l+r>>1;int cnt1,cnt2; cnt1=cnt2=0;for(int i=left;i<=right;i++){if(q[i].op==1){if(q[i].val<=a[mid]) add(q[i].x1,q[i].y1,1),q1[++cnt1]=q[i];else q2[++cnt2]=q[i];}else{int s=get_sub(q[i].x1,q[i].y1,q[i].x2,q[i].y2);if(q[i].val<=s) q1[++cnt1]=q[i];//别写错符号!else q[i].val-=s,q2[++cnt2]=q[i];}}for(int i=1;i<=cnt1;i++) if(q1[i].op==1) add(q1[i].x1,q1[i].y1,-1);for(int i=1;i<=cnt1;i++) q[i+left-1]=q1[i];for(int i=1;i<=cnt2;i++) q[left+cnt1+i-1]=q2[i];solve(l,mid,left,left+cnt1-1); solve(mid+1,r,left+cnt1,right);
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { int x; scanf("%d",&x); q[++tot]={i,j,x,1}; a[tot]=x; }sort(a+1,a+1+n*n);for(int i=1;i<=m;i++){int x1,y1,x2,y2,k; scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);q[++tot]={x1,y1,k,2,x2,y2,i};}solve(1,n*n,1,tot);for(int i=1;i<=m;i++) printf("%d\n",ans[i]);return 0;
}
/**/