P2154: [SDOI2009] The Pious Tomb Owner — Solution
Approach
This problem is really interesting. If we enumerate by brute force, the formula for the answer is:
\[ans = \sum_{x}^n \sum_{y}^m {up_{x,y} \choose k} {left_{x,y} \choose k} {right_{x,y} \choose k} {down_{x,y} \choose k}\]
where \(up, left, right, down\) denote the numbers of evergreen trees above, below, to the left, and to the right. How do we bring the complexity down?
First, we can consider coordinate compression: any case where one of up, down, left, right is \(0\) cannot possibly contribute to the answer. Then, we notice that the up-down part within a gap:
\[ {up_{x,y} \choose k} {down_{x,y} \choose k} \]
stays the same, so when computing a gap we just multiply this part by the height of the gap. The problem is how to handle the left-right contribution. Consider that every tree contributes to the gaps on its left; supposing its vertical coordinate is \(y\), we can consider changing position \(y\) of the Fenwick tree to:
\[ {TotalY[y] - cnt[y] \choose k} {cnt[y] \choose k} \]
where TotalY[y] is how many trees there are in total in row \(y\), and cnt[y] is how many trees have already been placed. By the distributive law of multiplication, we can quickly compute the product of a gap's vertical and horizontal contributions, and then add it to the answer.
Code
// P2154.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll MAX_N = 100100, mod = 2147483648;
ll n, m, k, w;
ll C[MAX_N][15], tree[MAX_N], btmpx[MAX_N << 1], bxtot;
ll xtot[MAX_N], ytot[MAX_N], last[MAX_N], lasty[MAX_N];
ll lenY, btmpy[MAX_N << 1], bytot;
struct pr
{
ll first, second;
bool operator<(const pr &p) const { return first < p.first || (p.first == first && second < p.second); }
} prs[MAX_N];
inline ll lowbit(int x) { return x & (-x); }
void add(int x, ll d)
{
while (x <= lenY)
tree[x] = (tree[x] + d + mod) % mod, x += lowbit(x);
}
ll sum(int x)
{
if (x == 0)
return 0;
ll ans = 0;
while (x)
ans = (ans + tree[x] + mod) % mod, x -= lowbit(x);
return ans;
}
void place(int y)
{
if (last[y])
add(y, -last[y]), last[y] = 0;
lasty[y]++;
if (lasty[y] >= k && ytot[y] - lasty[y] >= k)
last[y] = C[lasty[y]][k] * C[ytot[y] - lasty[y]][k] % mod, add(y, last[y]);
}
ll query(ll l, ll r)
{
if (l > r)
return 0;
return sum(r) - sum(l - 1);
}
int main()
{
scanf("%lld%lld%lld", &n, &m, &w);
for (int i = 1; i <= w; i++)
scanf("%lld%lld", &prs[i].first, &prs[i].second), btmpx[++bxtot] = prs[i].first, btmpy[++bytot] = prs[i].second;
scanf("%lld", &k);
sort(btmpx + 1, btmpx + 1 + bxtot), sort(btmpy + 1, btmpy + 1 + bytot);
bxtot = unique(btmpx + 1, btmpx + 1 + bxtot) - btmpx - 1;
bytot = unique(btmpy + 1, btmpy + 1 + bytot) - btmpy - 1;
for (int i = 1; i <= w; i++)
{
prs[i].first = lower_bound(btmpx + 1, btmpx + 1 + bxtot, prs[i].first) - btmpx;
prs[i].second = lower_bound(btmpy + 1, btmpy + 1 + bytot, prs[i].second) - btmpy;
xtot[prs[i].first]++, ytot[prs[i].second]++;
lenY = max(lenY, prs[i].second);
}
sort(prs + 1, prs + 1 + w);
C[0][0] = 1;
for (int i = 1; i <= w; i++)
for (int j = 0; j <= k; j++)
C[i][j] = (C[i - 1][j] + (j ? C[i - 1][j - 1] : 0)) % mod;
ll ans = 0, xcnt = 0, lx = -1;
for (int i = 1; i <= w; i++)
{
if (lx != prs[i].first)
xcnt = 0, lx = prs[i].first;
xcnt++;
if (xcnt >= k && xtot[lx] - xcnt >= k && prs[i + 1].first == lx)
ans = (ans + ((C[xcnt][k] * C[xtot[lx] - xcnt][k] % mod) * query(prs[i].second + 1, prs[i + 1].second - 1) % mod)) % mod;
place(prs[i].second);
}
printf("%lld", (ans + mod) % mod);
return 0;
}