题意:一个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 树状数组的更多相关文章

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

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

  2. CodeForces 828E DNA Evolution(树状数组)题解

    题意:给你一个串k,进行两个操作: “1 a b”:把a位置的字母换成b “2 l r s”:求l到r有多少个字母和s匹配,匹配的条件是这样:从l开始无限循环s形成一个串ss,然后匹配ss和指定区间的 ...

  3. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  4. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  5. Codeforces 635D Factory Repairs【树状数组】

    又是看了很久的题目... 题目链接: http://codeforces.com/contest/635/problem/D 题意: 一家工厂生产维修之前每天生产b个,维修了k天之后每天生产a个,维修 ...

  6. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  7. codeforces E. DNA Evolution(树状数组)

    题目链接:http://codeforces.com/contest/828/problem/E 题解:就是开4个数组举一个例子. A[mod][res][i]表示到i位置膜mod余数是res的‘A’ ...

  8. Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]

    题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...

  9. codeforces#1167F. Scalar Queries(树状数组+求贡献)

    题目链接: https://codeforces.com/contest/1167/problem/F 题意: 给出长度为$n$的数组,初始每个元素为$a_i$ 定义:$f(l, r)$为,重排$l$ ...

随机推荐

  1. hdu 4740 The Donkey of Gui Zhou(暴力搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4740 [题意]: 森林里有一只驴和一只老虎,驴和老虎互相从来都没有见过,各自自己走过的地方不能走第二次 ...

  2. 怎样修改Windows7环境变量

    在使用电脑的时候要运行某些特定的应用程序时需要修改系统的环境变量,例如安装JAVA时我们就需要配置系统的环境变量.那什么是环境变量呢?环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比 ...

  3. IE 浏览器下 button元素自动触发click?

    为什么这么说呢?且看下面的情形: 当用户焦点在输入框中的时候,当用户按了“Enter”键之后,达到点击登录的效果:所以这里就监听了input的keydown事件,当keyCode是13的时候,就发送请 ...

  4. Java 垃圾回收机制

    1.delete是C++里面用于释放内存的运算符,而不是Java. 2.当发现某个对象的引用计数为0时,就将对象列入待回收列表中,并不是马上予以销毁. 3.System.gc()仅仅是一个回收请求,J ...

  5. ubuntu下如何设置主机名

    方法如下: 在终端输入 hostname 查看主机名主机名存放在/etc/hostname中 ,sudo gedit /etc/hostname 修改后保存/etc/hosts 还有一份 , sudo ...

  6. Qt: 把内容写进字符串中与C++很相似(使用QTextStream包装QString)

    #include <iostream>#include <QChar>#include <QFile>#include <QTextStream>#in ...

  7. java:复写equals实例

    class User { String name; int age; /* *比较过程思路: *1.两个对象指向位置相同,那么他们就相等,return后跳出函数,不再往下执行 *2.指向位置不同,有3 ...

  8. Linux的终端与进程

    原文链接:http://os.51cto.com/art/201104/256477.htm Linux的普通进程(守护进程除外) 是终端的子进程,进程的存在要依赖终端为其提供空间包括标准输入.标准输 ...

  9. redolog 大小的实验

    前言:近日因工作需要,测试postgresql和MySQL在oltp对比测试,因结果差异太多(MySQL测试结果比较差,相同环境),寻求大神帮助,有幸得叶大师和姜大师指点,指出my.cnf配置文件in ...

  10. 【HDOJ】4089 Activation

    1. 题目描述长度为n的等待队列,tomato处于第m个,有如下四种可能:(1)激活失败,概率为$p_1$,队列中的顺序不变:(2)连接失败,概率为$p_2$,队头玩家重新排在队尾:(3)激活成功,概 ...