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. 阿里无线前端性能优化指南 (Pt.1 加载优化)

    前言 阿里无线前端团队在过去一年对所负责业务进行了全面的性能优化.以下是我们根据实际经验总结的优化指南,希望对大家有所帮助. 第一部分仅包括数据加载期优化. 图片控制 对于网页特别是电商类页面来说,图 ...

  2. 元素堆叠问题、z-index、position

    多次在项目中遇到html页面元素的非期待重叠错误,多数还是position定位情况下z-index的问题.其实每次解决类似问题思路大致都是一样的,说到底还是对z-index的理解比较模糊,可以解决问题 ...

  3. python界面

    import easygui as g import sys while 1: g.msgbox("我一定要学会编程!","加油!") #choices = [ ...

  4. appStore上传苹果应用程序软件发布流程

    如有疑问,或者需要人帮忙,可以到QQ群:460325065首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Port ...

  5. Java字节流和字符流区别

    1.字节流:直接操作文件本身. 2.字符流:通过缓冲区来操作文件. 所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,所以在开发中,字节 ...

  6. 解决mysql Table ‘xxx’ is marked as crashed and should be repaired的问题。

    解决mysql Table 'xxx' is marked as crashed and should be repaired的问题. 某个表在进行数据插入和更新时突然出现Table 'xxx' is ...

  7. Unity3D 响应摇杆

    if (Input.GetKeyUp(KeyCode.Joystick1Button0)) { Debug.Log("Joystick1Button0"); } if (Input ...

  8. sql server 2008 R2配置管理

    安装vs2013后,sql server 2008R2配置管理提示“远程过程调用失败” 这是因为vs2013自带的Microsoft SQL Server 2012Local DB与之冲突. 通过升级 ...

  9. 【webGL】threejs常用的api

    /*** 场景(scene) ***/ var scene = new THREE.Scene(); // 创建场景 scene.add(x); // 插入场景 /*** 相机(camera) *** ...

  10. Nginx + Tomcat Windows下的负载均衡配置

     Nginx + Tomcat Windows下的负载均衡配置 一.为什么需要对Tomcat服务器做负载均衡?    Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过50 ...