题解:P11826 [TOIP2024] 奇巧方块
作为一道黄题还是太难调了。
本文符合中国大陆的习惯,把题目中行和列反过来了。
做法
贡献答案的矩阵有两种,一种是不包含第
然后考虑每一个区间会包含多少
我们把这些东西离散化,然后差分即可。如果覆盖次数与
这里离散化的细节比较多。别忘了考虑区间和区间之间未被覆盖的部分,然后还有边界,我使用的左闭右开区间,边界是
代码
#include<bits/stdc++.h>
#define int long long
#define R(x) x=read()
using namespace std;
inline int read() {
int x=0;
char c=getchar();
while(c<'0'||c>'9') {
c=getchar();
}
while(c>='0'&&c<='9') {
x=(x<<1)+(x<<3)+(c^'0');
c=getchar();
}
return x;
}
const int N=500005;
int n,m,t,x,k,l[N],r[N],ans;
int pos[N],tot,ll[N],rr[N];
int c[N];
signed main() {
R(n),R(m),R(t),R(k),R(x);
for(int i=1; i<=t; ++i) {
R(r[i]),l[i]=max(1ll,r[i]-k+1);
r[i]=min(r[i],m-k+1);
if(r[i]<l[i])continue;
pos[++tot]=l[i],pos[++tot]=r[i];
if(r[i]+1<=m-k+1)pos[++tot]=r[i]+1;
if(l[i]-1>=1)pos[++tot]=l[i]-1;
}
pos[++tot]=1,pos[++tot]=m-k+2;
sort(pos+1,pos+1+tot);
tot=unique(pos+1,pos+1+tot)-pos-1;
for(int i=1; i<=t; ++i) {
if(r[i]<l[i])continue;
ll[i]=lower_bound(pos+1,pos+1+tot,l[i])-pos;
rr[i]=lower_bound(pos+1,pos+1+tot,r[i])-pos;
++c[ll[i]],--c[rr[i]+1];
}
for(int i=1; i<tot; ++i) {
c[i]+=c[i-1];
if((k^c[i])&1) ans+=(pos[i+1]-pos[i]);
}
ans*=(min(x+k-1,n)-max(x,k)+1);
if(k&1) ans+=max(0ll,(x-k)*(m-k+1))+max(0ll,(n-x+1-k)*(m-k+1));
cout<<ans;
return 0;
}