Codeforces 628E:Zbazi in Zeydabad 树状数组

本文介绍了一种算法,用于在一个给定的矩阵中寻找特定的Z图案,并计算这些图案的数量。该算法首先预处理矩阵以获取每个单元格的左侧、右侧及左下侧的连续‘z’字符的最大数量,接着利用树状数组进行高效查询和更新,实现快速计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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值插入到这个数组里,直接就可以筛出有多少个符合条件的点了。

代码:

[cpp]  view plain  copy
  1. #pragma warning(disable:4996)  
  2. #include <iostream>  
  3. #include <functional>  
  4. #include <algorithm>  
  5. #include <cstring>  
  6. #include <vector>  
  7. #include <string>  
  8. #include <cstdio>  
  9. #include <cmath>  
  10. #include <queue>  
  11. #include <stack>  
  12. #include <deque>  
  13. #include <set>  
  14. #include <map>  
  15. using namespace std;  
  16. typedef long long ll;  
  17.   
  18. #define INF 0x3fffffffffffffff  
  19.   
  20. const ll mod = 1e9 + 7;  
  21. const int maxn = 3005;  
  22.   
  23. int n, m;  
  24. char val[maxn][maxn];  
  25. int le[maxn][maxn], ri[maxn][maxn], ledown[maxn][maxn];  
  26. int tree[maxn * 2][maxn];  
  27. vector< pair<intint> >ed[maxn];  
  28.   
  29. int lowbit(int x)  
  30. {  
  31.     return x&(-x);  
  32. }  
  33.   
  34. void add(int num, int i, int x)  
  35. {  
  36.     while (i <= 3000)  
  37.     {  
  38.         tree[num][i] += x;  
  39.         i = i + lowbit(i);  
  40.     }  
  41. }  
  42.   
  43. int getsum(int num, int i)  
  44. {  
  45.     int res = 0;  
  46.     while (i > 0)  
  47.     {  
  48.         res += tree[num][i];  
  49.         i -= lowbit(i);  
  50.     }  
  51.     return res;  
  52. }  
  53.   
  54. void init()  
  55. {  
  56.     int i, j;  
  57.     scanf("%d%d", &n, &m);  
  58.   
  59.     for (i = 1; i <= n; i++)  
  60.     {  
  61.         scanf("%s", val[i] + 1);  
  62.     }  
  63.   
  64.     memset(le, 0, sizeof(le));  
  65.     memset(ri, 0, sizeof(ri));  
  66.     memset(ledown, 0, sizeof(ledown));  
  67.   
  68.     for (i = 1; i <= n; i++)  
  69.     {  
  70.         for (j = 1; j <= m; j++)  
  71.         {  
  72.             if (val[i][j] != 'z')  
  73.             {  
  74.                 le[i][j] = 0;  
  75.             }  
  76.             else  
  77.             {  
  78.                 le[i][j] = le[i][j - 1] + 1;  
  79.             }  
  80.         }  
  81.         for (j = m; j >= 1; j--)  
  82.         {  
  83.             if (val[i][j] != 'z')  
  84.             {  
  85.                 ri[i][j] = 0;  
  86.             }  
  87.             else  
  88.             {  
  89.                 ri[i][j] = ri[i][j + 1] + 1;  
  90.             }  
  91.         }  
  92.         for (j = 1; j <= m; j++)  
  93.         {  
  94.             if (val[i][j] == 'z')  
  95.             {  
  96.                 ed[j + ri[i][j] - 1].push_back(make_pair(i, j));  
  97.             }  
  98.         }  
  99.     }  
  100.   
  101.     for (i = n; i >= 1; i--)  
  102.     {  
  103.         for (j = 1; j <= m; j++)  
  104.         {  
  105.             if (val[i][j] != 'z')  
  106.             {  
  107.                 ledown[i][j] = 0;  
  108.             }  
  109.             else  
  110.             {  
  111.                 ledown[i][j] = ledown[i + 1][j - 1] + 1;  
  112.             }  
  113.         }  
  114.     }  
  115.   
  116. }  
  117.   
  118. void solve()  
  119. {  
  120.     int i, j, sz;  
  121.     ll res = 0;  
  122.     for (j = m; j >= 1; j--)  
  123.     {  
  124.         sz = ed[j].size();  
  125.         for (i = 0; i < sz; i++)  
  126.         {  
  127.             int x = ed[j][i].first;  
  128.             int y = ed[j][i].second;  
  129.             add(x + y, y, 1);  
  130.         }  
  131.         for (i = 1; i <= n; i++)  
  132.         {  
  133.             if (val[i][j] != 'z')continue;  
  134.             int r = min(le[i][j], ledown[i][j]);  
  135.   
  136.             res += (ll)(getsum(i + j, j) - getsum(i + j, j - r));  
  137.         }  
  138.     }  
  139.     printf("%I64d", res);  
  140. }  
  141.   
  142. int main()  
  143. {  
  144. #ifndef ONLINE_JUDGE  
  145.     freopen("i.txt""r", stdin);  
  146.     freopen("o.txt""w", stdout);  
  147. #endif  
  148.   
  149.     init();  
  150.     solve();  
  151.   
  152.     return 0;  
  153. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值