Codeforces CF#628 Education 8 E. Zbazi in Zeydabad
5 seconds
512 megabytes
standard input
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.
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.
Print the only integer a — the number of ''Z-pattern"s in Zeydabad.
4 4
zzzz
zzz.
.z..
zzzz
16
1 4
z.z.
2
2 2
zz
zz
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组
E. Zbazi in Zeydabad 题目连接: http://www.codeforces.com/contest/628/problem/D Description A tourist wan ...
- Codeforces 915E Physical Education Lessons
原题传送门 我承认,比赛的时候在C题上卡了好久(最后也不会),15min水掉D后(最后还FST了..),看到E时已经只剩15min了.尽管一眼看出是离散化+线段树的裸题,但是没有时间写,实在尴尬. 赛 ...
- codeforces 893F - Physical Education Lessons 动态开点线段树合并
https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...
- Codeforces Gym101606 E.Education (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
E Education 这个题有点意思,就是找满足条件的最小价格里的最大值的人数,有点贪心的思想吧,一开始写错了,人群的那个不能排序,而且是最小价格里找能住下人最多的部门,让这个部门去住这个房间.在循 ...
随机推荐
- Markdown 语法简要介绍
=================MarkDown================= Markdown 是一种方便记忆.书写的纯文本标记语言,用户可以使用这些标记符号以最小的输入代价生成极富表现力的文 ...
- 使用bootstrap的日期插件
1. 需要用的js包点击下载,在项目中引入该js. <script language="JavaScript" src="${pageContext.reques ...
- OC编程之道-创建对象之抽象工厂方法
定义:提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. <AbstractProductA> <AbstractProductB> <Ab ...
- Jquery获取select选中的文本与值
jquery获取select选择的文本与值获取select :获取select 选中的 text : $("#ddlregtype").find("option:s ...
- DataTable导出到Excel
简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...
- 打开eclipse报错
隔了一段时间没用eclipse, 打开之后报错: 从报错上来看是因为java版本太低导致的. 我打开cmd, 运行java -version 后 发现java 版本已经更新到了1.8 然后就有点懵. ...
- web.config数据库连接字符串
我们在做VB数据库经典实例这本书上的五个小例子和做学生信息管理系统时,都需要通过Vb链接数据库,在众多的链接方式中connectionstring字符串应该是较为简单的一种方式,下面我来详细介绍它的一 ...
- 将做好的py文件打包成模块,供别人安装调用
现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...
- .net中的反射(转载)
原文地址:http://www.cnblogs.com/Stephenchao/p/4481995.html 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的 ...
- 第六课 touch事件
1.移动端页面在PC上浏览时,限制宽度的方法: 2.移动端页面切换设备时自动刷新页面的方法: 3.touch事件 touchstart:当手指触摸屏幕时触发.通过addEventListener添加移 ...