Zbazi in Zeydabad CodeForces - 628E 树状数组
2017-11-03 23:25
323 查看
A tourist wants to visit country Zeydabad for Zbazi (a local game in Zeydabad).
The country Zeydabad is a rectangular table consisting of n rows and m columns. Each cell on the country is either ‘z’ or ‘.’.
The tourist knows this country is named Zeydabad because there are lots of ”Z-pattern”s in the country. A ”Z-pattern” is a square which anti-diagonal is completely filled with ‘z’ and its upper and lower rows are also completely filled with ‘z’. All other cells of a square can be arbitrary.
Note that a ”Z-pattern” can consist of only one cell (see the examples).
So he wants to count the number of ”Z-pattern”s in the country (a necessary skill for Zbazi).
Now your task is to help tourist with counting number of ”Z-pattern”s.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 3000) — the number of rows and columns respectively.
Each of the next n lines contains m characters ‘z’ or ‘.’ — the description of Zeydabad.
Output
Print the only integer a — the number of ”Z-pattern”s in Zeydabad.
Example
Input
4 4
zzzz
zzz.
.z..
zzzz
Output
16
Input
1 4
z.z.
Output
2
Input
2 2
zz
zz
Output
5
在一个正方形内,第一行和最后一行和反对角线全是’z’字符,则算满足条件,求有几个这样的正方形。
预处理每个点左边,右边,左下可以延伸的长度,暴力的思路是枚举每个点作为右上角,正方形的最大边长是第一条边和对角线的最小值,然后在范围内判断对角线上的每个点向右延伸的长度是否满足条件,每有一个点满足就是符合的图形。
由于不同的正方形对角线可能共线,有重叠的部分,判断区间内个数的操作可以用线段树优化。
预处理时记录每条线段的右端点,然后横坐标从右往左读取线段,每个读入的线段左端点归属于唯一的一条对角线i+k,对角线i+k上i位置出发的线条计数+1,而从右往左读取保证了此时已读取的线条右端点一定在横坐标j右边。接着区间查询当前枚举的顶点所在的对角线上,左端点在对角线上且在区间[i,i+len-1]中,右端点>=j(已经读取的一定满足)的线段。
The country Zeydabad is a rectangular table consisting of n rows and m columns. Each cell on the country is either ‘z’ or ‘.’.
The tourist knows this country is named Zeydabad because there are lots of ”Z-pattern”s in the country. A ”Z-pattern” is a square which anti-diagonal is completely filled with ‘z’ and its upper and lower rows are also completely filled with ‘z’. All other cells of a square can be arbitrary.
Note that a ”Z-pattern” can consist of only one cell (see the examples).
So he wants to count the number of ”Z-pattern”s in the country (a necessary skill for Zbazi).
Now your task is to help tourist with counting number of ”Z-pattern”s.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 3000) — the number of rows and columns respectively.
Each of the next n lines contains m characters ‘z’ or ‘.’ — the description of Zeydabad.
Output
Print the only integer a — the number of ”Z-pattern”s in Zeydabad.
Example
Input
4 4
zzzz
zzz.
.z..
zzzz
Output
16
Input
1 4
z.z.
Output
2
Input
2 2
zz
zz
Output
5
在一个正方形内,第一行和最后一行和反对角线全是’z’字符,则算满足条件,求有几个这样的正方形。
预处理每个点左边,右边,左下可以延伸的长度,暴力的思路是枚举每个点作为右上角,正方形的最大边长是第一条边和对角线的最小值,然后在范围内判断对角线上的每个点向右延伸的长度是否满足条件,每有一个点满足就是符合的图形。
由于不同的正方形对角线可能共线,有重叠的部分,判断区间内个数的操作可以用线段树优化。
预处理时记录每条线段的右端点,然后横坐标从右往左读取线段,每个读入的线段左端点归属于唯一的一条对角线i+k,对角线i+k上i位置出发的线条计数+1,而从右往左读取保证了此时已读取的线条右端点一定在横坐标j右边。接着区间查询当前枚举的顶点所在的对角线上,左端点在对角线上且在区间[i,i+len-1]中,右端点>=j(已经读取的一定满足)的线段。
#include <iostream> #include <stdio.h> #include <map> #include <set> #include <queue> #include <algorithm> #include <vector> #include <math.h> #include <iterator> #include <string.h> using namespace std; typedef long long ll; int mo[4][2]={0,1,1,0,0,-1,-1,0}; const int MAXN=0x3f3f3f3f; const int sz=3005; int n,m; ll ans; char mp[sz][sz]; ll cnt[2*sz][sz];//储存量有i+j int l[sz][sz],r[sz][sz],dia[sz][sz],ed[sz][sz]; void add(int num,int j){ while(j<=n){ cnt[num][j]++; j+=j&(-j); } } ll sum(int num,int j){ int re=0; while(j>0){ re+=cnt[num][j]; j-=j&(-j); } return re; } int main() { //freopen("r.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ scanf("%s",mp[i]+1); } ans=0; memset(l,0,sizeof(l)); memset(r,0,sizeof(r)); memset(cnt,0,sizeof(cnt)); memset(dia,0,sizeof(dia)); memset(ed,0,sizeof(ed)); for(int i=n;i>=1;i--){ for(int j=1;j<=m;j++){ if(mp[i][j]=='z'){ l[i][j]=l[i][j-1]+1; }else{ l[i][j]=0; } } for(int j=m;j>=1;j--){ if(mp[i][j]=='z'){ r[i][j]=r[i][j+1]+1; if(mp[i][j+1]!='z'){ ed[i][j]=1; } }else{ r[i][j]=0; } } for(int j=1;j<=m;j++){ if(mp[i][j]=='z'){ dia[i][j]=dia[i+1][j-1]+1; }else{ dia[i][j]=0; } } } for(int j=m;j>=1;j--){ for(int i=1;i<=n;i++){ if(ed[i][j]){//ed表示有在j处结尾的线段 int k=j; for(;mp[i][k]=='z';k--){ add(i+k,i);//i+k为对角线编号,线段起点,i是该线段在对角线上属于的行数,用于确定范围 //现在加入的线段和已经加入的线段右端点都大于等于J,保证满足要求 } } } for(int i=1;i<=n;i++){ int len=min(l[i][j],dia[i][j]); ll t=(sum(i+j,i+len-1)-sum(i+j,i-1)); ans+=t; } } cout<<ans<<endl; } return 0; }
相关文章推荐
- Codeforces edu 8 E. Zbazi in Zeydabad 树状数组 处理技巧
- Codeforces 628E:Zbazi in Zeydabad 树状数组的奇妙用法
- Codeforces 390E Inna and Large Sweet Matrix(树状数组)
- Codeforces 827C DNA Evolution(多维树状数组)
- Codeforces 12D Ball (树状数组)
- codeforces CF703D Mishka and Interesting sum 树状数组
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- codeforces 459D Pashmak and Parmida's problem 树状数组
- Codeforces 948C - Producing Snow (树状数组)
- Codeforces 396C On Changing Tree(树状数组)
- Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
- 【树状数组】[CodeForces - 341D]Iahub and Xors
- CodeForces 121E -- Lucky Array (树状数组)
- Codeforces 459D Pashmak and Parmida's problem【树状数组】
- Nested Segments CodeForces - 652D 树状数组
- Codeforces 387E George and Cards【思维+RMQ+二分+树状数组】被卡常= =
- Codeforces 703D Mishka and Interesting sum 树状数组
- CodeForces 605D Board Game(树状数组)
- Codeforces 669E Little Artem and Time Machine (离散化树状数组)
- Codeforces 635D Factory Repairs【树状数组】