ran_qwq @ 2023-12-30 18:47:17
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int,int>
#define fir first
#define sec second
#define mem(arr,val) memset(arr,val,sizeof arr)
using namespace std;
const int N=510,M=60010,INF=0x3f3f3f3f;
int read() {int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;}
void Write(int x) {if(x<0) {putchar('-'),Write(-x);return;}if(x<10) {putchar(x+'0');return;}Write(x/10),putchar(x%10+'0');}
void write(int x,char *s) {Write(x),printf("%s",s);}
int n,m,len,blQ,blV,tot,a[N][N],b[N*N],posQ[N],posV[N*N],L[N],R[N],c[N],d[N],ans[M];
struct node {int x,y,s,t,k,id;}q[M];
bool cmp(node c,node d)
{
if(posQ[c.x]!=posQ[d.x]) return posQ[c.x]<posQ[d.x];
if(posQ[c.y]!=posQ[d.y]) return posQ[c.x]&1?c.y<d.y:c.y>d.y;
if(posQ[c.s]!=posQ[d.s]) return posQ[c.y]&1?c.s<d.s:c.s>d.s;
return posQ[c.s]&1?c.t<d.t:c.t>d.t;
}
void add(int x,int y) {c[a[x][y]]++,d[posV[a[x][y]]]++;}
void del(int x,int y) {c[a[x][y]]--,d[posV[a[x][y]]]--;}
void addX(int x,int l,int r) {for(int i=l;i<=r;i++) add(x,i);}
void delX(int x,int l,int r) {for(int i=l;i<=r;i++) del(x,i);}
void addY(int x,int l,int r) {for(int i=l;i<=r;i++) add(i,x);}
void delY(int x,int l,int r) {for(int i=l;i<=r;i++) del(i,x);}
int query(int k)
{
int cnt=0;
for(int i=1;i<=posV[n*n];i++)
{
if(cnt+d[i]<k) cnt+=d[i];
else
for(int j=L[i];j<=R[i];j++)
{
if(cnt+c[j]<k) cnt+=c[j];
else return b[j];
}
}
return INF;
}
void solve()
{
n=read(),m=read(),blQ=n/sqrt(sqrt(m))/3+1,blV=n+1;
for(int i=1;i<=n;i++) posQ[i]=(i-1)/blQ+1;
for(int i=1;i<=n*n;i++) posV[i]=(i-1)/blV+1;
tot=posV[n*n];
for(int i=1;i<=tot;i++) L[i]=(i-1)*blV+1,R[i]=min(i*blV,n*n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) a[i][j]=read(),b[++len]=a[i][j];
sort(b+1,b+1+len),len=unique(b+1,b+1+len)-b-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) a[i][j]=lower_bound(b+1,b+1+len,a[i][j])-b;
for(int i=1;i<=m;i++) q[i]={read(),read(),read(),read(),read(),i};
sort(q+1,q+1+m,cmp),add(1,1);
for(int i=1,x=1,y=1,s=1,t=1;i<=m;i++)
{
while(x>q[i].x) addX(--x,y,t);
while(y>q[i].y) addY(--y,x,s);
while(s<q[i].s) addX(++s,y,t);
while(t<q[i].t) addY(++t,x,s);
while(x<q[i].x) delX(x++,y,t);
while(y<q[i].y) delY(y++,x,s);
while(s>q[i].s) delX(s--,y,t);
while(t>q[i].t) delY(t--,x,s);
ans[q[i].id]=query(q[i].k);
}
for(int i=1;i<=m;i++) write(ans[i],"\n");
}
signed main()
{
int T=1;
while(T--) solve();
}
by sto_5k_orz @ 2023-12-30 19:38:35
@ran_qwq 查询的时候应该将
by sto_5k_orz @ 2023-12-30 19:45:34
还有你这个膜堆顺序好像不太对。
by ran_qwq @ 2023-12-30 19:51:21
@sto_5k_orz 改成
by sto_5k_orz @ 2023-12-30 19:53:16
哦我过的膜堆顺序好像和你一样
by sto_5k_orz @ 2023-12-30 19:54:38
你可以看一下我的 code,我一眼没瞪出来
by Wf_yjqd @ 2023-12-30 21:40:23
@ran_qwq WA的话不清楚,如果TLE可以看 我的帖子