Codeforces 628E Zbazi in Zeydabad 树状数组
题意:一个n*m的矩阵,要么是 . 要么是 z ,问可以形成几个大z
分析:(直接奉上官方题解,我感觉说的实在是太好了)
Let's precalculate the values zlij, zrij, zldij — the maximal number of letters 'z' to the left, to the right and to the left-down from the position (i, j). It's easy to do in O(nm) time. Let's fix some cell (i, j). Consider the value c = min(zlij, zldij). It's the maximum size of the square with upper right ceil in (i, j). But the number of z-patterns can be less than c. Consider some cell (x, y) diagonally down-left from (i, j) on the distance no more than c. The cells (i, j) and (x, y) forms z-pattern if y + zrxy > j.
Let's maintain some data structure for each antidiagonal (it can be described by formula x + y) that can increment in a point and take the sum on a segment (Fenwick tree will be the best choice for that). Let's iterate over columns j from the right to the left and process the events: we have some cell (x, y) for which y + zrxy - 1 = j. In that case we should increment the position y in the tree number x + y by one. Now we should iterate over the cells (x, y) in the current column and add to the answer the value of the sum on the segment from j - c + 1 to j in the tree number i + j .
补充:这样从右到左更新,当更新第j列的时候,每一条对角线上的 能够向右延伸到大于等于j的"z"都已经更新完毕,直接统计就行
时间复杂度O(nmlogm)
代码:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 3e3+;
char s[N][N];
short l[N][N],r[N][N],x[N][N];
int n,m;
short c[N*][N];
void add(int x,int i)
{
for(; i<=m; i+=(i&(-i)))
c[x][i]++;
}
int sum(int x,int i)
{
int res=;
for(; i>; i-=(i&(-i)))
res+=c[x][i];
return res;
}
struct Point
{
int x,y;
};
vector<Point>g[N];
int main()
{
LL ans=;
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)
scanf("%s",s[i]+);
for(int i=; i<=n; ++i)
{
for(int j=,k=m; j<=m; ++j,--k)
{
if(s[i][j]=='z')l[i][j]=l[i][j-]+;
if(s[i][k]=='z')r[i][k]=r[i][k+]+;
}
}
for(int j=; j<=m; ++j)
{
for(int i=; i<=n; ++i)
{
if(s[i][j]=='.')continue;
x[i][j]=x[i+][j-]+;
}
}
for(int i=; i<=n; ++i)
for(int j=; j<=m; ++j)
if(s[i][j]=='z')
g[j+r[i][j]-].push_back(Point {i,j});
for(int j=m; j>=; --j)
{
for(int i=; i<g[j].size(); ++i)
{
int t=g[j][i].x+g[j][i].y;
add(t,g[j][i].y);
}
for(int i=; i<=n; ++i)
{
if(s[i][j]=='.')continue;
int p=min(l[i][j],x[i][j]);
ans+=sum(i+j,j)-sum(i+j,j-p);
}
}
printf("%I64d\n",ans);
return ;
}
Codeforces 628E Zbazi in Zeydabad 树状数组的更多相关文章
- 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 828E DNA Evolution(树状数组)题解
题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- CodeForces - 597C Subsequences 【DP + 树状数组】
题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...
- Codeforces 635D Factory Repairs【树状数组】
又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces E. DNA Evolution(树状数组)
题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...
- Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]
题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...
- codeforces#1167F. Scalar Queries(树状数组+求贡献)
题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...
随机推荐
- Python之print语句
print语句可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' 注意: 1.当我们在Python交 ...
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)
Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...
- spoj 362
规律还是比较好找的 大数除法 #include <cstdio> #include <cstring> int len,a[1000],q; int cc[] = {0,1, ...
- javascript div z-index, input tabindex属性说明
<html> <body> <form> 用户名: <input type="text" tabindex="1" / ...
- HDU4545+LCS
最长公共子序列. /* LCS 最长公共子序列 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
- UIActinSheet和UIActionSheetDelegate
UIActinSheet和UIActionSheetDelegate 这个是就那个UIActionSheet对象 一般用来选择类型或者改变界面...还有更多应用 定义如下:UIActionSheet ...
- linux配置防火墙详细步骤(iptables命令使用方法)
通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...
- IOS开发基础
http://blog.csdn.net/wokenshin/article/details/50292253 1.修改UI大小 2.设置颜色 3.禁止横屏 4.点击空白处隐藏键盘 5.弹出键盘时,后 ...
- jquery uploadify上传文件插件导致浏览器崩溃问题解决方法
自谷歌浏览器更新到(版本39.0.2171.99 )后,访问上传文件界面浏览器就崩溃了,而其他的浏览器不会出现问题. 出现这种问题的原因就是谷歌浏览器缓存问题,但将访问该jsp页面路径添加上时间戳后无 ...
- 条件与(&&)和逻辑与(&)以及条件或(||)和逻辑或(|)区别
条件与(&&)和逻辑与(&)以及条件或(||)和逻辑或(|)区别在于它们的运算结果是不相同的. 条件与(&&)和条件或(||)采用的是所谓的"短路规则 ...