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
题意:
给出一个n*n的矩阵,问有多少个子矩阵满足副对角线、最上面的行、最下面的行都是由z构成(其他位置无关)(理解一下,这样的图形构成了一个Z的图案)。

  

题解:
如果预处理出每个点向左向右连续的z能够有多长,
那么先枚举副对角线,
由于答案肯定是由副对角线上连续的一段z贡献的,
所以我们可以一段段z来做。
问题转换成了,在这一段z上,有多少个点对(i,j)满足
left[i]>=j - i, right[j] >= j - i,
也就是说,i、j必须能够分别用left[i]、right[j]的长度分别覆盖对方。 那么我们可以枚举j,确定有多少i对它进行了贡献。
那么对于每个i,我们都能够知道它最远能够贡献到哪里,
每当我们的j超过了这个i最远能贡献的距离,就将它排去,
因为i不可能再对能够覆盖它的j左贡县。
对于每个j,现在所有能够留下来的i都是能够覆盖它的。
能够对它有贡献的i就是那些它能覆盖的i。 所以保持i能覆盖当前的j这一步可以使用优先队列完成,
统计有多少i贡献j能够用树状数组完成。 挺水的。

  

 #include <cstdio>
#include <queue>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define clr(x, y) memset(x, y, sizeof(x))
#define mk make_pair const int N = ;
typedef long long ll;
int n, m;
char graph[N][N];
int lef[N][N], rig[N][N]; ll sum[N];
priority_queue<pair<int, int> > outQue; int lowbit(int x) {
return x & (-x);
} int len;
void add(ll *arr, int pos, ll val) {
if(!pos) return;
for(int x = pos; x <= len; x += lowbit(x)) arr[x] += val;
} ll query(ll *arr, int pos) {
ll ret = ;
for(int x = pos; x; x -= lowbit(x)) ret += arr[x];
return ret;
} ll query(ll *arr, int lef, int rig) {
ll b = query(arr, rig);
ll a = query(arr, lef - );
return b - a;
} void solve() {
for(int i = ; i <= n; ++i) {
lef[i][] = ;
for(int j = ; j <= m; ++j)
if(graph[i][j] == '.') lef[i][j] = ;
else lef[i][j] = lef[i][j - ] + ;
lef[i][m + ] = ;
for(int j = m; j >= ; --j)
if(graph[i][j] == '.') rig[i][j] = ;
else rig[i][j] = rig[i][j + ] + ;
} ll ans = ;
for(int diagon = ; diagon <= n + m - ; ++diagon) {
int stx = max(, diagon - m + ), sty = min(diagon, m);
int tot = ;
bool first = true;
for(int x = stx, y = sty; x <= n && y >= ; ++x, --y)
if(graph[x][y] == '.') {
for(int i = ; i <= tot; ++i) sum[i] = ;
while(!outQue.empty()) outQue.pop();
tot = , first = true;
} else {
if(first) {
len = , first = false;
for(int _x = x, _y = y; _x <= n && _y >= && graph[_x][_y] == 'z'; ++_x, --_y)
++len;
}
++tot;
add(sum, tot, );
outQue.push(mk(-(tot + lef[x][y] - ), tot)); while(-outQue.top().first < tot) {
add(sum, outQue.top().second, -);
outQue.pop();
} ll tmp = query(sum, max(, tot - rig[x][y] + ), tot);
ans += tmp;
}
for(int i = ; i <= tot; ++i) sum[i] = ;
while(!outQue.empty()) outQue.pop();
} cout << ans << endl;
} int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%s", graph[i] + );
solve();
return ;
}

Codeforces CF#628 Education 8 E. Zbazi in Zeydabad的更多相关文章

  1. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. Codeforces CF#628 Education 8 B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces CF#628 Education 8 A. Tennis Tournament

    A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组

    E. Zbazi in Zeydabad 题目连接: http://www.codeforces.com/contest/628/problem/D Description A tourist wan ...

  7. Codeforces 915E Physical Education Lessons

    原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...

  8. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  9. Codeforces Gym101606 E.Education (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))

    E Education 这个题有点意思,就是找满足条件的最小价格里的最大值的人数,有点贪心的思想吧,一开始写错了,人群的那个不能排序,而且是最小价格里找能住下人最多的部门,让这个部门去住这个房间.在循 ...

随机推荐

  1. node.js下使用RSA加密事例(windows)

    1.安装openss 直接下载window下的安装包 http://houjixin.blog.163.com/blog/static/3562841020144143494875/ 以我发博文现在的 ...

  2. ASP.NET运作流程

    当我们在浏览器输入域名访问服务器资源时,会向服务器发送Http请求,并经由IIS处理后,交由ASP.NET托管程序处理,进入ASP.NET管道.在IIS内部如何处理我们不需要深入去了解,在ASP.NE ...

  3. localstorage 和 sessionstorage 本地存储

    在我们日常的工作和实际项目中,做好数据数据缓存可以是我们的程序执行效率更高,可以使我们避免重复请求 服务器,减轻服务器的压力,可以提高使用户的体验度. 那么 HTML5 存储的目标是什么? 1.解决存 ...

  4. 使用angular.bootstrap() 完成模块的手动加载

    之前我们看到使用ng-app指令,可以实现模块的自动加载.现在我们看下,angular中如何手动加载模块.需要使用到angular.bootstrap这个函数. <html> <he ...

  5. list操作

    1.查看列表属性 >>> a = [1,2] >>> dir(a) ['__add__', '__class__', '__contains__', '__dela ...

  6. bootStrap树形目录组件

    需求描述 产品添加页面,需要选择车型.在bootStrap的modal上弹出子modal来使用.车型一共有4级目录.要使用目录树.然后分活动和商品两种,需要能够通过不通参数来调用该组件.车型品牌要使用 ...

  7. Sublime Text 3 常用插件以及安装方法(转)

    http://www.cnsecer.com/460.html 安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧, ...

  8. angular 自定义指令 directive transclude 理解

    项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...

  9. c/c++ 缓冲区的刷新

    利用string 对象查看缓冲区的变化,因为每个string对象在输入时会以空格作为分界. #include<iostream> #include<string> using ...

  10. OC与Swift单例

    OC: +(instancetype)shareNetworkTools{ static id instance; static dispatch_once_t onceToken; //onceTo ...