[ABC420F] kirinuki 解题报告
Chenhaoxuan · · 题解
简要题意:给定一个大小为
前置知识:P1950 长方形。
在求解上面的问题时,对于第
如果不考虑面积
在加上面积
-
设
x=j-l_j,y=r_j-j 。不妨x<y ,反之同理。 -
刻画 不考虑面积
\le k ,并固定高度h=h_j 时,矩形的 宽为w 的方案数,有如下的分段函数:-
f(h,w)=\begin{cases}w,&w\in [1,x)\\x,&w\in[x,y)\\ x+y-w,&w\in[y,x+y)\\0,&w\ge x+y\end{cases}
-
-
不难发现上式是一个可以二阶差分的式子。于是我们可以反推出矩形宽度为
w 时的方案数。 -
加入高度条件,容易发现高度更大的必然满足高度较小的要求,即对于
h\text{ from }n-1 \text{ to } 1 ,令f(h,w)\gets f(h,w)+f(h+1,w) 。这样就求出了在高度为h 时,宽度为w 的矩形个数。 -
最后统计答案时,如果
h\times w\le k ,那么将这种情况计入答案。
时间复杂度
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int maxn = 5e5 + 10;
int n, m, k, h[maxn], r[maxn];
vector<int> a[maxn];
vector<long long> dp[maxn];
stack<int> stk;
long long ans = 0;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
a[i].resize(m + 2);
dp[i].resize(m + 3);
for (int j = 1; j <= m; j++) {
char c = getchar();
while (c != '#' && c != '.') c = getchar();
if (c == '#') a[i][j] = 1;
else a[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (j <= m) {
if (a[i][j] == 0) h[j]++;
else h[j] = 0;
}
}
for (int j = m; j >= 1; j--) {
while (!stk.empty() && h[stk.top()] > h[j]) stk.pop();
r[j] = stk.empty() ? m + 1 : stk.top(); stk.push(j);
}
while (stk.size()) stk.pop();
for (int j = 1; j <= m; j++) {
while (!stk.empty() && h[stk.top()] >= h[j]) stk.pop();
int l = stk.empty() ? 0 : stk.top();
// [l] ... j ... [r],不妨 r-j >= j-l
// 1:+1 2:+2 ... j-l+1:+(j-l+1) j-l+2:+(j-l+1) ...
// r-j+1:+(j-l+1) r-j+2:+(j-l) ... r-l+1:+1 r-l+2:0 r-l+3:0
// 差分后: 011...110 0...00-1-1...-1-100
// 再次差分后:010...00-10...00-1 0... 0 010
if (h[j]) {
int x = j - l, y = r[j] - j; if (x > y) swap(x, y);
dp[h[j]][1]++; dp[h[j]][x + 1]--;
dp[h[j]][y + 1]--; dp[h[j]][x + y + 1]++;
}
stk.push(j);
}
while (stk.size()) stk.pop();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= m; j++) dp[i][j] += dp[i][j - 1];
for (int j = 1; j <= m; j++) dp[i][j] += dp[i][j - 1];
for (int j = 1; j <= m; j++) {
if (i != n) dp[i][j] += dp[i + 1][j];
if (i * j <= k) ans += dp[i][j];
}
}
printf("%lld", ans);
return 0;
}