[ABC278E] Grid Filling
题意
给定 的一个矩形,矩形中每个点有一个不超过 的正整数。
你需要对于 的所有 ,将 的 都在矩形中涂成黑色,求出没有被涂黑的数有多少种,即有多少种不一样的数。
询问的 是独立的,即涂黑操作在每次询问后将会还原。
解法
考虑按照题意模拟,复杂度是 ,显然通不过,我们可以看数据范围,像是一个 的。
我们可以考虑枚举 ,然后可以发现,随着 的增加,每次都是将一列的贡献涂成白色,一列涂成黑色,其余的列都被两次同时覆盖。然后就做完了。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 305, INF = 2e9, MOD = 1e9 + 7;
inline int read()
{
int op = 1, x = 0;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
while (ch == '-')
{
op = -op;
ch = getchar();
}
while (ch >= '0' and ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * op;
}
int cnt[N], res;
inline void write(int x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int H, W, n, h, w, a[N][N], ans[N][N], rcnt[N];
void add(int x)
{
if(++cnt[x]==1) res++;
}
void del(int x)
{
if(--cnt[x]==0) res--;
}
int rres;
int main()
{
// freopen("*.in", "r", stdin);
// freopen("*.out", "w", stdout);
H=read(),W=read(),n=read(),h=read(),w=read();
for(int i=1;i<=H;i++)
{
for(int j=1;j<=W;j++) a[i][j]=read(), add(a[i][j]);
}
for(int i=0;i<=H-h;i++)
{
for (int j=1;j<=n;j++) rcnt[j]=cnt[j];
rres=res;
int nl=i+1,nr=i+h;
int kl=1,kr=w;
for(int j=nl;j<=nr;j++)
{
for(int k=kl;k<=kr;k++) del(a[j][k]);
}
ans[i][0]=res;
for(int j=1;j<=W-w;j++)
{
for(int k=nl;k<=nr;k++)
{
add(a[k][j]);
}
int rr=j+w;
for(int k=nl;k<=nr;k++) del(a[k][rr]);
ans[i][j]=res;
}
for(int i=1;i<=n;i++) cnt[i]=rcnt[i];
res=rres;
}
for(int i=0;i<=H-h;i++)
{
for(int j=0;j<=W-w;j++)
{
printf("%d ", ans[i][j]);
}
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号