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$ ...
随机推荐
- SVN弱密码扫描(Python)
寂寞如雪的用脑过度,所以来写个博客分享一下.#虽然上一篇博客我还没写完 SVN的弱密码,看起来很复杂,但实际上很简单啊= =虽然不像pymssql/mymssql这种,Python提供了很好用的包,但 ...
- Experience all that SharePoint 15 has to offer. Start now or Remind me later.
$spSite = Get-SpSite($waUrl); $spSite.AllowSelfServiceUpgrade = $false
- oc 中四种实例变量的范围类型@private@protected@public@package
To enforce the ability of an object to hide its data, the compiler limits the scope of instance vari ...
- java接口与继承
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } p ...
- PDF、WORD、PPT、TXT转换方法
- PAT-乙级-1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 时序列数据库武斗大会之什么是 TSDB ?
本文选自 OneAPM Cloud Insight 高级工程师刘斌博客 . 刘斌,一个才思敏捷的程序员,<第一本 Docker 书>.<GitHub 入门与实践>等书籍译者,D ...
- POJ3282+模拟
模拟题 /* 模拟 注意:相同一边的车有先后顺序! */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...
- jmeter 响应结果分析一
转自:http://www.cnblogs.com/Carrie_Liang/archive/2008/11/05/1327604.html Jmeter测试结果分析这一篇,我打算分成上下两部分.上篇 ...
- 在其他的电脑上配置绿色Jre+tomcat运行环境
其他的同事要使用我们的web程序(基于tomcat的web程序).所以要求是对方的电脑任何程序都不需要安装,把我们的包拷贝过去,直接执行批处理就可以运行. 经过了一番摸索,实现方式如下: 1,准备jr ...