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$ ...
随机推荐
- google calendar api v3
google api for .net nuget Install-Package Google.Apis.Calendar.v3 oauth2 for asp.net http://www.code ...
- iOS百度地图路径规划和POI检索详细总结-b
路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj.png 如上图所示,在infoplist里 ...
- 原创新闻 11 个最佳 jQuery 滚动条插件
通过jQuery滚动条插件,你可以换掉千篇一律的默认浏览器滚动条,让你的网站或web项目更具特色,更有吸引力.本文收集了11款非常漂亮.实用的jQuery滚动条插件,你可以轻松将它们应用在自己的网站中 ...
- HTTP协议的几个重要概念
转自:http://ice-cream.iteye.com/blog/77549 1.连接(Connection):一个传输层的实际环流,它是建立在两个相互通讯的应用程序之间. 2.消息(Messag ...
- uva 10810
刘汝佳书上 143 归并排序求逆序数对 #include <cstdio> #include <cstring> #include <cstdlib> #incl ...
- [BEC][hujiang] Lesson03 Unit1:Working life ---Grammar & Listening & Vocabulary
3 Working life p8 Grammar Gerund and infinitive(动名词和不定式) 一般而言: 1 动词后面接动名词还是不定式没有特定规则,主要取决于语言习 ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- C#一个简单下载程序实例(可用于更新)
运行时的界面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- iOS开发UI篇—程序启动原理和UIApplication1
iOS开发UI篇—程序启动原理和UIApplication 一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...
- 【转】VMware设置共享文件夹之后Ubuntu中看不到怎么办?
一.共享文件夹设置好了,但是在虚拟机中的Ubuntu系统下却看不到,怎么办? 一种可能的原因是系统没有自动挂载,解决办法: 1.安装: sudo apt-get insta ...