Codeforces 628E:Zbazi in Zeydabad 树状数组的奇妙用法

E. Zbazi in Zeydabad
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A tourist wants to visit country Zeydabad for Zbazi (a local game in Zeydabad).

The country Zeydabad is a rectangular table consisting of n rows and m columns. Each cell on the country is either 'z' or '.'.

The tourist knows this country is named Zeydabad because there are lots of ''Z-pattern"s in the country. A ''Z-pattern" is a square which anti-diagonal is completely filled with 'z' and its upper and lower rows are also completely filled with 'z'. All other cells of a square can be arbitrary.

Note that a ''Z-pattern" can consist of only one cell (see the examples).

So he wants to count the number of ''Z-pattern"s in the country (a necessary skill for Zbazi).

Now your task is to help tourist with counting number of ''Z-pattern"s.

As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to usegets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/System.out in Java.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 3000) — the number of rows and columns respectively.

Each of the next n lines contains m characters 'z' or '.' — the description of Zeydabad.

Output

Print the only integer a — the number of ''Z-pattern"s in Zeydabad.

Examples
input
4 4
zzzz
zzz.
.z..
zzzz
output
16
input
1 4
z.z.
output
2
input
2 2
zz
zz
output
5

题意就是给出了一个矩阵,里面有实心有空心,问由这些实心的方格可以组成多少个z型。

这个题我读完,一脸懵逼。。。除了暴力之外完全不知道如何下手。。。

看了其他人的代码之后,可能是因为自己做题太太太少的缘故,感觉实在是太奇妙了。。。这也就是为什么算法好玩的一个原因。。。

求出连续实心的最长的左边长度,右边长度,左下长度。然后就是枚举z的右上端点,正常来讲是对每一个右上端点,取其左边长度和与左下长度的较小值,这样找到了这个z型的左下端点,之后通过这个左下端点的右边长度,求这样有多少个z,但这样明显会T。

奇妙的来了,造了一个6000的树状数组,每一个树状数组代表着自己对角线那部分点,这样只要是在这个树状数组里面的,就已经说明了一定符合右上端点对角线的要求。这样把其y值插入到这个数组里,直接就可以筛出有多少个符合条件的点了。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffffffffffff

const ll mod = 1e9 + 7;
const int maxn = 3005;

int n, m;
char val[maxn][maxn];
int le[maxn][maxn], ri[maxn][maxn], ledown[maxn][maxn];
int tree[maxn * 2][maxn];
vector< pair<int, int> >ed[maxn];

int lowbit(int x)
{
	return x&(-x);
}

void add(int num, int i, int x)
{
	while (i <= 3000)
	{
		tree[num][i] += x;
		i = i + lowbit(i);
	}
}

int getsum(int num, int i)
{
	int res = 0;
	while (i > 0)
	{
		res += tree[num][i];
		i -= lowbit(i);
	}
	return res;
}

void init()
{
	int i, j;
	scanf("%d%d", &n, &m);

	for (i = 1; i <= n; i++)
	{
		scanf("%s", val[i] + 1);
	}

	memset(le, 0, sizeof(le));
	memset(ri, 0, sizeof(ri));
	memset(ledown, 0, sizeof(ledown));

	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= m; j++)
		{
			if (val[i][j] != 'z')
			{
				le[i][j] = 0;
			}
			else
			{
				le[i][j] = le[i][j - 1] + 1;
			}
		}
		for (j = m; j >= 1; j--)
		{
			if (val[i][j] != 'z')
			{
				ri[i][j] = 0;
			}
			else
			{
				ri[i][j] = ri[i][j + 1] + 1;
			}
		}
		for (j = 1; j <= m; j++)
		{
			if (val[i][j] == 'z')
			{
				ed[j + ri[i][j] - 1].push_back(make_pair(i, j));
			}
		}
	}

	for (i = n; i >= 1; i--)
	{
		for (j = 1; j <= m; j++)
		{
			if (val[i][j] != 'z')
			{
				ledown[i][j] = 0;
			}
			else
			{
				ledown[i][j] = ledown[i + 1][j - 1] + 1;
			}
		}
	}

}

void solve()
{
	int i, j, sz;
	ll res = 0;
	for (j = m; j >= 1; j--)
	{
		sz = ed[j].size();
		for (i = 0; i < sz; i++)
		{
			int x = ed[j][i].first;
			int y = ed[j][i].second;
			add(x + y, y, 1);
		}
		for (i = 1; i <= n; i++)
		{
			if (val[i][j] != 'z')continue;
			int r = min(le[i][j], ledown[i][j]);

			res += (ll)(getsum(i + j, j) - getsum(i + j, j - r));
		}
	}
	printf("%I64d", res);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif

	init();
	solve();

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于高效处理区间和查询的数据结构,常用于解一维数组的前缀和、区间更新和查询等问题。 在 Codeforces 上,树状数组常被用来解决一些与区间和查询有关的问题。它可以在 O(logn) 的时间内完成单点更新和查询,以及区间求和等操作。 下面是一个简单的示例代码,展示了如何实现一个基本的树状数组: ```cpp #include <iostream> #include <vector> using namespace std; // 获取最低位的 1 int getLowbit(int x) { return x & -x; } // 树状数组的单点更新操作 void update(vector<int>& fenwick, int index, int delta) { while (index < fenwick.size()) { fenwick[index] += delta; index += getLowbit(index); } } // 树状数组的前缀和查询操作 int query(vector<int>& fenwick, int index) { int sum = 0; while (index > 0) { sum += fenwick[index]; index -= getLowbit(index); } return sum; } int main() { int n; cin >> n; vector<int> fenwick(n + 1, 0); // 初始化树状数组 for (int i = 1; i <= n; i++) { int val; cin >> val; update(fenwick, i, val); } // 进行查询操作 int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int index, delta; cin >> index >> delta; update(fenwick, index, delta); } else if (type == 2) { int l, r; cin >> l >> r; int sum = query(fenwick, r) - query(fenwick, l - 1); cout << sum << endl; } } return 0; } ``` 在这个示例中,我们使用了一个长度为 n 的数组 `fenwick` 来表示树状数组。`update` 函数用于更新树状数组中的某个元素,`query` 函数用于查询树状数组中某个区间的和。 你可以根据具体问题的要求进行相应的修改和扩展。希望对你有所帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值