题意:给定n*m 的格子矩阵,一些位置上有障碍物,问从(1,1)出发两步之内能到达的格子数量。
比较容易想到用树状数组,但是之后的细节比较多就写不对了。。
假设一个格子坐标为(x,y),如果他的正上方或者正左边有障碍物,那么这个格子一定不能到达。那么我们就比较容易可以想到对一个格子看他右上有多少列有格子,这个地方也可以用树状数组查询,
有两个特殊情况是第一行和第一列,对于这两个地方我们要添加额外的障碍物,也就是在不能从(1,1)向右直接到达的第一个格子后面全部加上障碍物,第一列同理。
重要的就是排序了,先按照x坐标从小到大,x一样y从小到大,然后每处理完一行,就可以查询一次,统计不能到达的点的个数,最后减掉。
#include <iostream>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <queue>
#include <vector>
using namespace std;
#define IO \
ios::sync_with_stdio(false); \
// cin.tie(0); \
// cout.tie(0);
typedef long long LL;
const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
struct Node
{
int x, y;
bool operator<(const Node a) const
{
return x == a.x ? y < a.y : x < a.x;
}
bool operator==(const Node a) const
{
return x == a.x && y == a.y;
}
} a[maxn];
bool vis[maxn];
LL c[maxn];
int n, m, k;
int lowbit(int x)
{
return x & (-x);
}
void add(int n, int x, int k)
{
while (x <= n)
{
c[x] += k;
x += lowbit(x);
}
return;
}
LL ask(int x)
{
LL ans = 0;
while (x >= 1)
{
ans += c[x];
x -= lowbit(x);
}
return ans;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int minx = inf, miny = inf;
cin >> n >> m >> k;
for (int i = 1; i <= k; i++)
{
cin >> a[i].x >> a[i].y;
if (a[i].x == 1)
miny = min(miny, a[i].y);
if (a[i].y == 1)
minx = min(minx, a[i].x);
}
for (int i = minx + 1; i <= n; i++)
{
++k;
a[k].x = i;
a[k].y = 1;
}
for (int i = miny + 1; i <= m; i++)
{
++k;
a[k].x = 1;
a[k].y = i;
}
sort(a + 1, a + k + 1);
int cnt = unique(a + 1, a + k + 1) - a - 1;
LL ans = 0, h = 1;
for (int i = 1; i <= cnt; i++)
{
if (!vis[a[i].y])
{
vis[a[i].y] = true;
add(m, a[i].y, 1);
}
if (i == cnt || a[i].x != a[i + 1].x)
{
ans += 1LL * (ask(m) - ask(a[h].y - 1));
h = i + 1;
}
// cout << a[i].x << " " << a[i].y << " " << h << " " << ans << endl;
}
cout << 1LL * n * m - ans;
return 0;
}