# Description

20%的数据：N<=100,Q<=1000；
40%的数据：N<=300,Q<=10000；
60%的数据：N<=400,Q<=30000；
100%的数据：N<=500,Q<=60000。

# Code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define lowbit(x) ((x)&(-(x)))

const int N=605;
const int G=80005;

struct Q {int x1,y1,x2,y2,k,id,ans;} q[G],tmp[G];
struct data {int x,y,w;} a[N*N];

int c[N][N],rec[G];
int now,tot,n;

int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}

for (int i=a.x;i<=n;i+=lowbit(i)) {
for (int j=a.y;j<=n;j+=lowbit(j)) {
c[i][j]+=v;
}
}
}

int get(int x,int y) {
int ret=0;
for (int i=x;i;i-=lowbit(i)) {
for (int j=y;j;j-=lowbit(j)) {
ret+=c[i][j];
}
}
return ret;
}

void solve(int l,int r,int L,int R) {
if (l>r) return ;
if (L==R) {
rep(i,l,r) q[i].ans=L;
return ;
}
int mid=(L+R)>>1;
int cnt=0;
rep(i,l,r) {
int res=get(q[i].x2,q[i].y2)+get(q[i].x1-1,q[i].y1-1);
res=res-get(q[i].x2,q[i].y1-1)-get(q[i].x1-1,q[i].y2);
if (res>=q[i].k) {
cnt++; rec[i]=1;
} else rec[i]=2;
}
int tl=l,tr=l+cnt;
rep(i,l,r) if (rec[i]==1) tmp[tl++]=q[i];
else tmp[tr++]=q[i];
rep(i,l,r) q[i]=tmp[i];
solve(l,l+cnt-1,L,mid);
solve(l+cnt,r,mid+1,R);
}

bool cmp1(data a,data b) {
return a.w<b.w;
}

bool cmp2(Q a,Q b) {
return a.id<b.id;
}

int main(void) {
std:: sort(a+1,a+tot+1,cmp1);
solve(1,T,1,a[tot].w);
std:: sort(q+1,q+T+1,cmp2);
rep(i,1,T) printf("%d\n", q[i].ans);
return 0;
}

• 评论

• 上一篇
• 下一篇