因为星星静态,没有必要树状数组,直接2*(n^2) 记录(1,1)到(i,j)的矩阵和,然后枚举任意两行,尺取即可;
AC代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int star[420][420];
int n,k;
int chi(int i,int j)
{
int l=1,r=1;
int minn=0x3f3f3f3f;
int h=(j-i)+1;
int num=(star[j][r]-star[i-1][r])-(star[j][l-1]-star[i-1][l-1]);
while (r<=n)
{
if (num==k)
{
minn=min(minn,h*(r-l+1));
l++;
if (l>r)
r=l;
if (l<=n)
num=(star[j][r]-star[i-1][r])-(star[j][l-1]-star[i-1][l-1]);
}
else if (num<k)
{
r++;
if (r<=n)
num=(star[j][r]-star[i-1][r])-(star[j][l-1]-star[i-1][l-1]);
}
else if (num>k)
{
l++;
if (l>r)
r=l;
if (l<=n)
num=(star[j][r]-star[i-1][r])-(star[j][l-1]-star[i-1][l-1]);
}
}
return minn;
}
int main()
{
int t,x,y;
scanf("%d",&t);
while (t--)
{
memset(star,0,sizeof(star));
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
star[x][y]=1;
}
int minn=0x3f3f3f3f;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
star[i][j]+=star[i][j-1];
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
star[i][j]+=star[i-1][j];
for (int i=1;i<=n;i++)
for (int j=i;j<=n;j++)
minn=min(chi(i,j),minn);
printf("%d\n",minn);
}
return 0;
}