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$ ...
随机推荐
- C# json to dynamic object
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(json); string greeting = obj.greeting; R ...
- Mac 使用phpMyAdmin
1 把phpMyAdmin-4.6.5.2-all-languages.zip文件解压到“/Library/WebServer/Documents/”中,并改名为phpmyadmin. 2 复制“/L ...
- ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...
- [转]oracle10.2.0.1下载链接
记录一下ORACLE 10gR2的软件下载地址,备用. 下载OTN上的这些软件,你需要一个OTN免费帐号,不过如果通过迅雷进行下载,就不用登陆OTN了: Oracle Database 10g Rel ...
- HDU 1422 重温世界杯(DP)
点我看题目 题意 : 中文题不详述. 思路 : 根据题目描述及样例可以看出来,如果你第一个城市选的是生活费减花费大于等于0的时候才可以,最好是多余的,这样接下来的就算是花超了(一定限度内的花超),也可 ...
- cocos2d-x 锚点,位置==》动手实验记录 多动手... :)
总结: 1:cocos2d-x的位置由锚点和Position位置 共同决定的.2: cocos2d-x,当位置不设置或者为零的时候, 子节点的锚点位置永远位于父节点的左小角的地方3:我们的自己的游戏编 ...
- linux2.6中的工作队列接口 workqueue_struct
http://blog.csdn.net/sfrysh/article/details/5801786 工作队列接口 工作队列接口是在2.5的开发过程中引入的,用于取代任务队列接口(用于调 度内核任务 ...
- PRMonitor,学习SSDT HOOK的好例子,学习的内核HOOK的好例子(注意右边有一堆类似的例子)
http://download.csdn.net/detail/bolong5240/1060645
- 删除appcompat_v7会出很多错误
创建工程的时候会出现appcompat_v7这个文件夹 手贱删除后,发现出错了 说明test项目是依赖于appcompat_v7包的,所以这个appcompat_v7包是不能被删除的. appcomp ...
- Spring中的Resource
Spring中的资源定义:Resource此接口的全名为:org.springframework.core.io.Resource比较常用的资源定义的实现类为:1.ClassPathResource ...